feat(react): add swc as compiler option for react vite (#17224)
This commit is contained in:
parent
7c9ec7241e
commit
631a948ded
@ -171,7 +171,7 @@
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["babel", "swc"],
|
"enum": ["babel", "swc"],
|
||||||
"default": "babel",
|
"default": "babel",
|
||||||
"description": "Which compiler to use. Only applies to `bundler: 'rollup'`."
|
"description": "Which compiler to use."
|
||||||
},
|
},
|
||||||
"skipPackageJson": {
|
"skipPackageJson": {
|
||||||
"description": "Do not add dependencies to `package.json`.",
|
"description": "Do not add dependencies to `package.json`.",
|
||||||
|
|||||||
@ -32,6 +32,12 @@
|
|||||||
"default": "none",
|
"default": "none",
|
||||||
"x-prompt": "What UI framework plugin should Vite use?"
|
"x-prompt": "What UI framework plugin should Vite use?"
|
||||||
},
|
},
|
||||||
|
"compiler": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Compiler to use for Vite when UI Framework is React.",
|
||||||
|
"enum": ["babel", "swc"],
|
||||||
|
"default": "babel"
|
||||||
|
},
|
||||||
"newProject": {
|
"newProject": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"description": "Is this a new project?",
|
"description": "Is this a new project?",
|
||||||
|
|||||||
@ -15,6 +15,12 @@
|
|||||||
"default": "react",
|
"default": "react",
|
||||||
"x-prompt": "What UI framework plugin should Vite use?"
|
"x-prompt": "What UI framework plugin should Vite use?"
|
||||||
},
|
},
|
||||||
|
"compiler": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Compiler to use for Vite when UI Framework is React.",
|
||||||
|
"enum": ["babel", "swc"],
|
||||||
|
"default": "babel"
|
||||||
|
},
|
||||||
"includeLib": {
|
"includeLib": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"description": "Add dependencies needed to build libraries.",
|
"description": "Add dependencies needed to build libraries.",
|
||||||
|
|||||||
@ -19,11 +19,32 @@ describe('Build React applications and libraries with Vite', () => {
|
|||||||
cleanupProject();
|
cleanupProject();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should test and lint app with bundler=vite', async () => {
|
it('should test and lint app with bundler=vite and compiler=babel', async () => {
|
||||||
const viteApp = uniq('viteapp');
|
const viteApp = uniq('viteapp');
|
||||||
|
|
||||||
runCLI(
|
runCLI(
|
||||||
`generate @nx/react:app ${viteApp} --bundler=vite --unitTestRunner=vitest --no-interactive`
|
`generate @nx/react:app ${viteApp} --bundler=vite --compiler=babel --unitTestRunner=vitest --no-interactive`
|
||||||
|
);
|
||||||
|
|
||||||
|
const appTestResults = await runCLIAsync(`test ${viteApp}`);
|
||||||
|
expect(appTestResults.combinedOutput).toContain(
|
||||||
|
'Successfully ran target test'
|
||||||
|
);
|
||||||
|
|
||||||
|
const appLintResults = await runCLIAsync(`lint ${viteApp}`);
|
||||||
|
expect(appLintResults.combinedOutput).toContain(
|
||||||
|
'Successfully ran target lint'
|
||||||
|
);
|
||||||
|
|
||||||
|
await runCLIAsync(`build ${viteApp}`);
|
||||||
|
checkFilesExist(`dist/apps/${viteApp}/index.html`);
|
||||||
|
}, 300_000);
|
||||||
|
|
||||||
|
it('should test and lint app with bundler=vite and compiler=swc', async () => {
|
||||||
|
const viteApp = uniq('viteapp');
|
||||||
|
|
||||||
|
runCLI(
|
||||||
|
`generate @nx/react:app ${viteApp} --bundler=vite --compiler=swc --unitTestRunner=vitest --no-interactive`
|
||||||
);
|
);
|
||||||
|
|
||||||
const appTestResults = await runCLIAsync(`test ${viteApp}`);
|
const appTestResults = await runCLIAsync(`test ${viteApp}`);
|
||||||
|
|||||||
@ -131,6 +131,7 @@ export async function applicationGenerator(
|
|||||||
newProject: true,
|
newProject: true,
|
||||||
includeVitest: options.unitTestRunner === 'vitest',
|
includeVitest: options.unitTestRunner === 'vitest',
|
||||||
inSourceTests: options.inSourceTests,
|
inSourceTests: options.inSourceTests,
|
||||||
|
compiler: options.compiler,
|
||||||
skipFormat: true,
|
skipFormat: true,
|
||||||
});
|
});
|
||||||
tasks.push(viteTask);
|
tasks.push(viteTask);
|
||||||
|
|||||||
@ -73,6 +73,7 @@ export async function libraryGenerator(host: Tree, schema: Schema) {
|
|||||||
includeLib: true,
|
includeLib: true,
|
||||||
inSourceTests: options.inSourceTests,
|
inSourceTests: options.inSourceTests,
|
||||||
includeVitest: options.unitTestRunner === 'vitest',
|
includeVitest: options.unitTestRunner === 'vitest',
|
||||||
|
compiler: options.compiler,
|
||||||
skipFormat: true,
|
skipFormat: true,
|
||||||
});
|
});
|
||||||
tasks.push(viteTask);
|
tasks.push(viteTask);
|
||||||
|
|||||||
@ -174,7 +174,7 @@
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["babel", "swc"],
|
"enum": ["babel", "swc"],
|
||||||
"default": "babel",
|
"default": "babel",
|
||||||
"description": "Which compiler to use. Only applies to `bundler: 'rollup'`."
|
"description": "Which compiler to use."
|
||||||
},
|
},
|
||||||
"skipPackageJson": {
|
"skipPackageJson": {
|
||||||
"description": "Do not add dependencies to `package.json`.",
|
"description": "Do not add dependencies to `package.json`.",
|
||||||
|
|||||||
@ -152,6 +152,7 @@ export async function viteConfigurationGenerator(
|
|||||||
const initTask = await initGenerator(tree, {
|
const initTask = await initGenerator(tree, {
|
||||||
uiFramework: schema.uiFramework,
|
uiFramework: schema.uiFramework,
|
||||||
includeLib: schema.includeLib,
|
includeLib: schema.includeLib,
|
||||||
|
compiler: schema.compiler,
|
||||||
});
|
});
|
||||||
tasks.push(initTask);
|
tasks.push(initTask);
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
export interface ViteConfigurationGeneratorSchema {
|
export interface ViteConfigurationGeneratorSchema {
|
||||||
uiFramework: 'react' | 'none';
|
uiFramework: 'react' | 'none';
|
||||||
|
compiler?: 'babel' | 'swc';
|
||||||
project: string;
|
project: string;
|
||||||
newProject?: boolean;
|
newProject?: boolean;
|
||||||
includeVitest?: boolean;
|
includeVitest?: boolean;
|
||||||
|
|||||||
@ -32,6 +32,12 @@
|
|||||||
"default": "none",
|
"default": "none",
|
||||||
"x-prompt": "What UI framework plugin should Vite use?"
|
"x-prompt": "What UI framework plugin should Vite use?"
|
||||||
},
|
},
|
||||||
|
"compiler": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Compiler to use for Vite when UI Framework is React.",
|
||||||
|
"enum": ["babel", "swc"],
|
||||||
|
"default": "babel"
|
||||||
|
},
|
||||||
"newProject": {
|
"newProject": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"description": "Is this a new project?",
|
"description": "Is this a new project?",
|
||||||
|
|||||||
@ -17,6 +17,7 @@ import {
|
|||||||
vitePluginDtsVersion,
|
vitePluginDtsVersion,
|
||||||
vitePluginEslintVersion,
|
vitePluginEslintVersion,
|
||||||
vitePluginReactVersion,
|
vitePluginReactVersion,
|
||||||
|
vitePluginReactSwcVersion,
|
||||||
vitestUiVersion,
|
vitestUiVersion,
|
||||||
vitestVersion,
|
vitestVersion,
|
||||||
viteTsConfigPathsVersion,
|
viteTsConfigPathsVersion,
|
||||||
@ -41,7 +42,11 @@ function checkDependenciesInstalled(host: Tree, schema: InitGeneratorSchema) {
|
|||||||
devDependencies['jsdom'] = jsdomVersion;
|
devDependencies['jsdom'] = jsdomVersion;
|
||||||
|
|
||||||
if (schema.uiFramework === 'react') {
|
if (schema.uiFramework === 'react') {
|
||||||
devDependencies['@vitejs/plugin-react'] = vitePluginReactVersion;
|
if (schema.compiler === 'swc') {
|
||||||
|
devDependencies['@vitejs/plugin-react-swc'] = vitePluginReactSwcVersion;
|
||||||
|
} else {
|
||||||
|
devDependencies['@vitejs/plugin-react'] = vitePluginReactVersion;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (schema.includeLib) {
|
if (schema.includeLib) {
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
export interface InitGeneratorSchema {
|
export interface InitGeneratorSchema {
|
||||||
uiFramework: 'react' | 'none';
|
uiFramework: 'react' | 'none';
|
||||||
|
compiler?: 'babel' | 'swc';
|
||||||
includeLib?: boolean;
|
includeLib?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,12 @@
|
|||||||
"default": "react",
|
"default": "react",
|
||||||
"x-prompt": "What UI framework plugin should Vite use?"
|
"x-prompt": "What UI framework plugin should Vite use?"
|
||||||
},
|
},
|
||||||
|
"compiler": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Compiler to use for Vite when UI Framework is React.",
|
||||||
|
"enum": ["babel", "swc"],
|
||||||
|
"default": "babel"
|
||||||
|
},
|
||||||
"includeLib": {
|
"includeLib": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"description": "Add dependencies needed to build libraries.",
|
"description": "Add dependencies needed to build libraries.",
|
||||||
|
|||||||
@ -556,7 +556,9 @@ export function createOrEditViteConfig(
|
|||||||
|
|
||||||
const reactPluginImportLine =
|
const reactPluginImportLine =
|
||||||
options.uiFramework === 'react'
|
options.uiFramework === 'react'
|
||||||
? `import react from '@vitejs/plugin-react';`
|
? options.compiler === 'swc'
|
||||||
|
? `import react from '@vitejs/plugin-react-swc';`
|
||||||
|
: `import react from '@vitejs/plugin-react';`
|
||||||
: '';
|
: '';
|
||||||
|
|
||||||
const reactPlugin = options.uiFramework === 'react' ? `react(),` : '';
|
const reactPlugin = options.uiFramework === 'react' ? `react(),` : '';
|
||||||
|
|||||||
@ -4,6 +4,7 @@ export const vitePluginEslintVersion = '^1.8.1';
|
|||||||
export const vitestVersion = '^0.31.0';
|
export const vitestVersion = '^0.31.0';
|
||||||
export const vitestUiVersion = '^0.31.0';
|
export const vitestUiVersion = '^0.31.0';
|
||||||
export const vitePluginReactVersion = '^3.0.0';
|
export const vitePluginReactVersion = '^3.0.0';
|
||||||
|
export const vitePluginReactSwcVersion = '^3.3.1';
|
||||||
export const vitePluginVueVersion = '^3.2.0';
|
export const vitePluginVueVersion = '^3.2.0';
|
||||||
export const vitePluginVueJsxVersion = '^2.1.1';
|
export const vitePluginVueJsxVersion = '^2.1.1';
|
||||||
export const viteTsConfigPathsVersion = '^4.0.2';
|
export const viteTsConfigPathsVersion = '^4.0.2';
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user