UNPKG

@nx/vite

Version:

The Nx Plugin for building and testing applications using Vite

590 lines (579 loc) • 18.2 kB
"use strict"; function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { mockAngularAppGenerator: function() { return mockAngularAppGenerator; }, mockReactAppGenerator: function() { return mockReactAppGenerator; }, mockReactLibNonBuildableJestTestRunnerGenerator: function() { return mockReactLibNonBuildableJestTestRunnerGenerator; }, mockReactLibNonBuildableVitestRunnerGenerator: function() { return mockReactLibNonBuildableVitestRunnerGenerator; }, mockReactMixedAppGenerator: function() { return mockReactMixedAppGenerator; }, mockUnknownAppGenerator: function() { return mockUnknownAppGenerator; }, mockViteReactAppGenerator: function() { return mockViteReactAppGenerator; }, mockWebAppGenerator: function() { return mockWebAppGenerator; } }); const _extends = require("@swc/helpers/_/_extends"); const _devkit = require("@nx/devkit"); const _reactviteprojectconfigjson = require("./test-files/react-vite-project.config.json"); const _angularprojectconfigjson = require("./test-files/angular-project.config.json"); const _unknownprojectconfigjson = require("./test-files/unknown-project.config.json"); const _reactmixedprojectconfigjson = require("./test-files/react-mixed-project.config.json"); const _reactlibnonbuildablejestjson = require("./test-files/react-lib-non-buildable-jest.json"); const _reactlibnonbuildablevitestjson = require("./test-files/react-lib-non-buildable-vitest.json"); function mockViteReactAppGenerator(tree) { const appName = 'my-test-react-vite-app'; tree.write(`apps/${appName}/src/main.tsx`, `import ReactDOM from 'react-dom';\n`); tree.write(`apps/${appName}/tsconfig.json`, `{ "compilerOptions": { "jsx": "react-jsx", "allowJs": false, "esModuleInterop": false, "allowSyntheticDefaultImports": true, "strict": true, "types": ["vite/client"] }, "files": [], "include": [], "references": [ { "path": "./tsconfig.app.json" }, { "path": "./tsconfig.spec.json" } ], "extends": "../../tsconfig.base.json" } `); tree.write(`apps/${appName}/tsconfig.app.json`, `{ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "../../dist/out-tsc", "types": ["node"] }, "files": [ "../../node_modules/@nx/react/typings/cssmodule.d.ts", "../../node_modules/@nx/react/typings/image.d.ts" ], "exclude": [ "src/**/*.spec.ts", "src/**/*.test.ts", "src/**/*.spec.tsx", "src/**/*.test.tsx", "src/**/*.spec.js", "src/**/*.test.js", "src/**/*.spec.jsx", "src/**/*.test.jsx" ], "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] } `); tree.write(`apps/${appName}/index.html`, `<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Rv1</title> <base href="/" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="icon" type="image/x-icon" href="/favicon.ico" /> <link rel="stylesheet" href="/src/styles.css" /> </head> <body> <div id="root"></div> <script type="module" src="/src/main.tsx"></script> </body> </html>`); tree.write(`apps/${appName}/vite.config.ts`, ` /// <reference types="vitest" /> import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import tsconfigPaths from 'vite-tsconfig-paths'; export default defineConfig({ cacheDir: '../../node_modules/.vitest', server: { port: 4200, host: 'localhost', }, plugins: [ react(), tsconfigPaths({ root: '../../', projects: ['tsconfig.base.json'], }), ], test: { globals: true, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], }, }); `); (0, _devkit.writeJson)(tree, 'workspace.json', { projects: { 'my-test-react-vite-app': _extends._({}, _reactviteprojectconfigjson, { root: `apps/${appName}`, projectType: 'application' }) } }); (0, _devkit.writeJson)(tree, `apps/${appName}/project.json`, _extends._({}, _reactviteprojectconfigjson, { root: `apps/${appName}`, projectType: 'application' })); return tree; } function mockReactAppGenerator(tree, userAppName) { const appName = userAppName != null ? userAppName : 'my-test-react-app'; tree.write(`apps/${appName}/src/main.tsx`, `import ReactDOM from 'react-dom';\n`); tree.write(`apps/${appName}/webpack.config.ts`, ``); tree.write(`apps/${appName}/tsconfig.json`, `{ "extends": "../../tsconfig.base.json", "compilerOptions": { "jsx": "react-jsx", "allowJs": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitOverride": true, "noPropertyAccessFromIndexSignature": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true }, "files": [], "include": [], "references": [ { "path": "./tsconfig.app.json" }, { "path": "./tsconfig.spec.json" } ] } `); tree.write(`apps/${appName}/tsconfig.app.json`, `{ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "../../dist/out-tsc" }, "files": [ "../../node_modules/@nx/react/typings/cssmodule.d.ts", "../../node_modules/@nx/react/typings/image.d.ts" ], "exclude": [ "jest.config.ts", "**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx", "**/*.spec.js", "**/*.test.js", "**/*.spec.jsx", "**/*.test.jsx" ], "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] } `); tree.write(`apps/${appName}/src/index.html`, `<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>My Test React App</title> <base href="/" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="icon" type="image/x-icon" href="favicon.ico" /> </head> <body> <div id="root"></div> </body> </html>`); (0, _devkit.writeJson)(tree, `apps/${appName}/project.json`, { root: `apps/${appName}`, projectType: 'application' }); return tree; } function mockReactMixedAppGenerator(tree) { const appName = 'my-test-mixed-react-app'; tree.write(`apps/${appName}/src/main.tsx`, `import ReactDOM from 'react-dom';\n`); tree.write(`apps/${appName}/tsconfig.json`, `{ "extends": "../../tsconfig.base.json", "compilerOptions": { "jsx": "react-jsx", "allowJs": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitOverride": true, "noPropertyAccessFromIndexSignature": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true }, "files": [], "include": [], "references": [ { "path": "./tsconfig.app.json" }, { "path": "./tsconfig.spec.json" } ] } `); tree.write(`apps/${appName}/tsconfig.app.json`, `{ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "../../dist/out-tsc" }, "files": [ "../../node_modules/@nx/react/typings/cssmodule.d.ts", "../../node_modules/@nx/react/typings/image.d.ts" ], "exclude": [ "jest.config.ts", "**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx", "**/*.spec.js", "**/*.test.js", "**/*.spec.jsx", "**/*.test.jsx" ], "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] } `); tree.write(`apps/${appName}/src/index.html`, `<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>My Test React App</title> <base href="/" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="icon" type="image/x-icon" href="favicon.ico" /> </head> <body> <div id="root"></div> </body> </html>`); (0, _devkit.writeJson)(tree, 'workspace.json', { projects: { 'my-test-mixed-react-app': _extends._({}, _reactmixedprojectconfigjson, { root: `apps/${appName}`, projectType: 'application' }) } }); (0, _devkit.writeJson)(tree, `apps/${appName}/project.json`, _extends._({}, _reactmixedprojectconfigjson, { root: `apps/${appName}`, projectType: 'application' })); return tree; } function mockWebAppGenerator(tree) { const appName = 'my-test-web-app'; tree.write(`apps/${appName}/src/main.ts`, `import './app/app.element.ts';`); tree.write(`apps/${appName}/tsconfig.json`, `{ "extends": "../../tsconfig.base.json", "files": [], "include": [], "references": [ { "path": "./tsconfig.app.json" }, { "path": "./tsconfig.spec.json" } ] } `); tree.write(`apps/${appName}/webpack.config.ts`, ``); tree.write(`apps/${appName}/src/index.html`, `<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>WebappPure</title> <base href="/" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="icon" type="image/x-icon" href="favicon.ico" /> </head> <body> <workspace-root></workspace-root> </body> </html> `); (0, _devkit.writeJson)(tree, 'workspace.json', { projects: { 'my-test-web-app': { root: `apps/${appName}`, projectType: 'application' } } }); (0, _devkit.writeJson)(tree, `apps/${appName}/project.json`, { root: `apps/${appName}`, projectType: 'application' }); return tree; } function mockAngularAppGenerator(tree) { const appName = 'my-test-angular-app'; (0, _devkit.writeJson)(tree, 'workspace.json', { projects: { 'my-test-angular-app': _extends._({}, _angularprojectconfigjson, { root: `apps/${appName}`, projectType: 'application' }) } }); (0, _devkit.writeJson)(tree, `apps/${appName}/project.json`, _extends._({}, _angularprojectconfigjson, { root: `apps/${appName}`, projectType: 'application' })); (0, _devkit.writeJson)(tree, `apps/${appName}/tsconfig.json`, { compilerOptions: { target: 'es2022', esModuleInterop: true, forceConsistentCasingInFileNames: true, strict: true, noImplicitOverride: true, noPropertyAccessFromIndexSignature: true, noImplicitReturns: true, noFallthroughCasesInSwitch: true }, files: [], include: [], references: [ { path: './tsconfig.editor.json' }, { path: './tsconfig.app.json' }, { path: './tsconfig.spec.json' } ], extends: '../../tsconfig.base.json', angularCompilerOptions: { enableI18nLegacyMessageIdFormat: false, strictInjectionParameters: true, strictInputAccessModifiers: true, strictTemplates: true } }); (0, _devkit.writeJson)(tree, `apps/${appName}/tsconfig.app.json`, { extends: './tsconfig.json', compilerOptions: { outDir: '../../dist/out-tsc', types: [] }, files: [ 'src/main.ts' ], include: [ 'src/**/*.d.ts' ], exclude: [ 'jest.config.ts', 'src/**/*.test.ts', 'src/**/*.spec.ts' ] }); (0, _devkit.writeJson)(tree, `apps/${appName}/tsconfig.spec.json`, { extends: './tsconfig.json', compilerOptions: { outDir: '../../dist/out-tsc', module: 'commonjs', target: 'es2016', types: [ 'jest', 'node' ] }, files: [ 'src/test-setup.ts' ], include: [ 'jest.config.ts', 'src/**/*.test.ts', 'src/**/*.spec.ts', 'src/**/*.d.ts' ] }); return tree; } function mockUnknownAppGenerator(tree) { const appName = 'my-test-random-app'; (0, _devkit.writeJson)(tree, 'workspace.json', { projects: { 'my-test-random-app': _extends._({}, _unknownprojectconfigjson, { root: `apps/${appName}`, projectType: 'application' }) } }); (0, _devkit.writeJson)(tree, `apps/${appName}/project.json`, _extends._({}, _unknownprojectconfigjson, { root: `apps/${appName}`, projectType: 'application' })); return tree; } function mockReactLibNonBuildableJestTestRunnerGenerator(tree) { const libName = 'react-lib-nonb-jest'; tree.write(`libs/${libName}/src/index.ts`, ``); tree.write(`libs/${libName}/tsconfig.json`, `{ "compilerOptions": { "jsx": "react-jsx", "allowJs": false, "esModuleInterop": false, "allowSyntheticDefaultImports": true, "strict": true }, "files": [], "include": [], "references": [ { "path": "./tsconfig.lib.json" }, { "path": "./tsconfig.spec.json" } ], "extends": "../../tsconfig.base.json" }`); tree.write(`libs/${libName}/tsconfig.lib.json`, `{ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "../../dist/out-tsc", "types": ["node"] }, "files": [ "../../node_modules/@nx/react/typings/cssmodule.d.ts", "../../node_modules/@nx/react/typings/image.d.ts" ], "exclude": [ "jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts", "src/**/*.spec.tsx", "src/**/*.test.tsx", "src/**/*.spec.js", "src/**/*.test.js", "src/**/*.spec.jsx", "src/**/*.test.jsx" ], "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] }`); (0, _devkit.writeJson)(tree, 'workspace.json', { projects: { [`${libName}`]: _extends._({}, _reactlibnonbuildablejestjson, { root: `libs/${libName}`, projectType: 'library' }) } }); (0, _devkit.writeJson)(tree, `libs/${libName}/project.json`, _extends._({}, _reactlibnonbuildablejestjson, { root: `libs/${libName}`, projectType: 'library' })); return tree; } function mockReactLibNonBuildableVitestRunnerGenerator(tree) { const libName = 'react-lib-nonb-vitest'; tree.write(`libs/${libName}/src/index.ts`, ``); tree.write(`libs/${libName}/vite.config.ts`, `/// <reference types="vitest" /> import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; export default defineConfig({ cacheDir: '../../node_modules/.vitest', plugins: [ nxViteTsPaths(), react(), ], test: { globals: true, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], }, }); `); tree.write(`libs/${libName}/tsconfig.json`, `{ "compilerOptions": { "jsx": "react-jsx", "allowJs": false, "esModuleInterop": false, "allowSyntheticDefaultImports": true, "strict": true }, "files": [], "include": [], "references": [ { "path": "./tsconfig.lib.json" }, { "path": "./tsconfig.spec.json" } ], "extends": "../../tsconfig.base.json" }`); tree.write(`libs/${libName}/tsconfig.lib.json`, `{ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "../../dist/out-tsc", "types": ["node"] }, "files": [ "../../node_modules/@nx/react/typings/cssmodule.d.ts", "../../node_modules/@nx/react/typings/image.d.ts" ], "exclude": [ "**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx", "**/*.spec.js", "**/*.test.js", "**/*.spec.jsx", "**/*.test.jsx" ], "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] }`); (0, _devkit.writeJson)(tree, 'workspace.json', { projects: { [`${libName}`]: _extends._({}, _reactlibnonbuildablevitestjson, { root: `libs/${libName}`, projectType: 'library' }) } }); (0, _devkit.writeJson)(tree, `libs/${libName}/project.json`, _extends._({}, _reactlibnonbuildablevitestjson, { root: `libs/${libName}`, projectType: 'library' })); return tree; } //# sourceMappingURL=test-utils.js.map