feat(core): react standalone bundler prompt (#14464)

This commit is contained in:
Katerina Skroumpelou 2023-01-20 15:18:32 +02:00 committed by GitHub
parent b076f0380a
commit dbe2c3b1e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 165 additions and 3 deletions

View File

@ -31,6 +31,12 @@ Type: `string`
The name of the application when a preset with pregenerated app is selected The name of the application when a preset with pregenerated app is selected
### bundler
Type: `string`
Bundler to be used to build the application
### ci ### ci
Type: `string` Type: `string`

View File

@ -31,6 +31,12 @@ Type: `string`
The name of the application when a preset with pregenerated app is selected The name of the application when a preset with pregenerated app is selected
### bundler
Type: `string`
Bundler to be used to build the application
### ci ### ci
Type: `string` Type: `string`

View File

@ -62,6 +62,12 @@
"description": "The framework which the application is using", "description": "The framework which the application is using",
"type": "string", "type": "string",
"enum": ["express", "koa", "fastify", "connect"] "enum": ["express", "koa", "fastify", "connect"]
},
"bundler": {
"description": "The bundler to use for building the application.",
"type": "string",
"enum": ["webpack", "vite"],
"default": "vite"
} }
}, },
"presets": [] "presets": []

View File

@ -160,6 +160,7 @@ export function runCreateWorkspace(
ci, ci,
useDetectedPm = false, useDetectedPm = false,
cwd = e2eCwd, cwd = e2eCwd,
bundler,
}: { }: {
preset: string; preset: string;
appName?: string; appName?: string;
@ -171,6 +172,7 @@ export function runCreateWorkspace(
ci?: 'azure' | 'github' | 'circleci'; ci?: 'azure' | 'github' | 'circleci';
useDetectedPm?: boolean; useDetectedPm?: boolean;
cwd?: string; cwd?: string;
bundler?: 'webpack' | 'vite';
} }
) { ) {
projName = name; projName = name;
@ -190,6 +192,10 @@ export function runCreateWorkspace(
command += ` --ci=${ci}`; command += ` --ci=${ci}`;
} }
if (bundler) {
command += ` --bundler=${bundler}`;
}
if (base) { if (base) {
command += ` --defaultBase="${base}"`; command += ` --defaultBase="${base}"`;
} }

View File

@ -39,6 +39,7 @@ describe('create-nx-workspace', () => {
appName: wsName, appName: wsName,
style: 'css', style: 'css',
packageManager, packageManager,
bundler: 'vite',
}); });
checkFilesExist('package.json'); checkFilesExist('package.json');

View File

