Skip to content

Commit

Permalink
feat(core): react standalone bundler prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Jan 18, 2023
1 parent 93ac55f commit 6989dde
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/generated/cli/create-nx-workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Type: `string`

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

Type: `string`
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/nx/documents/create-nx-workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Type: `string`

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

Type: `string`
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/workspace/generators/preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
"description": "The framework which the application is using",
"type": "string",
"enum": ["express", "koa", "fastify", "connect"]
},
"bundler": {
"description": "The bundler to use for building the application.",
"type": "string",
"enum": ["webpack", "vite"],
"default": "vite"
}
},
"presets": []
Expand Down
60 changes: 59 additions & 1 deletion packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Arguments = {
name: string;
email: string;
};
bundler: 'vite' | 'webpack';
};

enum Preset {
Expand Down Expand Up @@ -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`,
type: 'string',
})
.option('bundler', {
describe: chalk.dim`Bundler to be used to build the application`,
type: 'string',
})
.option('framework', {
describe: chalk.dim`Framework option to be used when the node-server preset is selected`,
type: 'string',
Expand Down Expand Up @@ -313,7 +318,7 @@ async function getConfiguration(
argv: yargs.Arguments<Arguments>
): Promise<void> {
try {
let name, appName, style, preset, framework;
let name, appName, style, preset, framework, bundler;

output.log({
title:
Expand Down Expand Up @@ -360,6 +365,10 @@ async function getConfiguration(
if (preset === Preset.NodeServer) {
framework = await determineFramework(preset, argv);
}

if (preset === Preset.ReactStandalone) {
bundler = await determineBundler(argv);
}
} else {
name = await determineRepoName(argv);
appName = await determineAppName(preset, argv);
Expand All @@ -384,6 +393,7 @@ async function getConfiguration(
packageManager,
defaultBase,
ci,
bundler,
});
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -836,6 +846,54 @@ async function determineStyle(
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(
parsedArgs: yargs.Arguments<Arguments>
): Promise<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,43 @@ Array [
"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,
},
}
`;
34 changes: 33 additions & 1 deletion packages/workspace/src/generators/preset/preset.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tree, readJson, readProjectConfiguration } from '@nrwl/devkit';
import { Tree, readProjectConfiguration } from '@nrwl/devkit';
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import { overrideCollectionResolutionForTesting } from '@nrwl/devkit/ngcli-adapter';
import { presetGenerator } from './preset';
Expand Down Expand Up @@ -114,4 +114,36 @@ describe('preset', () => {

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();
});
});
2 changes: 1 addition & 1 deletion packages/workspace/src/generators/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function createPreset(tree: Tree, options: Schema) {
linter: options.linter,
standaloneConfig: options.standaloneConfig,
rootProject: true,
bundler: 'vite',
bundler: options.bundler ?? 'vite',
e2eTestRunner: 'cypress',
unitTestRunner: 'vitest',
});
Expand Down
1 change: 1 addition & 0 deletions packages/workspace/src/generators/preset/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface Schema {
standaloneConfig?: boolean;
framework?: string;
packageManager?: PackageManager;
bundler?: 'vite' | 'webpack';
}
6 changes: 6 additions & 0 deletions packages/workspace/src/generators/preset/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
"description": "The framework which the application is using",
"type": "string",
"enum": ["express", "koa", "fastify", "connect"]
},
"bundler": {
"description": "The bundler to use for building the application.",
"type": "string",
"enum": ["webpack", "vite"],
"default": "vite"
}
}
}

0 comments on commit 6989dde

Please sign in to comment.