Skip to content

Commit

Permalink
feat(rsbuild): add inferred targets plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Dec 5, 2024
1 parent 8da5677 commit 68440c3
Show file tree
Hide file tree
Showing 16 changed files with 561 additions and 4 deletions.
20 changes: 19 additions & 1 deletion docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -9889,7 +9889,25 @@
"id": "rsbuild",
"path": "/nx-api/rsbuild",
"name": "rsbuild",
"children": [],
"children": [
{
"id": "generators",
"path": "/nx-api/rsbuild/generators",
"name": "generators",
"children": [
{
"id": "init",
"path": "/nx-api/rsbuild/generators/init",
"name": "init",
"children": [],
"isExternal": false,
"disableCollapsible": false
}
],
"isExternal": false,
"disableCollapsible": false
}
],
"isExternal": false,
"disableCollapsible": false
},
Expand Down
12 changes: 11 additions & 1 deletion docs/generated/manifests/nx-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -2900,7 +2900,17 @@
"root": "/packages/rsbuild",
"source": "/packages/rsbuild/src",
"executors": {},
"generators": {},
"generators": {
"/nx-api/rsbuild/generators/init": {
"description": "Initialize the `@nx/rsbuild` plugin.",
"file": "generated/packages/rsbuild/generators/init.json",
"hidden": true,
"name": "init",
"originalFilePath": "/packages/rsbuild/src/generators/init/schema.json",
"path": "/nx-api/rsbuild/generators/init",
"type": "generator"
}
},
"path": "/nx-api/rsbuild"
},
"rspack": {
Expand Down
12 changes: 11 additions & 1 deletion docs/generated/packages-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2869,7 +2869,17 @@
"description": "The Nx Plugin for Rsbuild contains an Nx plugin, executors and utilities that support building applications using Rsbuild.",
"documents": [],
"executors": [],
"generators": [],
"generators": [
{
"description": "Initialize the `@nx/rsbuild` plugin.",
"file": "generated/packages/rsbuild/generators/init.json",
"hidden": true,
"name": "init",
"originalFilePath": "/packages/rsbuild/src/generators/init/schema.json",
"path": "rsbuild/generators/init",
"type": "generator"
}
],
"githubRoot": "https://github.com/nrwl/nx/blob/master",
"name": "rsbuild",
"packageName": "@nx/rsbuild",
Expand Down
44 changes: 44 additions & 0 deletions docs/generated/packages/rsbuild/generators/init.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "init",
"factory": "./src/generators/init/init#initGeneratorInternal",
"schema": {
"$schema": "http://json-schema.org/schema",
"$id": "Init",
"title": "Nx Rsbuild Init Generator",
"type": "object",
"description": "Rsbuild init generator.",
"properties": {
"rootProject": { "type": "boolean", "x-priority": "internal" },
"keepExistingVersions": {
"type": "boolean",
"x-priority": "internal",
"description": "Keep existing dependencies versions",
"default": false
},
"updatePackageScripts": {
"type": "boolean",
"x-priority": "internal",
"description": "Update package scripts",
"default": false
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
"default": false
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": [],
"presets": []
},
"description": "Initialize the `@nx/rsbuild` plugin.",
"aliases": ["ng-add"],
"hidden": true,
"implementation": "/packages/rsbuild/src/generators/init/init#initGeneratorInternal.ts",
"path": "/packages/rsbuild/src/generators/init/schema.json",
"type": "generator"
}
2 changes: 2 additions & 0 deletions docs/shared/reference/sitemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@
- [configuration](/nx-api/rollup/generators/configuration)
- [convert-to-inferred](/nx-api/rollup/generators/convert-to-inferred)
- [rsbuild](/nx-api/rsbuild)
- [generators](/nx-api/rsbuild/generators)
- [init](/nx-api/rsbuild/generators/init)
- [rspack](/nx-api/rspack)
- [documents](/nx-api/rspack/documents)
- [Overview](/nx-api/rspack/documents/overview)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-url": "^8.0.2",
"@rspack/core": "^1.1.5",
"@rsbuild/core": "1.1.8",
"@rspack/dev-server": "1.0.9",
"@rspack/plugin-minify": "^0.7.5",
"@rspack/plugin-react-refresh": "^1.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/command-line/init/init-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ const npmPackageToPluginMap: Record<string, `@nx/${string}`> = {
nuxt: '@nx/nuxt',
'react-native': '@nx/react-native',
'@remix-run/dev': '@nx/remix',
'@rsbuild/core': '@nx/rsbuild',
};

export async function detectPlugins(
Expand Down
13 changes: 13 additions & 0 deletions packages/rsbuild/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Nx Rsbuild",
"version": "0.1",
"generators": {
"init": {
"factory": "./src/generators/init/init#initGeneratorInternal",
"schema": "./src/generators/init/schema.json",
"description": "Initialize the `@nx/rsbuild` plugin.",
"aliases": ["ng-add"],
"hidden": true
}
}
}
7 changes: 6 additions & 1 deletion packages/rsbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@
"license": "MIT",
"homepage": "https://nx.dev",
"main": "index.js",
"generators": "./generators.json",
"executors": "./executors.json",
"dependencies": {
"tslib": "^2.3.0"
"tslib": "^2.3.0",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
"@rsbuild/core": "1.1.8",
"minimatch": "9.0.3"
},
"peerDependencies": {},
"nx-migrations": {
Expand Down
1 change: 1 addition & 0 deletions packages/rsbuild/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createNodesV2, RsbuildPluginOptions } from './src/plugins/plugin';
75 changes: 75 additions & 0 deletions packages/rsbuild/src/generators/init/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {
type Tree,
type GeneratorCallback,
readNxJson,
createProjectGraphAsync,
addDependenciesToPackageJson,
formatFiles,
runTasksInSerial,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { InitGeneratorSchema } from './schema';
import { createNodesV2 } from '../../plugins/plugin';
import { nxVersion, rsbuildVersion } from '../../utils/versions';

export function updateDependencies(tree: Tree, schema: InitGeneratorSchema) {
return addDependenciesToPackageJson(
tree,
{},
{
'@nx/rsbuild': nxVersion,
'@rsbuild/core': rsbuildVersion,
},
undefined,
schema.keepExistingVersions
);
}

export function initGenerator(tree: Tree, schema: InitGeneratorSchema) {
return initGeneratorInternal(tree, { addPlugin: false, ...schema });
}

export async function initGeneratorInternal(
tree: Tree,
schema: InitGeneratorSchema
) {
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/rsbuild/plugin',
createNodesV2,
{
buildTargetName: ['build', 'rsbuild:build', 'rsbuild-build'],
devTargetName: ['dev', 'rsbuild:dev', 'rsbuild-dev'],
previewTargetName: ['preview', 'rsbuild:preview', 'rsbuild-preview'],
inspectTargetName: ['inspect', 'rsbuild:inspect', 'rsbuild-inspect'],
typecheckTargetName: [
'typecheck',
'rsbuild:typecheck',
'rsbuild-typecheck',
],
},
schema.updatePackageScripts
);
}

const tasks: GeneratorCallback[] = [];
if (!schema.skipPackageJson) {
tasks.push(updateDependencies(tree, schema));
}

if (!schema.skipFormat) {
await formatFiles(tree);
}

return runTasksInSerial(...tasks);
}

export default initGenerator;
7 changes: 7 additions & 0 deletions packages/rsbuild/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface InitGeneratorSchema {
keepExistingVersions?: boolean;
updatePackageScripts?: boolean;
addPlugin?: boolean;
skipFormat?: boolean;
skipPackageJson?: boolean;
}
36 changes: 36 additions & 0 deletions packages/rsbuild/src/generators/init/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "Init",
"title": "Nx Rsbuild Init Generator",
"type": "object",
"description": "Rsbuild init generator.",
"properties": {
"rootProject": {
"type": "boolean",
"x-priority": "internal"
},
"keepExistingVersions": {
"type": "boolean",
"x-priority": "internal",
"description": "Keep existing dependencies versions",
"default": false
},
"updatePackageScripts": {
"type": "boolean",
"x-priority": "internal",
"description": "Update package scripts",
"default": false
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
"default": false
},
"skipPackageJson": {
"description": "Do not add dependencies to `package.json`.",
"type": "boolean",
"default": false
}
},
"required": []
}
Loading

0 comments on commit 68440c3

Please sign in to comment.