Skip to content

Commit

Permalink
feat(rspack): add crystal plugin for inferring projects (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jun 12, 2024
1 parent 87f4ad4 commit 3860a69
Show file tree
Hide file tree
Showing 15 changed files with 717 additions and 347 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ Thumbs.db

# migration file
./migrations.json
.nx/cache
.nx/workspace-data
.nx/cache
2 changes: 1 addition & 1 deletion e2e/rspack-e2e/tests/rspack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('rspack e2e', () => {
// Generate a Nest app and verify build output
const app3 = uniq('app3');
await runNxCommandAsync(
`generate @nx/rspack:app ${app3} --framework=nest --unitTestRunner=jest`
`generate @nx/rspack:app ${app3} --framework=nest --unitTestRunner=jest --no-interactive`
);
checkFilesExist(`${app3}/project.json`);

Expand Down
29 changes: 14 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
},
"packageManager": "[email protected]",
"devDependencies": {
"@nx/cypress": "19.0.4",
"@nx/detox": "19.0.4",
"@nx/devkit": "19.0.4",
"@nx/eslint": "19.0.4",
"@nx/eslint-plugin": "19.0.4",
"@nx/jest": "19.0.4",
"@nx/js": "19.0.4",
"@nx/node": "19.0.4",
"@nx/plugin": "19.0.4",
"@nx/react": "19.0.4",
"@nx/storybook": "19.0.4",
"@nx/vite": "19.0.4",
"@nx/workspace": "19.0.4",
"@nx/cypress": "19.2.3",
"@nx/detox": "19.2.3",
"@nx/devkit": "19.2.3",
"@nx/eslint": "19.2.3",
"@nx/eslint-plugin": "19.2.3",
"@nx/jest": "19.2.3",
"@nx/js": "19.2.3",
"@nx/node": "19.2.3",
"@nx/plugin": "19.2.3",
"@nx/react": "19.2.3",
"@nx/storybook": "19.2.3",
"@nx/vite": "19.2.3",
"@nx/workspace": "19.2.3",
"@phenomnomnominal/tsquery": "^5.0.1",
"@rspack/core": "^0.5.6",
"@rspack/dev-server": "^0.5.6",
Expand All @@ -60,7 +60,7 @@
"kill-port": "^2.0.1",
"license-webpack-plugin": "^4.0.2",
"metro-resolver": "0.71.0",
"nx": "19.0.4",
"nx": "19.2.3",
"prettier": "2.6.2",
"prettier-plugin-organize-imports": "^3.2.1",
"release-it": "15.2.0",
Expand All @@ -73,4 +73,3 @@
"verdaccio": "^5.0.4"
}
}

6 changes: 3 additions & 3 deletions packages/rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"generators": "./generators.json",
"executors": "./executors.json",
"dependencies": {
"@nx/js": "^18.1.3",
"@nx/devkit": "^18.1.3",
"@nx/js": "^19.2.3",
"@nx/devkit": "^19.2.3",
"@phenomnomnominal/tsquery": "~5.0.1",
"less-loader": "11.1.0",
"license-webpack-plugin": "^4.0.2",
"sass-loader": "^12.2.0",
"stylus-loader": "^7.1.0",
"@nx/eslint": "^18.1.3",
"@nx/eslint": "^19.2.3",
"@rspack/core": "^0.6.1",
"@rspack/plugin-react-refresh": "^0.6.1",
"@rspack/plugin-minify": "^0.6.1"
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createDependencies, createNodesV2 } from './src/plugins/plugin';
export type { RspackPluginOptions } from './src/plugins/plugin';
11 changes: 10 additions & 1 deletion packages/rspack/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export default async function (
_options: ApplicationGeneratorSchema
) {
const tasks = [];
const initTask = await rspackInitGenerator(tree, _options);
const initTask = await rspackInitGenerator(tree, {
..._options,
// TODO: Crystalize the default rspack.config.js file.
// The default setup isn't crystalized so don't add plugin.
addPlugin: false,
});
tasks.push(initTask);

const options = normalizeOptions(tree, _options);
Expand All @@ -26,6 +31,7 @@ export default async function (
...options,
skipFormat: true,
tags: options.tags ?? '',
addPlugin: false,
});

const convertAppTask = await configurationGenerator(tree, {
Expand All @@ -52,6 +58,7 @@ export default async function (
e2eTestRunner: options.e2eTestRunner,
rootProject: options.rootProject,
skipFormat: true,
addPlugin: false,
});
const convertAppTask = await configurationGenerator(tree, {
project: options.name,
Expand All @@ -60,6 +67,7 @@ export default async function (
buildTarget: 'build',
serveTarget: 'serve',
framework: 'web',
addPlugin: false,
});
tasks.push(createAppTask, convertAppTask);
} else {
Expand All @@ -78,6 +86,7 @@ export default async function (
e2eTestRunner: options.e2eTestRunner,
rootProject: options.rootProject,
skipFormat: true,
addPlugin: false,
});
const convertAppTask = await configurationGenerator(tree, {
project: options.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export async function configurationGenerator(
tree: Tree,
options: ConfigurationSchema
) {
const task = await rspackInitGenerator(tree, options);
const task = await rspackInitGenerator(tree, {
...options,
// TODO: Crystalize the default rspack.config.js file.
// The default setup isn't crystalized so don't add plugin.
addPlugin: false,
});
const { targets, root, projectType } = readProjectConfiguration(
tree,
options.project
Expand Down
52 changes: 49 additions & 3 deletions packages/rspack/src/generators/init/init.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import {
addDependenciesToPackageJson,
convertNxGenerator,
createProjectGraphAsync,
GeneratorCallback,
readNxJson,
runTasksInSerial,
Tree,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { initGenerator } from '@nx/js';
import { createNodesV2 } from '../../../plugin';
import {
lessLoaderVersion, reactRefreshVersion,
lessLoaderVersion,
reactRefreshVersion,
rspackCoreVersion,
rspackDevServerVersion,
rspackPluginMinifyVersion,
rspackPluginReactRefreshVersion
rspackPluginReactRefreshVersion,
} from '../../utils/versions';
import { InitGeneratorSchema } from './schema';

Expand All @@ -20,6 +25,46 @@ export async function rspackInitGenerator(
schema: InitGeneratorSchema
) {
const tasks: GeneratorCallback[] = [];

const nxJson = readNxJson(tree);
const addPluginDefault =
process.env.NX_ADD_PLUGINS !== 'false' &&
nxJson.useInferencePlugins !== false;
schema.addPlugin ??= addPluginDefault;

if (schema.addPlugin) {
await addPlugin(
tree,
await createProjectGraphAsync(),
'@nx/rspack/plugin',
createNodesV2,
{
buildTargetName: [
'build',
'rspack:build',
'build:rspack',
'rspack-build',
'build-rspack',
],
serveTargetName: [
'serve',
'rspack:serve',
'serve:rspack',
'rspack-serve',
'serve-rspack',
],
previewTargetName: [
'preview',
'rspack:preview',
'preview:rspack',
'rspack-preview',
'preview-rspack',
],
},
schema.updatePackageScripts
);
}

const jsInitTask = await initGenerator(tree, {
...schema,
tsConfigName: schema.rootProject ? 'tsconfig.json' : 'tsconfig.base.json',
Expand All @@ -30,9 +75,10 @@ export async function rspackInitGenerator(

const devDependencies = {
'@rspack/core': rspackCoreVersion,
'@rspack/cli': rspackCoreVersion,
'@rspack/plugin-minify': rspackPluginMinifyVersion,
'@rspack/plugin-react-refresh': rspackPluginReactRefreshVersion,
'react-refresh': reactRefreshVersion
'react-refresh': reactRefreshVersion,
};

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down
8 changes: 5 additions & 3 deletions packages/rspack/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export type Framework = 'none' | 'react' | 'web' | 'nest';

export interface InitGeneratorSchema {
framework?: Framework;
style?: 'none' | 'css' | 'scss' | 'less' | 'styl';
addPlugin?: boolean;
devServer?: boolean;
rootProject?: boolean;
framework?: Framework;
keepExistingVersions?: boolean;
rootProject?: boolean;
style?: 'none' | 'css' | 'scss' | 'less' | 'styl';
updatePackageScripts?: boolean;
}
Loading

0 comments on commit 3860a69

Please sign in to comment.