@putout/plugin-convert-webpack-to-rspack 1.0.2 → 1.1.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/README.md +99 -65
- package/lib/apply-global-define/fixture/define-other-fix.js +14 -0
- package/lib/apply-global-define/fixture/define-other.js +11 -0
- package/lib/apply-global-define/fixture/defined-destructured.js +12 -0
- package/lib/apply-global-define/fixture/defined.js +10 -0
- package/lib/apply-global-define/fixture/export-not-object.js +1 -0
- package/lib/apply-global-define/fixture/global-define-fix.js +13 -0
- package/lib/apply-global-define/fixture/global-define.js +8 -0
- package/lib/apply-global-define/fixture/not-config.js +3 -0
- package/lib/apply-global-define/fixture/output-only.js +5 -0
- package/lib/apply-global-define/fixture/plugins-fix.js +13 -0
- package/lib/apply-global-define/fixture/plugins-variable.js +6 -0
- package/lib/apply-global-define/fixture/plugins.js +10 -0
- package/lib/apply-global-define/fixture/target-node-array.js +4 -0
- package/lib/apply-global-define/fixture/target-node.js +4 -0
- package/lib/apply-global-define/fixture/target-web-fix.js +9 -0
- package/lib/apply-global-define/fixture/target-web.js +4 -0
- package/lib/apply-global-define/index.js +120 -0
- package/lib/apply-library-object/fixture/library-export-not-string.js +7 -0
- package/lib/apply-library-object/fixture/library-object.js +9 -0
- package/lib/apply-library-object/fixture/library-target-no-library.js +6 -0
- package/lib/apply-library-object/fixture/library-target-not-string.js +6 -0
- package/lib/apply-library-object/index.js +25 -16
- package/lib/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,49 +13,32 @@ npm i putout @putout/plugin-convert-webpack-to-rspack -D
|
|
|
13
13
|
|
|
14
14
|
## Rules
|
|
15
15
|
|
|
16
|
-
- ✅ [apply-
|
|
17
|
-
- ✅ [apply-
|
|
18
|
-
- ✅ [apply-
|
|
19
|
-
- ✅ [apply-
|
|
20
|
-
- ✅ [
|
|
16
|
+
- ✅ [apply-asset-type](#apply-asset-type);
|
|
17
|
+
- ✅ [apply-global-define](#apply-global-define);
|
|
18
|
+
- ✅ [apply-library-object](#apply-library-object);
|
|
19
|
+
- ✅ [apply-rspack-import](#apply-rspack-import);
|
|
20
|
+
- ✅ [apply-swc-loader](#apply-swc-loader);
|
|
21
|
+
- ✅ [remove-webpackbar](#remove-webpackbar);
|
|
21
22
|
|
|
22
23
|
## Config
|
|
23
24
|
|
|
24
25
|
```json
|
|
25
26
|
{
|
|
26
27
|
"rules": {
|
|
27
|
-
"convert-webpack-to-rspack/apply-rspack-import": "on",
|
|
28
|
-
"convert-webpack-to-rspack/apply-swc-loader": "on",
|
|
29
28
|
"convert-webpack-to-rspack/apply-asset-type": "on",
|
|
29
|
+
"convert-webpack-to-rspack/apply-global-define": "on",
|
|
30
30
|
"convert-webpack-to-rspack/apply-library-object": "on",
|
|
31
|
+
"convert-webpack-to-rspack/apply-rspack-import": "on",
|
|
32
|
+
"convert-webpack-to-rspack/apply-swc-loader": "on",
|
|
33
|
+
|
|
31
34
|
"convert-webpack-to-rspack/remove-webpackbar": "on"
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
```
|
|
35
38
|
|
|
36
|
-
## apply-
|
|
37
|
-
|
|
38
|
-
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/6af7c06cecb5e2b5717271dddd783fcf/fb2b92aa42bb5c1dadd2d5d5fb98ca06d2b6ee0d).
|
|
39
|
-
|
|
40
|
-
### ❌ Example of incorrect code
|
|
41
|
-
|
|
42
|
-
```js
|
|
43
|
-
import webpack from 'webpack';
|
|
44
|
-
|
|
45
|
-
const {EnvironmentPlugin} = webpack;
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### ✅ Example of correct code
|
|
49
|
-
|
|
50
|
-
```js
|
|
51
|
-
import {rspack} from '@rspack/core';
|
|
52
|
-
|
|
53
|
-
const {EnvironmentPlugin} = rspack;
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## apply-swc-loader
|
|
39
|
+
## apply-asset-type
|
|
57
40
|
|
|
58
|
-
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/
|
|
41
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/6d4152d33dd67f39ef4a1d56769dc22b/a6d7b603f326717a462c01fb9568e4f989ca7af7).
|
|
59
42
|
|
|
60
43
|
### ❌ Example of incorrect code
|
|
61
44
|
|
|
@@ -63,8 +46,13 @@ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d26a479c7f
|
|
|
63
46
|
export default {
|
|
64
47
|
module: {
|
|
65
48
|
rules: [{
|
|
66
|
-
test: /\.
|
|
67
|
-
|
|
49
|
+
test: /\.(png|gif|svg)$/,
|
|
50
|
+
use: {
|
|
51
|
+
loader: 'url-loader',
|
|
52
|
+
options: {
|
|
53
|
+
limit: 50_000,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
68
56
|
}],
|
|
69
57
|
},
|
|
70
58
|
};
|
|
@@ -76,16 +64,11 @@ export default {
|
|
|
76
64
|
export default {
|
|
77
65
|
module: {
|
|
78
66
|
rules: [{
|
|
79
|
-
test: /\.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
syntax: 'ecmascript',
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
env: {
|
|
88
|
-
targets: 'defaults',
|
|
67
|
+
test: /\.(png|gif|svg)$/,
|
|
68
|
+
type: 'asset',
|
|
69
|
+
parser: {
|
|
70
|
+
dataUrlCondition: {
|
|
71
|
+
maxSize: 50_000,
|
|
89
72
|
},
|
|
90
73
|
},
|
|
91
74
|
}],
|
|
@@ -93,43 +76,34 @@ export default {
|
|
|
93
76
|
};
|
|
94
77
|
```
|
|
95
78
|
|
|
96
|
-
## apply-
|
|
79
|
+
## apply-global-define
|
|
97
80
|
|
|
98
|
-
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/
|
|
81
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/627e3b66b31f9490499dcd611b535db5/8c20fc89fbe02011c38ffb850486db0452ffdcfa).
|
|
82
|
+
|
|
83
|
+
Webpack replaces `global` with the global object in browser bundles, `rspack` does not do it for ESM modules (`node.global` has no effect there), so a bundle that touches `global` throws `ReferenceError: global is not defined` in the browser.
|
|
84
|
+
|
|
85
|
+
Use `DefinePlugin` to replace `global` with `globalThis`. Skipped for `target: 'node'`.
|
|
99
86
|
|
|
100
87
|
### ❌ Example of incorrect code
|
|
101
88
|
|
|
102
89
|
```js
|
|
103
90
|
export default {
|
|
104
|
-
|
|
105
|
-
rules: [{
|
|
106
|
-
test: /\.(png|gif|svg)$/,
|
|
107
|
-
use: {
|
|
108
|
-
loader: 'url-loader',
|
|
109
|
-
options: {
|
|
110
|
-
limit: 50_000,
|
|
111
|
-
},
|
|
112
|
-
},
|
|
113
|
-
}],
|
|
114
|
-
},
|
|
91
|
+
entry: './client/index.js',
|
|
115
92
|
};
|
|
116
93
|
```
|
|
117
94
|
|
|
118
95
|
### ✅ Example of correct code
|
|
119
96
|
|
|
120
97
|
```js
|
|
98
|
+
import {rspack} from '@rspack/core';
|
|
99
|
+
|
|
121
100
|
export default {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
maxSize: 50_000,
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
}],
|
|
132
|
-
},
|
|
101
|
+
entry: './client/index.js',
|
|
102
|
+
plugins: [
|
|
103
|
+
new rspack.DefinePlugin({
|
|
104
|
+
global: 'globalThis',
|
|
105
|
+
}),
|
|
106
|
+
],
|
|
133
107
|
};
|
|
134
108
|
```
|
|
135
109
|
|
|
@@ -163,6 +137,66 @@ export default {
|
|
|
163
137
|
};
|
|
164
138
|
```
|
|
165
139
|
|
|
140
|
+
## apply-rspack-import
|
|
141
|
+
|
|
142
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/6af7c06cecb5e2b5717271dddd783fcf/fb2b92aa42bb5c1dadd2d5d5fb98ca06d2b6ee0d).
|
|
143
|
+
|
|
144
|
+
### ❌ Example of incorrect code
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import webpack from 'webpack';
|
|
148
|
+
|
|
149
|
+
const {EnvironmentPlugin} = webpack;
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### ✅ Example of correct code
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
import {rspack} from '@rspack/core';
|
|
156
|
+
|
|
157
|
+
const {EnvironmentPlugin} = rspack;
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## apply-swc-loader
|
|
161
|
+
|
|
162
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d26a479c7f677bcfe74f9f946c514983/c88686d689396c1e47b22d8aee24e737eff7e24f).
|
|
163
|
+
|
|
164
|
+
### ❌ Example of incorrect code
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
export default {
|
|
168
|
+
module: {
|
|
169
|
+
rules: [{
|
|
170
|
+
test: /\.js$/,
|
|
171
|
+
loader: 'babel-loader',
|
|
172
|
+
}],
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### ✅ Example of correct code
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
export default {
|
|
181
|
+
module: {
|
|
182
|
+
rules: [{
|
|
183
|
+
test: /\.js$/,
|
|
184
|
+
loader: 'builtin:swc-loader',
|
|
185
|
+
options: {
|
|
186
|
+
jsc: {
|
|
187
|
+
parser: {
|
|
188
|
+
syntax: 'ecmascript',
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
env: {
|
|
192
|
+
targets: 'defaults',
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
}],
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
```
|
|
199
|
+
|
|
166
200
|
## remove-webpackbar
|
|
167
201
|
|
|
168
202
|
### ❌ Example of incorrect code
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {rspack} from '@rspack/core';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
entry: './client/index.js',
|
|
5
|
+
plugins: [
|
|
6
|
+
new rspack.DefinePlugin(config),
|
|
7
|
+
new rspack.DefinePlugin({
|
|
8
|
+
hello: 'world',
|
|
9
|
+
}),
|
|
10
|
+
new rspack.DefinePlugin({
|
|
11
|
+
global: 'globalThis',
|
|
12
|
+
}),
|
|
13
|
+
],
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default config;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {
|
|
2
|
+
types,
|
|
3
|
+
operator,
|
|
4
|
+
template,
|
|
5
|
+
} from 'putout';
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
createTypeChecker,
|
|
9
|
+
getProperty,
|
|
10
|
+
} = operator;
|
|
11
|
+
|
|
12
|
+
const {isArray} = Array;
|
|
13
|
+
|
|
14
|
+
const {
|
|
15
|
+
isStringLiteral,
|
|
16
|
+
isIdentifier,
|
|
17
|
+
isArrayExpression,
|
|
18
|
+
} = types;
|
|
19
|
+
|
|
20
|
+
const IMPORT = `import {rspack} from '@rspack/core'`;
|
|
21
|
+
|
|
22
|
+
const DEFINE_GLOBAL = `new rspack.DefinePlugin({
|
|
23
|
+
global: 'globalThis',
|
|
24
|
+
})`;
|
|
25
|
+
|
|
26
|
+
const getConfigProperty = (path, name) => getProperty(path.get('declaration'), name);
|
|
27
|
+
|
|
28
|
+
const hasNoEntry = (path) => !getConfigProperty(path, 'entry');
|
|
29
|
+
const hasDefinedGlobal = ({parentPath}) => hasGlobalDefine(parentPath);
|
|
30
|
+
|
|
31
|
+
export const report = () => `Use 'DefinePlugin' to replace 'global' with 'globalThis': 'node.global' does not work for ESM modules`;
|
|
32
|
+
|
|
33
|
+
export const include = () => [
|
|
34
|
+
'ExportDefaultDeclaration',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
export const filter = createTypeChecker([
|
|
38
|
+
['-: node.declaration -> !ObjectExpression'],
|
|
39
|
+
['-', hasNoEntry],
|
|
40
|
+
['-', hasNodeTarget],
|
|
41
|
+
['-', hasDefinedGlobal],
|
|
42
|
+
['-', hasWrongPlugins],
|
|
43
|
+
['+: -> +'],
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
export const fix = (path) => {
|
|
47
|
+
const program = path.parentPath;
|
|
48
|
+
const pluginsProp = getConfigProperty(path, 'plugins');
|
|
49
|
+
const hasImport = program.get('body').some(isRspackImport);
|
|
50
|
+
|
|
51
|
+
if (!hasImport)
|
|
52
|
+
program.unshiftContainer('body', template.ast(IMPORT));
|
|
53
|
+
|
|
54
|
+
if (pluginsProp) {
|
|
55
|
+
pluginsProp.get('value').pushContainer('elements', template.ast(DEFINE_GLOBAL));
|
|
56
|
+
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const [property] = template.ast(`({plugins: [${DEFINE_GLOBAL}]})`).properties;
|
|
61
|
+
|
|
62
|
+
path.get('declaration').pushContainer('properties', property);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const includesNode = (a) => isStringLiteral(a) && a.value.includes('node');
|
|
66
|
+
const hasNode = (a) => isArray(a) && a.some(includesNode);
|
|
67
|
+
|
|
68
|
+
const hasGlobal = (properties) => properties.some(({key}) => isIdentifier(key, {
|
|
69
|
+
name: 'global',
|
|
70
|
+
}));
|
|
71
|
+
|
|
72
|
+
const hasRspack = (specifiers) => specifiers.some(({imported}) => isIdentifier(imported, {
|
|
73
|
+
name: 'rspack',
|
|
74
|
+
}));
|
|
75
|
+
|
|
76
|
+
const isNodeTarget = createTypeChecker([
|
|
77
|
+
['+: node.value', includesNode],
|
|
78
|
+
['+: node.value.elements', hasNode],
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
const isRspackImport = createTypeChecker([
|
|
82
|
+
['-: -> !ImportDeclaration'],
|
|
83
|
+
['-: node.source.value', '!=', '@rspack/core'],
|
|
84
|
+
['+: node.specifiers', hasRspack],
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
const isDefinePlugin = createTypeChecker([
|
|
88
|
+
['+: node.callee.name', '=', 'DefinePlugin'],
|
|
89
|
+
['+: node.callee.property.name', '=', 'DefinePlugin'],
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
const hasGlobalKey = createTypeChecker([
|
|
93
|
+
['-: node.arguments.0 -> !ObjectExpression'],
|
|
94
|
+
['+: node.arguments.0.properties', hasGlobal],
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
const hasGlobalDefine = (program) => {
|
|
98
|
+
let found = false;
|
|
99
|
+
|
|
100
|
+
program.traverse({
|
|
101
|
+
NewExpression(path) {
|
|
102
|
+
if (isDefinePlugin(path) && hasGlobalKey(path))
|
|
103
|
+
found = true;
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return found;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
function hasNodeTarget(path) {
|
|
111
|
+
const targetProp = getConfigProperty(path, 'target');
|
|
112
|
+
|
|
113
|
+
return targetProp && isNodeTarget(targetProp);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function hasWrongPlugins(path) {
|
|
117
|
+
const pluginsProp = getConfigProperty(path, 'plugins');
|
|
118
|
+
|
|
119
|
+
return pluginsProp && !isArrayExpression(pluginsProp.get('value'));
|
|
120
|
+
}
|
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import {operator, template} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {
|
|
4
|
+
remove,
|
|
5
|
+
replaceWith,
|
|
6
|
+
getProperty,
|
|
7
|
+
createTypeChecker,
|
|
8
|
+
} = operator;
|
|
4
9
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const isLibraryTarget = (path) => path.get('key').isIdentifier({
|
|
10
|
-
name: 'libraryTarget',
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
const isLibraryExport = (path) => path.get('key').isIdentifier({
|
|
14
|
-
name: 'libraryExport',
|
|
15
|
-
});
|
|
10
|
+
const isStringValue = createTypeChecker([
|
|
11
|
+
'-: node.value -> !StringLiteral',
|
|
12
|
+
'+: -> +',
|
|
13
|
+
]);
|
|
16
14
|
|
|
17
15
|
export const report = () => `Use 'output.library' object instead of 'libraryTarget'`;
|
|
18
16
|
|
|
@@ -43,17 +41,28 @@ export const fix = ({libraryProp, libraryTargetProp, libraryExportProp}) => {
|
|
|
43
41
|
|
|
44
42
|
export const traverse = ({push}) => ({
|
|
45
43
|
ObjectExpression(path) {
|
|
46
|
-
const
|
|
47
|
-
const libraryTargetProp = properties.find(isLibraryTarget);
|
|
44
|
+
const libraryTargetProp = getProperty(path, 'libraryTarget');
|
|
48
45
|
|
|
49
46
|
if (!libraryTargetProp)
|
|
50
47
|
return;
|
|
51
48
|
|
|
49
|
+
const libraryProp = getProperty(path, 'library');
|
|
50
|
+
const libraryExportProp = getProperty(path, 'libraryExport');
|
|
51
|
+
|
|
52
|
+
if (!libraryProp || !isStringValue(libraryProp))
|
|
53
|
+
return;
|
|
54
|
+
|
|
55
|
+
if (!isStringValue(libraryTargetProp))
|
|
56
|
+
return;
|
|
57
|
+
|
|
58
|
+
if (libraryExportProp && !isStringValue(libraryExportProp))
|
|
59
|
+
return;
|
|
60
|
+
|
|
52
61
|
push({
|
|
53
62
|
path,
|
|
54
|
-
libraryProp
|
|
63
|
+
libraryProp,
|
|
55
64
|
libraryTargetProp,
|
|
56
|
-
libraryExportProp
|
|
65
|
+
libraryExportProp,
|
|
57
66
|
});
|
|
58
67
|
},
|
|
59
68
|
});
|
package/lib/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import * as applyRspackImport from './apply-rspack-import/index.js';
|
|
|
2
2
|
import * as applySwcLoader from './apply-swc-loader/index.js';
|
|
3
3
|
import * as applyAssetType from './apply-asset-type/index.js';
|
|
4
4
|
import * as applyLibraryObject from './apply-library-object/index.js';
|
|
5
|
+
import * as applyGlobalDefine from './apply-global-define/index.js';
|
|
5
6
|
import * as removeWebpackbar from './remove-webpackbar/index.js';
|
|
6
7
|
|
|
7
8
|
export const rules = {
|
|
@@ -9,5 +10,6 @@ export const rules = {
|
|
|
9
10
|
'apply-swc-loader': applySwcLoader,
|
|
10
11
|
'apply-asset-type': applyAssetType,
|
|
11
12
|
'apply-library-object': applyLibraryObject,
|
|
13
|
+
'apply-global-define': applyGlobalDefine,
|
|
12
14
|
'remove-webpackbar': removeWebpackbar,
|
|
13
15
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-convert-webpack-to-rspack",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout plugin to convert webpack config to rspack",
|