@ -43,6 +43,7 @@ type Arguments = {
name: string; name: string;
email: string; email: string;
}; };
bundler: 'vite' | 'webpack';
}; };
enum Preset { enum Preset {
@ -152,6 +153,10 @@ export const commandsObject: yargs.Argv<Arguments> = yargs
describe: chalk.dim`Style option to be used when a preset with pregenerated app is selected`, describe: chalk.dim`Style option to be used when a preset with pregenerated app is selected`,
type: 'string', type: 'string',
}) })
.option('bundler', {
describe: chalk.dim`Bundler to be used to build the application`,
type: 'string',
})
.option('framework', { .option('framework', {
describe: chalk.dim`Framework option to be used when the node-server preset is selected`, describe: chalk.dim`Framework option to be used when the node-server preset is selected`,
type: 'string', type: 'string',
@ -313,7 +318,7 @@ async function getConfiguration(
argv: yargs.Arguments<Arguments> argv: yargs.Arguments<Arguments>
): Promise<void> { ): Promise<void> {
try { try {
let name, appName, style, preset, framework; let name, appName, style, preset, framework, bundler;
output.log({ output.log({
title: title:
@ -360,6 +365,10 @@ async function getConfiguration(
if (preset === Preset.NodeServer) { if (preset === Preset.NodeServer) {
framework = await determineFramework(preset, argv); framework = await determineFramework(preset, argv);
} }
if (preset === Preset.ReactStandalone) {
bundler = await determineBundler(argv);
}
} else { } else {
name = await determineRepoName(argv); name = await determineRepoName(argv);
appName = await determineAppName(preset, argv); appName = await determineAppName(preset, argv);
@ -384,6 +393,7 @@ async function getConfiguration(
packageManager, packageManager,
defaultBase, defaultBase,
ci, ci,
bundler,
}); });
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@ -836,6 +846,54 @@ async function determineStyle(
return Promise.resolve(parsedArgs.style); return Promise.resolve(parsedArgs.style);
} }
async function determineBundler(
parsedArgs: yargs.Arguments<Arguments>
): Promise<'vite' | 'webpack'> {
const choices = [
{
name: 'vite',
message: 'Vite [ https://vitejs.dev/ ]',
},
{
name: 'webpack',
message: 'Webpack [ https://webpack.js.org/ ]',
},
];
if (!parsedArgs.bundler) {
return enquirer
.prompt([
{
name: 'bundler',
message: `Bundler to be used to build the application`,
initial: 'vite' as any,
type: 'autocomplete',
choices: choices,
},
])
.then((a: { bundler: 'vite' | 'webpack' }) => a.bundler);
}
const foundBundler = choices.find(
(choice) => choice.name === parsedArgs.bundler
);
if (foundBundler === undefined) {
output.error({
title: 'Invalid bundler',
bodyLines: [
`It must be one of the following:`,
'',
...choices.map((choice) => choice.name),
],
});
process.exit(1);
}
return Promise.resolve(parsedArgs.bundler);
}
async function determineNxCloud( async function determineNxCloud(
parsedArgs: yargs.Arguments<Arguments> parsedArgs: yargs.Arguments<Arguments>
): Promise<boolean> { ): Promise<boolean> {

View File

@ -34,3 +34,43 @@ Array [
"nx-welcome.component.ts", "nx-welcome.component.ts",
] ]
`; `;
exports[`preset should create files (preset = react-standalone bundler = vite) 1`] = `
Object {
"configurations": Object {
"development": Object {
"buildTarget": "proj:build:development",
"hmr": true,
},
"production": Object {
"buildTarget": "proj:build:production",
"hmr": false,
},
},
"defaultConfiguration": "development",
"executor": "@nrwl/vite:dev-server",
"options": Object {
"buildTarget": "proj:build",
},
}
`;
exports[`preset should create files (preset = react-standalone bundler = webpack) 1`] = `
Object {
"configurations": Object {
"development": Object {
"buildTarget": "proj:build:development",
},
"production": Object {
"buildTarget": "proj:build:production",
"hmr": false,
},
},
"defaultConfiguration": "development",
"executor": "@nrwl/webpack:dev-server",
"options": Object {
"buildTarget": "proj:build",
"hmr": true,
},
}
`;

View File

@ -1,4 +1,4 @@
import { Tree, readJson, readProjectConfiguration } from '@nrwl/devkit'; import { Tree, readProjectConfiguration } from '@nrwl/devkit';
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing'; import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import { overrideCollectionResolutionForTesting } from '@nrwl/devkit/ngcli-adapter'; import { overrideCollectionResolutionForTesting } from '@nrwl/devkit/ngcli-adapter';
import { presetGenerator } from './preset'; import { presetGenerator } from './preset';
@ -114,4 +114,36 @@ describe('preset', () => {
expect(tree.exists('/apps/proj/src/app/App.tsx')).toBe(true); expect(tree.exists('/apps/proj/src/app/App.tsx')).toBe(true);
}); });
it(`should create files (preset = ${Preset.ReactStandalone} bundler = webpack)`, async () => {
await presetGenerator(tree, {
name: 'proj',
preset: Preset.ReactStandalone,
style: 'css',
linter: 'eslint',
cli: 'nx',
standaloneConfig: false,
bundler: 'webpack',
});
expect(tree.exists('webpack.config.js')).toBe(true);
expect(
readProjectConfiguration(tree, 'proj').targets.serve
).toMatchSnapshot();
});
it(`should create files (preset = ${Preset.ReactStandalone} bundler = vite)`, async () => {
await presetGenerator(tree, {
name: 'proj',
preset: Preset.ReactStandalone,
style: 'css',
linter: 'eslint',
cli: 'nx',
standaloneConfig: false,
bundler: 'vite',
});
expect(tree.exists('vite.config.ts')).toBe(true);
expect(
readProjectConfiguration(tree, 'proj').targets.serve
).toMatchSnapshot();
});
}); });

View File

@ -68,7 +68,7 @@ async function createPreset(tree: Tree, options: Schema) {
linter: options.linter, linter: options.linter,
standaloneConfig: options.standaloneConfig, standaloneConfig: options.standaloneConfig,
rootProject: true, rootProject: true,
bundler: 'vite', bundler: options.bundler ?? 'vite',
e2eTestRunner: 'cypress', e2eTestRunner: 'cypress',
unitTestRunner: 'vitest', unitTestRunner: 'vitest',
}); });

View File

@ -11,4 +11,5 @@ export interface Schema {
standaloneConfig?: boolean; standaloneConfig?: boolean;
framework?: string; framework?: string;
packageManager?: PackageManager; packageManager?: PackageManager;
bundler?: 'vite' | 'webpack';
} }

View File

@ -68,6 +68,12 @@
"description": "The framework which the application is using", "description": "The framework which the application is using",
"type": "string", "type": "string",
"enum": ["express", "koa", "fastify", "connect"] "enum": ["express", "koa", "fastify", "connect"]
},
"bundler": {
"description": "The bundler to use for building the application.",
"type": "string",
"enum": ["webpack", "vite"],
"default": "vite"
} }
} }
} }