@nx/rspack 20.0.7 → 20.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/generators.json +5 -0
- package/package.json +4 -4
- package/src/executors/rspack/rspack.impl.js +1 -0
- package/src/executors/rspack/schema.d.ts +4 -4
- package/src/executors/rspack/schema.json +1 -1
- package/src/generators/convert-webpack/convert-webpack.d.ts +3 -0
- package/src/generators/convert-webpack/convert-webpack.js +88 -0
- package/src/generators/convert-webpack/lib/transform-cjs.d.ts +2 -0
- package/src/generators/convert-webpack/lib/transform-cjs.js +169 -0
- package/src/generators/convert-webpack/lib/transform-esm.d.ts +2 -0
- package/src/generators/convert-webpack/lib/transform-esm.js +169 -0
- package/src/generators/convert-webpack/schema.d.ts +4 -0
- package/src/generators/convert-webpack/schema.json +26 -0
- package/src/utils/create-compiler.js +9 -0
- package/src/utils/versions.d.ts +1 -0
- package/src/utils/versions.js +2 -1
- package/src/utils/with-web.js +21 -0
package/generators.json
CHANGED
|
@@ -26,6 +26,11 @@
|
|
|
26
26
|
"aliases": ["app"],
|
|
27
27
|
"x-type": "application",
|
|
28
28
|
"description": "React application generator."
|
|
29
|
+
},
|
|
30
|
+
"convert-webpack": {
|
|
31
|
+
"factory": "./src/generators/convert-webpack/convert-webpack",
|
|
32
|
+
"schema": "./src/generators/convert-webpack/schema.json",
|
|
33
|
+
"description": "Convert a webpack application to use rspack."
|
|
29
34
|
}
|
|
30
35
|
}
|
|
31
36
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nx/rspack",
|
|
3
3
|
"description": "The Nx Plugin for Rspack contains executors and generators that support building applications using Rspack.",
|
|
4
|
-
"version": "20.0.
|
|
4
|
+
"version": "20.1.0-beta.0",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"generators": "./generators.json",
|
|
25
25
|
"executors": "./executors.json",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@nx/js": "20.0.
|
|
28
|
-
"@nx/devkit": "20.0.
|
|
29
|
-
"@nx/web": "20.0.
|
|
27
|
+
"@nx/js": "20.1.0-beta.0",
|
|
28
|
+
"@nx/devkit": "20.1.0-beta.0",
|
|
29
|
+
"@nx/web": "20.1.0-beta.0",
|
|
30
30
|
"@phenomnomnominal/tsquery": "~5.0.1",
|
|
31
31
|
"enquirer": "~2.3.6",
|
|
32
32
|
"express": "^4.19.2",
|
|
@@ -11,6 +11,7 @@ const create_compiler_1 = require("../../utils/create-compiler");
|
|
|
11
11
|
const mode_utils_1 = require("../../utils/mode-utils");
|
|
12
12
|
async function* runExecutor(options, context) {
|
|
13
13
|
process.env.NODE_ENV ??= options.mode ?? 'production';
|
|
14
|
+
options.target ??= 'web';
|
|
14
15
|
if ((0, mode_utils_1.isMode)(process.env.NODE_ENV)) {
|
|
15
16
|
options.mode = process.env.NODE_ENV;
|
|
16
17
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Mode } from '@rspack/core';
|
|
2
2
|
|
|
3
3
|
export interface RspackExecutorSchema {
|
|
4
|
-
target
|
|
5
|
-
main
|
|
4
|
+
target?: 'web' | 'node';
|
|
5
|
+
main?: string;
|
|
6
6
|
index?: string;
|
|
7
|
-
tsConfig
|
|
7
|
+
tsConfig?: string;
|
|
8
8
|
typeCheck?: boolean;
|
|
9
|
-
outputPath
|
|
9
|
+
outputPath?: string;
|
|
10
10
|
outputFileName?: string;
|
|
11
11
|
indexHtml?: string;
|
|
12
12
|
mode?: Mode;
|
|
@@ -133,7 +133,7 @@
|
|
|
133
133
|
"description": "Generates a `package.json` and pruned lock file with the project's `node_module` dependencies populated for installing in a container. If a `package.json` exists in the project's directory, it will be reused with dependencies populated."
|
|
134
134
|
}
|
|
135
135
|
},
|
|
136
|
-
"required": ["
|
|
136
|
+
"required": ["rspackConfig"],
|
|
137
137
|
"definitions": {
|
|
138
138
|
"assetPattern": {
|
|
139
139
|
"oneOf": [
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = default_1;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
const versions_1 = require("../../utils/versions");
|
|
6
|
+
const transform_esm_1 = require("./lib/transform-esm");
|
|
7
|
+
const transform_cjs_1 = require("./lib/transform-cjs");
|
|
8
|
+
async function default_1(tree, options) {
|
|
9
|
+
const projects = (0, devkit_1.getProjects)(tree);
|
|
10
|
+
if (!projects.has(options.project)) {
|
|
11
|
+
throw new Error(`Could not find project '${options.project}'. Ensure you have specified the project you'd like to convert correctly.`);
|
|
12
|
+
}
|
|
13
|
+
const project = projects.get(options.project);
|
|
14
|
+
const webpackConfigsToConvert = [];
|
|
15
|
+
for (const [targetName, target] of Object.entries(project.targets)) {
|
|
16
|
+
if (target.executor === '@nx/webpack:webpack') {
|
|
17
|
+
target.executor = '@nx/rspack:rspack';
|
|
18
|
+
if (!target.options.target) {
|
|
19
|
+
target.options.target = 'web';
|
|
20
|
+
}
|
|
21
|
+
const convertWebpackConfigOption = (options) => {
|
|
22
|
+
if (!options.webpackConfig) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const rspackConfigPath = options.webpackConfig.replace(/webpack(?!.*webpack)/, 'rspack');
|
|
26
|
+
webpackConfigsToConvert.push([options.webpackConfig, rspackConfigPath]);
|
|
27
|
+
options.rspackConfig = rspackConfigPath;
|
|
28
|
+
delete options.webpackConfig;
|
|
29
|
+
};
|
|
30
|
+
if (target.options.webpackConfig) {
|
|
31
|
+
convertWebpackConfigOption(target.options);
|
|
32
|
+
}
|
|
33
|
+
if (target.configurations) {
|
|
34
|
+
for (const [configurationName, configuration] of Object.entries(target.configurations)) {
|
|
35
|
+
convertWebpackConfigOption(configuration);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else if (target.executor === '@nx/webpack:dev-server') {
|
|
40
|
+
target.executor = '@nx/rspack:dev-server';
|
|
41
|
+
}
|
|
42
|
+
else if (target.executor === '@nx/webpack:ssr-dev-server') {
|
|
43
|
+
target.executor = '@nx/rspack:dev-server';
|
|
44
|
+
}
|
|
45
|
+
else if (target.executor === '@nx/react:module-federation-dev-server') {
|
|
46
|
+
target.executor = '@nx/rspack:module-federation-dev-server';
|
|
47
|
+
}
|
|
48
|
+
else if (target.executor === '@nx/react:module-federation-ssr-dev-server') {
|
|
49
|
+
target.executor = '@nx/rspack:module-federation-ssr-dev-server';
|
|
50
|
+
}
|
|
51
|
+
else if (target.executor === '@nx/react:module-federation-static-server') {
|
|
52
|
+
target.executor = '@nx/rspack:module-federation-static-server';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const [webpackConfigPath, rspackConfigPath] of webpackConfigsToConvert) {
|
|
56
|
+
tree.rename(webpackConfigPath, rspackConfigPath);
|
|
57
|
+
transformConfigFile(tree, rspackConfigPath);
|
|
58
|
+
}
|
|
59
|
+
(0, devkit_1.updateProjectConfiguration)(tree, options.project, project);
|
|
60
|
+
const installTask = (0, devkit_1.addDependenciesToPackageJson)(tree, {}, {
|
|
61
|
+
'@rspack/core': versions_1.rspackCoreVersion,
|
|
62
|
+
'@rspack/dev-server': versions_1.rspackDevServerVersion,
|
|
63
|
+
});
|
|
64
|
+
if (!options.skipFormat) {
|
|
65
|
+
await (0, devkit_1.formatFiles)(tree);
|
|
66
|
+
}
|
|
67
|
+
return installTask;
|
|
68
|
+
}
|
|
69
|
+
function transformConfigFile(tree, configPath) {
|
|
70
|
+
(0, transform_esm_1.transformEsmConfigFile)(tree, configPath);
|
|
71
|
+
(0, transform_cjs_1.transformCjsConfigFile)(tree, configPath);
|
|
72
|
+
cleanupEmptyImports(tree, configPath);
|
|
73
|
+
replaceOfRequireOfLocalWebpackConfig(tree, configPath);
|
|
74
|
+
}
|
|
75
|
+
function replaceOfRequireOfLocalWebpackConfig(tree, configPath) {
|
|
76
|
+
const requireOfLocalWebpackConfig = /(?<=require\s*\(\s*['"][^'"]*)(webpack)(?!.*webpack)(?=[^'"]*['"]\s*\))/g;
|
|
77
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
78
|
+
const newContents = configContents.replace(requireOfLocalWebpackConfig, 'rspack');
|
|
79
|
+
tree.write(configPath, newContents);
|
|
80
|
+
}
|
|
81
|
+
function cleanupEmptyImports(tree, configPath) {
|
|
82
|
+
const emptyImportRegex = /import\s*\{\s*\}\s*from\s*['"][^'"]+['"];/g;
|
|
83
|
+
const emptyConstRequires = /(const|let)\s*\{\s*\}\s*=\s*require\s*\(\s*['"][^'"]+['"]\s*\);/g;
|
|
84
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
85
|
+
let newContents = configContents.replace(emptyImportRegex, '');
|
|
86
|
+
newContents = newContents.replace(emptyConstRequires, '');
|
|
87
|
+
tree.write(configPath, newContents);
|
|
88
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformCjsConfigFile = transformCjsConfigFile;
|
|
4
|
+
const tsquery_1 = require("@phenomnomnominal/tsquery");
|
|
5
|
+
function transformCjsConfigFile(tree, configPath) {
|
|
6
|
+
['@nx', '@nrwl'].forEach((scope) => {
|
|
7
|
+
transformComposePlugins(tree, configPath, scope);
|
|
8
|
+
transformWithNx(tree, configPath, scope);
|
|
9
|
+
transformWithWeb(tree, configPath, scope);
|
|
10
|
+
transformWithReact(tree, configPath, scope);
|
|
11
|
+
transformModuleFederationConfig(tree, configPath, scope);
|
|
12
|
+
transformWithModuleFederation(tree, configPath, scope);
|
|
13
|
+
transformWithModuleFederationSSR(tree, configPath, scope);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function transformComposePlugins(tree, configPath, scope) {
|
|
17
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
18
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
19
|
+
const HAS_COMPOSE_PLUGINS_FROM_NX_WEBPACK = `VariableDeclaration:has(Identifier[name=composePlugins]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/webpack]`;
|
|
20
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_COMPOSE_PLUGINS_FROM_NX_WEBPACK);
|
|
21
|
+
if (nodes.length === 0) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const COMPOSE_PLUGINS_IMPORT = 'VariableDeclaration:has(Identifier[name=composePlugins]) Identifier[name=composePlugins]';
|
|
25
|
+
const composePluginsNodes = (0, tsquery_1.tsquery)(ast, COMPOSE_PLUGINS_IMPORT);
|
|
26
|
+
if (nodes.length === 0) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const startIndex = composePluginsNodes[0].getStart();
|
|
30
|
+
let endIndex = composePluginsNodes[0].getEnd();
|
|
31
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
32
|
+
endIndex++;
|
|
33
|
+
}
|
|
34
|
+
const newContents = `const { composePlugins } = require('@nx/rspack');
|
|
35
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
36
|
+
tree.write(configPath, newContents);
|
|
37
|
+
}
|
|
38
|
+
function transformWithNx(tree, configPath, scope) {
|
|
39
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
40
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
41
|
+
const HAS_WITH_NX_FROM_NX_WEBPACK = `VariableDeclaration:has(Identifier[name=withNx]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/webpack]`;
|
|
42
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_NX_FROM_NX_WEBPACK);
|
|
43
|
+
if (nodes.length === 0) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const WITH_NX_IMPORT = 'VariableDeclaration:has(Identifier[name=withNx]) Identifier[name=withNx]';
|
|
47
|
+
const withNxNodes = (0, tsquery_1.tsquery)(ast, WITH_NX_IMPORT);
|
|
48
|
+
if (nodes.length === 0) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const startIndex = withNxNodes[0].getStart();
|
|
52
|
+
let endIndex = withNxNodes[0].getEnd();
|
|
53
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
54
|
+
endIndex++;
|
|
55
|
+
}
|
|
56
|
+
const newContents = `const { withNx } = require('@nx/rspack');
|
|
57
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
58
|
+
tree.write(configPath, newContents);
|
|
59
|
+
}
|
|
60
|
+
function transformWithWeb(tree, configPath, scope) {
|
|
61
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
62
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
63
|
+
const HAS_WITH_WEB_FROM_NX_WEBPACK = `VariableDeclaration:has(Identifier[name=withWeb]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/webpack]`;
|
|
64
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_WEB_FROM_NX_WEBPACK);
|
|
65
|
+
if (nodes.length === 0) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const WITH_WEB_IMPORT = 'VariableDeclaration:has(Identifier[name=withWeb]) Identifier[name=withWeb]';
|
|
69
|
+
const withWebNodes = (0, tsquery_1.tsquery)(ast, WITH_WEB_IMPORT);
|
|
70
|
+
if (nodes.length === 0) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const startIndex = withWebNodes[0].getStart();
|
|
74
|
+
let endIndex = withWebNodes[0].getEnd();
|
|
75
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
76
|
+
endIndex++;
|
|
77
|
+
}
|
|
78
|
+
const newContents = `const { withWeb } = require('@nx/rspack');
|
|
79
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
80
|
+
tree.write(configPath, newContents);
|
|
81
|
+
}
|
|
82
|
+
function transformWithReact(tree, configPath, scope) {
|
|
83
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
84
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
85
|
+
const HAS_WITH_REACT_FROM_NX_REACT = `VariableDeclaration:has(Identifier[name=withReact]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/react]`;
|
|
86
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_REACT_FROM_NX_REACT);
|
|
87
|
+
if (nodes.length === 0) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const WITH_REACT_IMPORT = 'VariableDeclaration:has(Identifier[name=withReact]) Identifier[name=withReact]';
|
|
91
|
+
const withReactNodes = (0, tsquery_1.tsquery)(ast, WITH_REACT_IMPORT);
|
|
92
|
+
if (nodes.length === 0) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const startIndex = withReactNodes[0].getStart();
|
|
96
|
+
let endIndex = withReactNodes[0].getEnd();
|
|
97
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
98
|
+
endIndex++;
|
|
99
|
+
}
|
|
100
|
+
const newContents = `const { withReact } = require('@nx/rspack');
|
|
101
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
102
|
+
tree.write(configPath, newContents);
|
|
103
|
+
}
|
|
104
|
+
function transformModuleFederationConfig(tree, configPath, scope) {
|
|
105
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
106
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
107
|
+
const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `VariableDeclaration:has(Identifier[name=ModuleFederationConfig]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/webpack]`;
|
|
108
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
|
|
109
|
+
if (nodes.length === 0) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const WITH_MODULE_FEDERATION_IMPORT = 'VariableDeclaration:has(Identifier[name=ModuleFederationConfig]) Identifier[name=ModuleFederationConfig]';
|
|
113
|
+
const withModuleFederationNodes = (0, tsquery_1.tsquery)(ast, WITH_MODULE_FEDERATION_IMPORT);
|
|
114
|
+
if (nodes.length === 0) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const startIndex = withModuleFederationNodes[0].getStart();
|
|
118
|
+
let endIndex = withModuleFederationNodes[0].getEnd();
|
|
119
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
120
|
+
endIndex++;
|
|
121
|
+
}
|
|
122
|
+
const newContents = `const { ModuleFederationConfig } = require('@nx/rspack/module-federation');
|
|
123
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
124
|
+
tree.write(configPath, newContents);
|
|
125
|
+
}
|
|
126
|
+
function transformWithModuleFederation(tree, configPath, scope) {
|
|
127
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
128
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
129
|
+
const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `VariableDeclaration:has(Identifier[name=withModuleFederation]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/react/module-federation]`;
|
|
130
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
|
|
131
|
+
if (nodes.length === 0) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const WITH_MODULE_FEDERATION_IMPORT = 'VariableDeclaration:has(Identifier[name=withModuleFederation]) Identifier[name=withModuleFederation]';
|
|
135
|
+
const withModuleFederationNodes = (0, tsquery_1.tsquery)(ast, WITH_MODULE_FEDERATION_IMPORT);
|
|
136
|
+
if (nodes.length === 0) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const startIndex = withModuleFederationNodes[0].getStart();
|
|
140
|
+
let endIndex = withModuleFederationNodes[0].getEnd();
|
|
141
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
142
|
+
endIndex++;
|
|
143
|
+
}
|
|
144
|
+
const newContents = `const { withModuleFederation } = require('@nx/rspack/module-federation');
|
|
145
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
146
|
+
tree.write(configPath, newContents);
|
|
147
|
+
}
|
|
148
|
+
function transformWithModuleFederationSSR(tree, configPath, scope) {
|
|
149
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
150
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
151
|
+
const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `VariableDeclaration:has(Identifier[name=withModuleFederationForSSR]) > CallExpression:has(Identifier[name=require]) StringLiteral[value=${scope}/react/module-federation]`;
|
|
152
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
|
|
153
|
+
if (nodes.length === 0) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const WITH_MODULE_FEDERATION_IMPORT = 'VariableDeclaration:has(Identifier[name=withModuleFederationForSSR]) Identifier[name=withModuleFederationForSSR]';
|
|
157
|
+
const withModuleFederationNodes = (0, tsquery_1.tsquery)(ast, WITH_MODULE_FEDERATION_IMPORT);
|
|
158
|
+
if (nodes.length === 0) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const startIndex = withModuleFederationNodes[0].getStart();
|
|
162
|
+
let endIndex = withModuleFederationNodes[0].getEnd();
|
|
163
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
164
|
+
endIndex++;
|
|
165
|
+
}
|
|
166
|
+
const newContents = `const { withModuleFederationForSSR } = require('@nx/rspack/module-federation');
|
|
167
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
168
|
+
tree.write(configPath, newContents);
|
|
169
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformEsmConfigFile = transformEsmConfigFile;
|
|
4
|
+
const tsquery_1 = require("@phenomnomnominal/tsquery");
|
|
5
|
+
function transformEsmConfigFile(tree, configPath) {
|
|
6
|
+
['@nx', '@nrwl'].forEach((scope) => {
|
|
7
|
+
transformComposePlugins(tree, configPath, scope);
|
|
8
|
+
transformWithNx(tree, configPath, scope);
|
|
9
|
+
transformWithWeb(tree, configPath, scope);
|
|
10
|
+
transformWithReact(tree, configPath, scope);
|
|
11
|
+
transformModuleFederationConfig(tree, configPath, scope);
|
|
12
|
+
transformWithModuleFederation(tree, configPath, scope);
|
|
13
|
+
transformWithModuleFederationSSR(tree, configPath, scope);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function transformComposePlugins(tree, configPath, scope) {
|
|
17
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
18
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
19
|
+
const HAS_COMPOSE_PLUGINS_FROM_NX_WEBPACK = `ImportDeclaration:has(Identifier[name=composePlugins]) > StringLiteral[value=${scope}/webpack]`;
|
|
20
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_COMPOSE_PLUGINS_FROM_NX_WEBPACK);
|
|
21
|
+
if (nodes.length === 0) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const COMPOSE_PLUGINS_IMPORT = 'ImportDeclaration:has(Identifier[name=composePlugins]) Identifier[name=composePlugins]';
|
|
25
|
+
const composePluginsNodes = (0, tsquery_1.tsquery)(ast, COMPOSE_PLUGINS_IMPORT);
|
|
26
|
+
if (nodes.length === 0) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const startIndex = composePluginsNodes[0].getStart();
|
|
30
|
+
let endIndex = composePluginsNodes[0].getEnd();
|
|
31
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
32
|
+
endIndex++;
|
|
33
|
+
}
|
|
34
|
+
const newContents = `import { composePlugins } from '@nx/rspack';
|
|
35
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
36
|
+
tree.write(configPath, newContents);
|
|
37
|
+
}
|
|
38
|
+
function transformWithNx(tree, configPath, scope) {
|
|
39
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
40
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
41
|
+
const HAS_WITH_NX_FROM_NX_WEBPACK = `ImportDeclaration:has(Identifier[name=withNx]) > StringLiteral[value=${scope}/webpack]`;
|
|
42
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_NX_FROM_NX_WEBPACK);
|
|
43
|
+
if (nodes.length === 0) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const WITH_NX_IMPORT = 'ImportDeclaration:has(Identifier[name=withNx]) Identifier[name=withNx]';
|
|
47
|
+
const withNxNodes = (0, tsquery_1.tsquery)(ast, WITH_NX_IMPORT);
|
|
48
|
+
if (nodes.length === 0) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const startIndex = withNxNodes[0].getStart();
|
|
52
|
+
let endIndex = withNxNodes[0].getEnd();
|
|
53
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
54
|
+
endIndex++;
|
|
55
|
+
}
|
|
56
|
+
const newContents = `import { withNx } from '@nx/rspack';
|
|
57
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
58
|
+
tree.write(configPath, newContents);
|
|
59
|
+
}
|
|
60
|
+
function transformWithWeb(tree, configPath, scope) {
|
|
61
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
62
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
63
|
+
const HAS_WITH_WEB_FROM_NX_WEBPACK = `ImportDeclaration:has(Identifier[name=withWeb]) > StringLiteral[value=${scope}/webpack]`;
|
|
64
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_WEB_FROM_NX_WEBPACK);
|
|
65
|
+
if (nodes.length === 0) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const WITH_WEB_IMPORT = 'ImportDeclaration:has(Identifier[name=withWeb]) Identifier[name=withWeb]';
|
|
69
|
+
const withWebNodes = (0, tsquery_1.tsquery)(ast, WITH_WEB_IMPORT);
|
|
70
|
+
if (nodes.length === 0) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const startIndex = withWebNodes[0].getStart();
|
|
74
|
+
let endIndex = withWebNodes[0].getEnd();
|
|
75
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
76
|
+
endIndex++;
|
|
77
|
+
}
|
|
78
|
+
const newContents = `import { withWeb } from '@nx/rspack';
|
|
79
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
80
|
+
tree.write(configPath, newContents);
|
|
81
|
+
}
|
|
82
|
+
function transformWithReact(tree, configPath, scope) {
|
|
83
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
84
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
85
|
+
const HAS_WITH_REACT_FROM_NX_REACT = `ImportDeclaration:has(Identifier[name=withReact]) > StringLiteral[value=${scope}/react]`;
|
|
86
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_REACT_FROM_NX_REACT);
|
|
87
|
+
if (nodes.length === 0) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const WITH_REACT_IMPORT = 'ImportDeclaration:has(Identifier[name=withReact]) Identifier[name=withReact]';
|
|
91
|
+
const withReactNodes = (0, tsquery_1.tsquery)(ast, WITH_REACT_IMPORT);
|
|
92
|
+
if (nodes.length === 0) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const startIndex = withReactNodes[0].getStart();
|
|
96
|
+
let endIndex = withReactNodes[0].getEnd();
|
|
97
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
98
|
+
endIndex++;
|
|
99
|
+
}
|
|
100
|
+
const newContents = `import { withReact } from '@nx/rspack';
|
|
101
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
102
|
+
tree.write(configPath, newContents);
|
|
103
|
+
}
|
|
104
|
+
function transformWithModuleFederation(tree, configPath, scope) {
|
|
105
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
106
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
107
|
+
const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `ImportDeclaration:has(Identifier[name=withModuleFederation]) > StringLiteral[value=${scope}/react/module-federation]`;
|
|
108
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
|
|
109
|
+
if (nodes.length === 0) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const WITH_MODULE_FEDERATION_IMPORT = 'ImportDeclaration:has(Identifier[name=withModuleFederation]) Identifier[name=withModuleFederation]';
|
|
113
|
+
const withModuleFederationNodes = (0, tsquery_1.tsquery)(ast, WITH_MODULE_FEDERATION_IMPORT);
|
|
114
|
+
if (nodes.length === 0) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const startIndex = withModuleFederationNodes[0].getStart();
|
|
118
|
+
let endIndex = withModuleFederationNodes[0].getEnd();
|
|
119
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
120
|
+
endIndex++;
|
|
121
|
+
}
|
|
122
|
+
const newContents = `import { withModuleFederation } from '@nx/rspack/module-federation';
|
|
123
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
124
|
+
tree.write(configPath, newContents);
|
|
125
|
+
}
|
|
126
|
+
function transformModuleFederationConfig(tree, configPath, scope) {
|
|
127
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
128
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
129
|
+
const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `ImportDeclaration:has(Identifier[name=ModuleFederationConfig]) > StringLiteral[value=${scope}/webpack]`;
|
|
130
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
|
|
131
|
+
if (nodes.length === 0) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const WITH_MODULE_FEDERATION_IMPORT = 'ImportDeclaration:has(Identifier[name=ModuleFederationConfig]) Identifier[name=ModuleFederationConfig]';
|
|
135
|
+
const withModuleFederationNodes = (0, tsquery_1.tsquery)(ast, WITH_MODULE_FEDERATION_IMPORT);
|
|
136
|
+
if (nodes.length === 0) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const startIndex = withModuleFederationNodes[0].getStart();
|
|
140
|
+
let endIndex = withModuleFederationNodes[0].getEnd();
|
|
141
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
142
|
+
endIndex++;
|
|
143
|
+
}
|
|
144
|
+
const newContents = `import { ModuleFederationConfig } from '@nx/rspack/module-federation';
|
|
145
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
146
|
+
tree.write(configPath, newContents);
|
|
147
|
+
}
|
|
148
|
+
function transformWithModuleFederationSSR(tree, configPath, scope) {
|
|
149
|
+
const configContents = tree.read(configPath, 'utf-8');
|
|
150
|
+
const ast = tsquery_1.tsquery.ast(configContents);
|
|
151
|
+
const HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT = `ImportDeclaration:has(Identifier[name=withModuleFederationForSSR]) > StringLiteral[value=${scope}/react/module-federation]`;
|
|
152
|
+
const nodes = (0, tsquery_1.tsquery)(ast, HAS_WITH_MODULE_FEDERATION_FROM_NX_REACT);
|
|
153
|
+
if (nodes.length === 0) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const WITH_MODULE_FEDERATION_IMPORT = 'ImportDeclaration:has(Identifier[name=withModuleFederationForSSR]) Identifier[name=withModuleFederationForSSR]';
|
|
157
|
+
const withModuleFederationNodes = (0, tsquery_1.tsquery)(ast, WITH_MODULE_FEDERATION_IMPORT);
|
|
158
|
+
if (nodes.length === 0) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const startIndex = withModuleFederationNodes[0].getStart();
|
|
162
|
+
let endIndex = withModuleFederationNodes[0].getEnd();
|
|
163
|
+
if (configContents.charAt(endIndex) === ',') {
|
|
164
|
+
endIndex++;
|
|
165
|
+
}
|
|
166
|
+
const newContents = `import { withModuleFederationForSSR } from '@nx/rspack/module-federation';
|
|
167
|
+
${configContents.slice(0, startIndex)}${configContents.slice(endIndex)}`;
|
|
168
|
+
tree.write(configPath, newContents);
|
|
169
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/schema",
|
|
3
|
+
"$id": "Rspack",
|
|
4
|
+
"title": "Nx Webpack to Rspack Generator",
|
|
5
|
+
"description": "Convert a Webpack project to Rspack.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"project": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "The name of the project.",
|
|
11
|
+
"$default": {
|
|
12
|
+
"$source": "argv",
|
|
13
|
+
"index": 0
|
|
14
|
+
},
|
|
15
|
+
"x-dropdown": "project",
|
|
16
|
+
"x-prompt": "What is the name of the project to convert to rspack?",
|
|
17
|
+
"x-priority": "important"
|
|
18
|
+
},
|
|
19
|
+
"skipFormat": {
|
|
20
|
+
"description": "Skip formatting files.",
|
|
21
|
+
"type": "boolean",
|
|
22
|
+
"default": false,
|
|
23
|
+
"x-priority": "internal"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -26,8 +26,17 @@ async function createCompiler(options, context) {
|
|
|
26
26
|
config = userDefinedConfig;
|
|
27
27
|
config.devServer ??= options.devServer;
|
|
28
28
|
}
|
|
29
|
+
validateConfig(config);
|
|
29
30
|
return (0, core_1.rspack)(config);
|
|
30
31
|
}
|
|
31
32
|
function isMultiCompiler(compiler) {
|
|
32
33
|
return 'compilers' in compiler;
|
|
33
34
|
}
|
|
35
|
+
function validateConfig(config) {
|
|
36
|
+
if (!config.entry) {
|
|
37
|
+
throw new Error('Entry is required. Please set the `main` option in the executor or the `entry` property in the rspack config.');
|
|
38
|
+
}
|
|
39
|
+
if (!config.output) {
|
|
40
|
+
throw new Error('Output is required. Please set the `outputPath` option in the executor or the `output` property in the rspack config.');
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/utils/versions.d.ts
CHANGED
package/src/utils/versions.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.eslintPluginReactHooksVersion = exports.eslintPluginReactVersion = exports.eslintPluginJsxA11yVersion = exports.eslintPluginImportVersion = exports.stylusVersion = exports.sassVersion = exports.lessVersion = exports.nestjsMicroservicesVersion = exports.nestjsPlatformExpressVersion = exports.nestjsCoreVersion = exports.nestjsCommonVersion = exports.typesReactDomVersion = exports.typesReactVersion = exports.reactDomVersion = exports.reactRefreshVersion = exports.reactVersion = exports.lessLoaderVersion = exports.rspackPluginReactRefreshVersion = exports.rspackPluginMinifyVersion = exports.rspackDevServerVersion = exports.rspackCoreVersion = void 0;
|
|
3
|
+
exports.eslintPluginReactHooksVersion = exports.eslintPluginReactVersion = exports.eslintPluginJsxA11yVersion = exports.eslintPluginImportVersion = exports.stylusVersion = exports.sassVersion = exports.lessVersion = exports.nestjsMicroservicesVersion = exports.nestjsPlatformExpressVersion = exports.nestjsCoreVersion = exports.nestjsCommonVersion = exports.typesReactDomVersion = exports.typesReactVersion = exports.reactDomVersion = exports.reactRefreshVersion = exports.reactVersion = exports.lessLoaderVersion = exports.rspackPluginReactRefreshVersion = exports.rspackPluginMinifyVersion = exports.rspackDevServerVersion = exports.rspackCoreVersion = exports.nxVersion = void 0;
|
|
4
|
+
exports.nxVersion = require('../../package.json').version;
|
|
4
5
|
exports.rspackCoreVersion = '1.0.5';
|
|
5
6
|
exports.rspackDevServerVersion = '1.0.5';
|
|
6
7
|
exports.rspackPluginMinifyVersion = '^0.7.5';
|
package/src/utils/with-web.js
CHANGED
|
@@ -20,6 +20,7 @@ function withWeb(opts = {}) {
|
|
|
20
20
|
}
|
|
21
21
|
return {
|
|
22
22
|
...config,
|
|
23
|
+
target: config.target ?? 'web',
|
|
23
24
|
experiments: {
|
|
24
25
|
css: true,
|
|
25
26
|
},
|
|
@@ -101,7 +102,27 @@ function withWeb(opts = {}) {
|
|
|
101
102
|
new core_1.rspack.EnvironmentPlugin({
|
|
102
103
|
NODE_ENV: isProd ? 'production' : 'development',
|
|
103
104
|
}),
|
|
105
|
+
new core_1.rspack.DefinePlugin(getClientEnvironment(isProd ? 'production' : undefined).stringified),
|
|
104
106
|
],
|
|
105
107
|
};
|
|
106
108
|
};
|
|
107
109
|
}
|
|
110
|
+
function getClientEnvironment(mode) {
|
|
111
|
+
// Grab NODE_ENV and NX_PUBLIC_* environment variables and prepare them to be
|
|
112
|
+
// injected into the application via DefinePlugin in webpack configuration.
|
|
113
|
+
const nxPublicKeyRegex = /^NX_PUBLIC_/i;
|
|
114
|
+
const raw = Object.keys(process.env)
|
|
115
|
+
.filter((key) => nxPublicKeyRegex.test(key))
|
|
116
|
+
.reduce((env, key) => {
|
|
117
|
+
env[key] = process.env[key];
|
|
118
|
+
return env;
|
|
119
|
+
}, {});
|
|
120
|
+
// Stringify all values so we can feed into webpack DefinePlugin
|
|
121
|
+
const stringified = {
|
|
122
|
+
'process.env': Object.keys(raw).reduce((env, key) => {
|
|
123
|
+
env[key] = JSON.stringify(raw[key]);
|
|
124
|
+
return env;
|
|
125
|
+
}, {}),
|
|
126
|
+
};
|
|
127
|
+
return { stringified };
|
|
128
|
+
}
|