@putout/plugin-rspack 0.0.0-stage β 1.0.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/LICENSE +21 -0
- package/README.md +222 -2
- package/lib/apply-asset-type/fixture/no-url-loader.js +13 -0
- package/lib/apply-asset-type/fixture/url-loader-fix.js +13 -0
- package/lib/apply-asset-type/fixture/url-loader.js +13 -0
- package/lib/apply-asset-type/fixture/use-array.js +9 -0
- package/lib/apply-asset-type/index.js +87 -0
- 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/other-import-fix.js +9 -0
- package/lib/apply-global-define/fixture/other-import.js +5 -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-fix.js +10 -0
- package/lib/apply-library-object/fixture/library-target-no-export-fix.js +9 -0
- package/lib/apply-library-object/fixture/library-target-no-export.js +7 -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/fixture/library-target.js +8 -0
- package/lib/apply-library-object/fixture/no-library-target.js +10 -0
- package/lib/apply-library-object/index.js +67 -0
- package/lib/apply-rspack-import/fixture/no-webpack.js +3 -0
- package/lib/apply-rspack-import/fixture/webpack-import-fix.js +7 -0
- package/lib/apply-rspack-import/fixture/webpack-import.js +7 -0
- package/lib/apply-rspack-import/index.js +12 -0
- package/lib/apply-swc-loader/fixture/babel-loader-fix.js +19 -0
- package/lib/apply-swc-loader/fixture/babel-loader-options-fix.js +19 -0
- package/lib/apply-swc-loader/fixture/babel-loader-options.js +12 -0
- package/lib/apply-swc-loader/fixture/babel-loader.js +9 -0
- package/lib/apply-swc-loader/fixture/no-babel-loader.js +19 -0
- package/lib/apply-swc-loader/index.js +78 -0
- package/lib/index.js +15 -0
- package/lib/remove-webpackbar/fixture/no-webpackbar.js +7 -0
- package/lib/remove-webpackbar/fixture/webpackbar-fix.js +7 -0
- package/lib/remove-webpackbar/fixture/webpackbar.js +9 -0
- package/lib/remove-webpackbar/index.js +6 -0
- package/package.json +50 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) coderaiser
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,223 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @putout/plugin-rspack [![NPM version][NPMIMGURL]][NPMURL]
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-rspack.svg?style=flat&longCache=true
|
|
4
|
+
[NPMURL]: https://npmjs.org/package/@putout/plugin-rspack "npm"
|
|
5
|
+
|
|
6
|
+
π[**Putout**](https://github.com/coderaiser/putout) plugin to convert `webpack.config.js` to [rspack](https://rspack.dev). *Not bundled*.
|
|
7
|
+
|
|
8
|
+
Renamed to [@putout/plugin-rspack](https://npmjs.org/package/@putout/plugin-rspack).
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
npm i putout @putout/plugin-rspack -D
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Rules
|
|
17
|
+
|
|
18
|
+
- β
[apply-asset-type](#apply-asset-type);
|
|
19
|
+
- β
[apply-global-define](#apply-global-define);
|
|
20
|
+
- β
[apply-library-object](#apply-library-object);
|
|
21
|
+
- β
[apply-rspack-import](#apply-rspack-import);
|
|
22
|
+
- β
[apply-swc-loader](#apply-swc-loader);
|
|
23
|
+
- β
[remove-webpackbar](#remove-webpackbar);
|
|
24
|
+
|
|
25
|
+
## Config
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"rules": {
|
|
30
|
+
"rspack/apply-asset-type": "on",
|
|
31
|
+
"rspack/apply-global-define": "on",
|
|
32
|
+
"rspack/apply-library-object": "on",
|
|
33
|
+
"rspack/apply-rspack-import": "on",
|
|
34
|
+
"rspack/apply-swc-loader": "on",
|
|
35
|
+
"rspack/remove-webpackbar": "on"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## apply-asset-type
|
|
41
|
+
|
|
42
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/6d4152d33dd67f39ef4a1d56769dc22b/a6d7b603f326717a462c01fb9568e4f989ca7af7).
|
|
43
|
+
|
|
44
|
+
### β Example of incorrect code
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
export default {
|
|
48
|
+
module: {
|
|
49
|
+
rules: [{
|
|
50
|
+
test: /\.(png|gif|svg)$/,
|
|
51
|
+
use: {
|
|
52
|
+
loader: 'url-loader',
|
|
53
|
+
options: {
|
|
54
|
+
limit: 50_000,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
}],
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### β
Example of correct code
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
export default {
|
|
66
|
+
module: {
|
|
67
|
+
rules: [{
|
|
68
|
+
test: /\.(png|gif|svg)$/,
|
|
69
|
+
type: 'asset',
|
|
70
|
+
parser: {
|
|
71
|
+
dataUrlCondition: {
|
|
72
|
+
maxSize: 50_000,
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
}],
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## apply-global-define
|
|
81
|
+
|
|
82
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/627e3b66b31f9490499dcd611b535db5/8c20fc89fbe02011c38ffb850486db0452ffdcfa).
|
|
83
|
+
|
|
84
|
+
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.
|
|
85
|
+
|
|
86
|
+
Use `DefinePlugin` to replace `global` with `globalThis`. Skipped for `target: 'node'`.
|
|
87
|
+
|
|
88
|
+
### β Example of incorrect code
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
export default {
|
|
92
|
+
entry: './client/index.js',
|
|
93
|
+
};
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### β
Example of correct code
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
import {rspack} from '@rspack/core';
|
|
100
|
+
|
|
101
|
+
export default {
|
|
102
|
+
entry: './client/index.js',
|
|
103
|
+
plugins: [
|
|
104
|
+
new rspack.DefinePlugin({
|
|
105
|
+
global: 'globalThis',
|
|
106
|
+
}),
|
|
107
|
+
],
|
|
108
|
+
};
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## apply-library-object
|
|
112
|
+
|
|
113
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9b85da3e385d1c254f4103db8952323/1b7c7c9e8568bba9070469ced671c1dd5022bed3).
|
|
114
|
+
|
|
115
|
+
### β Example of incorrect code
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
export default {
|
|
119
|
+
output: {
|
|
120
|
+
library: 'Deepword',
|
|
121
|
+
libraryTarget: 'var',
|
|
122
|
+
libraryExport: 'default',
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### β
Example of correct code
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
export default {
|
|
131
|
+
output: {
|
|
132
|
+
library: {
|
|
133
|
+
name: 'Deepword',
|
|
134
|
+
type: 'var',
|
|
135
|
+
export: 'default',
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## apply-rspack-import
|
|
142
|
+
|
|
143
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/6af7c06cecb5e2b5717271dddd783fcf/fb2b92aa42bb5c1dadd2d5d5fb98ca06d2b6ee0d).
|
|
144
|
+
|
|
145
|
+
### β Example of incorrect code
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
import webpack from 'webpack';
|
|
149
|
+
|
|
150
|
+
const {EnvironmentPlugin} = webpack;
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### β
Example of correct code
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
import {rspack} from '@rspack/core';
|
|
157
|
+
|
|
158
|
+
const {EnvironmentPlugin} = rspack;
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## apply-swc-loader
|
|
162
|
+
|
|
163
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d26a479c7f677bcfe74f9f946c514983/c88686d689396c1e47b22d8aee24e737eff7e24f).
|
|
164
|
+
|
|
165
|
+
### β Example of incorrect code
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
export default {
|
|
169
|
+
module: {
|
|
170
|
+
rules: [{
|
|
171
|
+
test: /\.js$/,
|
|
172
|
+
loader: 'babel-loader',
|
|
173
|
+
}],
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### β
Example of correct code
|
|
179
|
+
|
|
180
|
+
```js
|
|
181
|
+
export default {
|
|
182
|
+
module: {
|
|
183
|
+
rules: [{
|
|
184
|
+
test: /\.js$/,
|
|
185
|
+
loader: 'builtin:swc-loader',
|
|
186
|
+
options: {
|
|
187
|
+
jsc: {
|
|
188
|
+
parser: {
|
|
189
|
+
syntax: 'ecmascript',
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
env: {
|
|
193
|
+
targets: 'defaults',
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
}],
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## remove-webpackbar
|
|
202
|
+
|
|
203
|
+
### β Example of incorrect code
|
|
204
|
+
|
|
205
|
+
```js
|
|
206
|
+
import WebpackBar from 'webpackbar';
|
|
207
|
+
|
|
208
|
+
export default {
|
|
209
|
+
plugins: [new WebpackBar()],
|
|
210
|
+
};
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### β
Example of correct code
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
export default {
|
|
217
|
+
plugins: [],
|
|
218
|
+
};
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## License
|
|
222
|
+
|
|
223
|
+
MIT
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
template,
|
|
3
|
+
types,
|
|
4
|
+
operator,
|
|
5
|
+
} from 'putout';
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
isObjectProperty,
|
|
9
|
+
stringLiteral,
|
|
10
|
+
identifier,
|
|
11
|
+
objectProperty,
|
|
12
|
+
isStringLiteral,
|
|
13
|
+
isIdentifier,
|
|
14
|
+
isArrayExpression,
|
|
15
|
+
} = types;
|
|
16
|
+
|
|
17
|
+
const {insertAfter, remove} = operator;
|
|
18
|
+
|
|
19
|
+
const isUse = (path) => isIdentifier(path.node.key, {
|
|
20
|
+
name: 'use',
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const isOptions = (path) => isIdentifier(path.node.key, {
|
|
24
|
+
name: 'options',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const isLimit = (path) => isIdentifier(path.node.key, {
|
|
28
|
+
name: 'limit',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const isUrlLoader = (path) => isStringLiteral(path.node.value, {
|
|
32
|
+
value: 'url-loader',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const report = () => `Use rspack built-in 'asset' type instead of 'url-loader'`;
|
|
36
|
+
|
|
37
|
+
export const fix = ({path, propertyUse, propertyLimit}) => {
|
|
38
|
+
const limit = propertyLimit.node.value.extra.raw;
|
|
39
|
+
|
|
40
|
+
remove(propertyUse);
|
|
41
|
+
|
|
42
|
+
const [firstProp] = path.get('properties');
|
|
43
|
+
const propertyParser = objectProperty(identifier('parser'), template.ast(`({
|
|
44
|
+
dataUrlCondition: {
|
|
45
|
+
maxSize: ${limit},
|
|
46
|
+
},
|
|
47
|
+
})`));
|
|
48
|
+
|
|
49
|
+
const propertyType = objectProperty(identifier('type'), stringLiteral('asset'));
|
|
50
|
+
|
|
51
|
+
insertAfter(firstProp, propertyParser);
|
|
52
|
+
insertAfter(firstProp, propertyType);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const traverse = ({push}) => ({
|
|
56
|
+
ObjectExpression(path) {
|
|
57
|
+
const topProperties = path.get('properties').filter(isObjectProperty);
|
|
58
|
+
|
|
59
|
+
for (const propertyUse of topProperties) {
|
|
60
|
+
if (!isUse(propertyUse))
|
|
61
|
+
continue;
|
|
62
|
+
|
|
63
|
+
const valuePath = propertyUse.get('value');
|
|
64
|
+
|
|
65
|
+
if (isArrayExpression(valuePath))
|
|
66
|
+
continue;
|
|
67
|
+
|
|
68
|
+
const properties = valuePath.get('properties').filter(isObjectProperty);
|
|
69
|
+
|
|
70
|
+
for (const property of properties) {
|
|
71
|
+
if (!isUrlLoader(property))
|
|
72
|
+
continue;
|
|
73
|
+
|
|
74
|
+
const propertyOptions = properties.find(isOptions);
|
|
75
|
+
|
|
76
|
+
const propertyLimit = propertyOptions.get('value.properties').filter(isObjectProperty)
|
|
77
|
+
.find(isLimit);
|
|
78
|
+
|
|
79
|
+
push({
|
|
80
|
+
path,
|
|
81
|
+
propertyUse,
|
|
82
|
+
propertyLimit,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
});
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {operator, template} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
remove,
|
|
5
|
+
replaceWith,
|
|
6
|
+
getProperty,
|
|
7
|
+
createTypeChecker,
|
|
8
|
+
} = operator;
|
|
9
|
+
|
|
10
|
+
const isStringValue = createTypeChecker([
|
|
11
|
+
'+: node.value -> StringLiteral',
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
export const report = () => `Use 'output.library' object instead of 'libraryTarget'`;
|
|
15
|
+
|
|
16
|
+
export const fix = ({libraryProp, libraryTargetProp, libraryExportProp}) => {
|
|
17
|
+
const libraryValuePath = libraryProp.get('value');
|
|
18
|
+
const name = libraryValuePath.node.value;
|
|
19
|
+
const type = libraryTargetProp.node.value.value;
|
|
20
|
+
|
|
21
|
+
remove(libraryTargetProp);
|
|
22
|
+
|
|
23
|
+
if (libraryExportProp) {
|
|
24
|
+
const exportVal = libraryExportProp.node.value.value;
|
|
25
|
+
remove(libraryExportProp);
|
|
26
|
+
replaceWith(libraryValuePath, template.ast(`({
|
|
27
|
+
name: '${name}',
|
|
28
|
+
type: '${type}',
|
|
29
|
+
export: '${exportVal}',
|
|
30
|
+
})`));
|
|
31
|
+
} else {
|
|
32
|
+
replaceWith(libraryValuePath, template.ast(`({
|
|
33
|
+
name: '${name}',
|
|
34
|
+
type: '${type}',
|
|
35
|
+
})`));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
delete libraryProp.node.loc;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const traverse = ({push}) => ({
|
|
42
|
+
ObjectExpression(path) {
|
|
43
|
+
const libraryTargetProp = getProperty(path, 'libraryTarget');
|
|
44
|
+
|
|
45
|
+
if (!libraryTargetProp)
|
|
46
|
+
return;
|
|
47
|
+
|
|
48
|
+
const libraryProp = getProperty(path, 'library');
|
|
49
|
+
const libraryExportProp = getProperty(path, 'libraryExport');
|
|
50
|
+
|
|
51
|
+
if (!libraryProp || !isStringValue(libraryProp))
|
|
52
|
+
return;
|
|
53
|
+
|
|
54
|
+
if (!isStringValue(libraryTargetProp))
|
|
55
|
+
return;
|
|
56
|
+
|
|
57
|
+
if (libraryExportProp && !isStringValue(libraryExportProp))
|
|
58
|
+
return;
|
|
59
|
+
|
|
60
|
+
push({
|
|
61
|
+
path,
|
|
62
|
+
libraryProp,
|
|
63
|
+
libraryTargetProp,
|
|
64
|
+
libraryExportProp,
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {operator} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {rename} = operator;
|
|
4
|
+
|
|
5
|
+
export const report = () => `Use 'rspack' instead of 'webpack'`;
|
|
6
|
+
|
|
7
|
+
export const replace = () => ({
|
|
8
|
+
'import webpack from "webpack"': (vars, path) => {
|
|
9
|
+
rename(path, 'webpack', 'rspack');
|
|
10
|
+
return 'import {rspack} from "@rspack/core"';
|
|
11
|
+
},
|
|
12
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
module: {
|
|
3
|
+
rules: [{
|
|
4
|
+
test: /\.js$/,
|
|
5
|
+
exclude: /node_modules/,
|
|
6
|
+
loader: 'builtin:swc-loader',
|
|
7
|
+
options: {
|
|
8
|
+
jsc: {
|
|
9
|
+
parser: {
|
|
10
|
+
syntax: 'ecmascript',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
env: {
|
|
14
|
+
targets: 'defaults',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
}],
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
module: {
|
|
3
|
+
rules: [{
|
|
4
|
+
test: /\.js$/,
|
|
5
|
+
exclude: /node_modules/,
|
|
6
|
+
loader: 'builtin:swc-loader',
|
|
7
|
+
options: {
|
|
8
|
+
jsc: {
|
|
9
|
+
parser: {
|
|
10
|
+
syntax: 'ecmascript',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
env: {
|
|
14
|
+
targets: 'defaults',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
}],
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
module: {
|
|
3
|
+
rules: [{
|
|
4
|
+
test: /\.js$/,
|
|
5
|
+
exclude: /node_modules/,
|
|
6
|
+
loader: 'builtin:swc-loader',
|
|
7
|
+
options: {
|
|
8
|
+
jsc: {
|
|
9
|
+
parser: {
|
|
10
|
+
syntax: 'ecmascript',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
env: {
|
|
14
|
+
targets: 'defaults',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
}],
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {
|
|
2
|
+
template,
|
|
3
|
+
operator,
|
|
4
|
+
types,
|
|
5
|
+
} from 'putout';
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
isStringLiteral,
|
|
9
|
+
isIdentifier,
|
|
10
|
+
isObjectProperty,
|
|
11
|
+
identifier,
|
|
12
|
+
objectProperty,
|
|
13
|
+
} = types;
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
insertAfter,
|
|
17
|
+
replaceWith,
|
|
18
|
+
setLiteralValue,
|
|
19
|
+
} = operator;
|
|
20
|
+
|
|
21
|
+
const SWC_OPTIONS = `({
|
|
22
|
+
jsc: {
|
|
23
|
+
parser: {
|
|
24
|
+
syntax: 'ecmascript',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
env: {
|
|
28
|
+
targets: 'defaults',
|
|
29
|
+
},
|
|
30
|
+
})`;
|
|
31
|
+
|
|
32
|
+
export const report = () => `Use 'builtin:swc-loader' instead of 'babel-loader'`;
|
|
33
|
+
|
|
34
|
+
export const fix = ({path, optionsProp}) => {
|
|
35
|
+
setLiteralValue(path, 'builtin:swc-loader');
|
|
36
|
+
|
|
37
|
+
if (optionsProp) {
|
|
38
|
+
replaceWith(optionsProp.get('value'), template.ast(SWC_OPTIONS));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const {parentPath} = path;
|
|
43
|
+
const property = objectProperty(identifier('options'), template.ast(SWC_OPTIONS));
|
|
44
|
+
|
|
45
|
+
insertAfter(parentPath, property);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const traverse = ({push}) => ({
|
|
49
|
+
ObjectExpression(path) {
|
|
50
|
+
const properties = path.get('properties').filter(isObjectProperty);
|
|
51
|
+
|
|
52
|
+
for (const prop of properties) {
|
|
53
|
+
if (!isBabelLoader(prop))
|
|
54
|
+
continue;
|
|
55
|
+
|
|
56
|
+
const loaderPath = prop.get('value');
|
|
57
|
+
|
|
58
|
+
const optionsProp = properties.find(isOptions);
|
|
59
|
+
|
|
60
|
+
push({
|
|
61
|
+
path: loaderPath,
|
|
62
|
+
optionsProp,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const isLoader = (path) => isIdentifier(path.node.key, {
|
|
69
|
+
name: 'loader',
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const isOptions = (path) => isIdentifier(path.node.key, {
|
|
73
|
+
name: 'options',
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const isBabelLoader = (path) => isLoader(path) && isStringLiteral(path.node.value, {
|
|
77
|
+
value: 'babel-loader',
|
|
78
|
+
});
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as applyRspackImport from './apply-rspack-import/index.js';
|
|
2
|
+
import * as applySwcLoader from './apply-swc-loader/index.js';
|
|
3
|
+
import * as applyAssetType from './apply-asset-type/index.js';
|
|
4
|
+
import * as applyLibraryObject from './apply-library-object/index.js';
|
|
5
|
+
import * as applyGlobalDefine from './apply-global-define/index.js';
|
|
6
|
+
import * as removeWebpackbar from './remove-webpackbar/index.js';
|
|
7
|
+
|
|
8
|
+
export const rules = {
|
|
9
|
+
'apply-rspack-import': applyRspackImport,
|
|
10
|
+
'apply-swc-loader': applySwcLoader,
|
|
11
|
+
'apply-asset-type': applyAssetType,
|
|
12
|
+
'apply-library-object': applyLibraryObject,
|
|
13
|
+
'apply-global-define': applyGlobalDefine,
|
|
14
|
+
'remove-webpackbar': removeWebpackbar,
|
|
15
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-rspack",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
|
+
"description": "πPutout plugin add support of rspack",
|
|
7
|
+
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-rspack#readme",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"release": false,
|
|
10
|
+
"tag": false,
|
|
11
|
+
"changelog": false,
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/coderaiser/putout.git"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "madrun test",
|
|
18
|
+
"watch:test": "madrun watch:test",
|
|
19
|
+
"lint": "madrun lint",
|
|
20
|
+
"fresh:lint": "madrun fresh:lint",
|
|
21
|
+
"lint:fresh": "madrun lint:fresh",
|
|
22
|
+
"fix:lint": "madrun fix:lint",
|
|
23
|
+
"coverage": "madrun coverage",
|
|
24
|
+
"report": "madrun report"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"putout",
|
|
28
|
+
"putout-plugin",
|
|
29
|
+
"plugin",
|
|
30
|
+
"webpack",
|
|
31
|
+
"rspack"
|
|
32
|
+
],
|
|
33
|
+
"dependencies": {},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@putout/test": "^15.0.0",
|
|
36
|
+
"eslint": "^10.0.0",
|
|
37
|
+
"eslint-plugin-putout": "^31.0.0",
|
|
38
|
+
"madrun": "^13.0.0",
|
|
39
|
+
"nodemon": "^3.0.1",
|
|
40
|
+
"superc8": "^12.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"putout": ">=42"
|
|
44
|
+
},
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=22"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
}
|
|
52
|
+
}
|