@putout/plugin-rspack 0.0.0-stage β†’ 1.0.1

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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +220 -2
  3. package/lib/apply-asset-type/fixture/no-url-loader.js +13 -0
  4. package/lib/apply-asset-type/fixture/url-loader-fix.js +13 -0
  5. package/lib/apply-asset-type/fixture/url-loader.js +13 -0
  6. package/lib/apply-asset-type/fixture/use-array.js +9 -0
  7. package/lib/apply-asset-type/index.js +87 -0
  8. package/lib/apply-global-define/fixture/define-other-fix.js +14 -0
  9. package/lib/apply-global-define/fixture/define-other.js +11 -0
  10. package/lib/apply-global-define/fixture/defined-destructured.js +12 -0
  11. package/lib/apply-global-define/fixture/defined.js +10 -0
  12. package/lib/apply-global-define/fixture/export-not-object.js +1 -0
  13. package/lib/apply-global-define/fixture/global-define-fix.js +13 -0
  14. package/lib/apply-global-define/fixture/global-define.js +8 -0
  15. package/lib/apply-global-define/fixture/not-config.js +3 -0
  16. package/lib/apply-global-define/fixture/other-import-fix.js +9 -0
  17. package/lib/apply-global-define/fixture/other-import.js +5 -0
  18. package/lib/apply-global-define/fixture/output-only.js +5 -0
  19. package/lib/apply-global-define/fixture/plugins-fix.js +13 -0
  20. package/lib/apply-global-define/fixture/plugins-variable.js +6 -0
  21. package/lib/apply-global-define/fixture/plugins.js +10 -0
  22. package/lib/apply-global-define/fixture/target-node-array.js +4 -0
  23. package/lib/apply-global-define/fixture/target-node.js +4 -0
  24. package/lib/apply-global-define/fixture/target-web-fix.js +9 -0
  25. package/lib/apply-global-define/fixture/target-web.js +4 -0
  26. package/lib/apply-global-define/index.js +120 -0
  27. package/lib/apply-library-object/fixture/library-export-not-string.js +7 -0
  28. package/lib/apply-library-object/fixture/library-object.js +9 -0
  29. package/lib/apply-library-object/fixture/library-target-fix.js +10 -0
  30. package/lib/apply-library-object/fixture/library-target-no-export-fix.js +9 -0
  31. package/lib/apply-library-object/fixture/library-target-no-export.js +7 -0
  32. package/lib/apply-library-object/fixture/library-target-no-library.js +6 -0
  33. package/lib/apply-library-object/fixture/library-target-not-string.js +6 -0
  34. package/lib/apply-library-object/fixture/library-target.js +8 -0
  35. package/lib/apply-library-object/fixture/no-library-target.js +10 -0
  36. package/lib/apply-library-object/index.js +67 -0
  37. package/lib/apply-rspack-import/fixture/no-webpack.js +3 -0
  38. package/lib/apply-rspack-import/fixture/webpack-import-fix.js +7 -0
  39. package/lib/apply-rspack-import/fixture/webpack-import.js +7 -0
  40. package/lib/apply-rspack-import/index.js +12 -0
  41. package/lib/apply-swc-loader/fixture/babel-loader-fix.js +19 -0
  42. package/lib/apply-swc-loader/fixture/babel-loader-options-fix.js +19 -0
  43. package/lib/apply-swc-loader/fixture/babel-loader-options.js +12 -0
  44. package/lib/apply-swc-loader/fixture/babel-loader.js +9 -0
  45. package/lib/apply-swc-loader/fixture/no-babel-loader.js +19 -0
  46. package/lib/apply-swc-loader/index.js +78 -0
  47. package/lib/index.js +15 -0
  48. package/lib/remove-webpackbar/fixture/no-webpackbar.js +7 -0
  49. package/lib/remove-webpackbar/fixture/webpackbar-fix.js +7 -0
  50. package/lib/remove-webpackbar/fixture/webpackbar.js +9 -0
  51. package/lib/remove-webpackbar/index.js +6 -0
  52. 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,221 @@
1
- # Temporary Holding Version
1
+ # @putout/plugin-rspack [![NPM version][NPMIMGURL]][NPMURL]
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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
+ ## Install
9
+
10
+ ```
11
+ npm i putout @putout/plugin-rspack -D
12
+ ```
13
+
14
+ ## Rules
15
+
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);
22
+
23
+ ## Config
24
+
25
+ ```json
26
+ {
27
+ "rules": {
28
+ "rspack/apply-asset-type": "on",
29
+ "rspack/apply-global-define": "on",
30
+ "rspack/apply-library-object": "on",
31
+ "rspack/apply-rspack-import": "on",
32
+ "rspack/apply-swc-loader": "on",
33
+ "rspack/remove-webpackbar": "on"
34
+ }
35
+ }
36
+ ```
37
+
38
+ ## apply-asset-type
39
+
40
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/6d4152d33dd67f39ef4a1d56769dc22b/a6d7b603f326717a462c01fb9568e4f989ca7af7).
41
+
42
+ ### ❌ Example of incorrect code
43
+
44
+ ```js
45
+ export default {
46
+ module: {
47
+ rules: [{
48
+ test: /\.(png|gif|svg)$/,
49
+ use: {
50
+ loader: 'url-loader',
51
+ options: {
52
+ limit: 50_000,
53
+ },
54
+ },
55
+ }],
56
+ },
57
+ };
58
+ ```
59
+
60
+ ### βœ… Example of correct code
61
+
62
+ ```js
63
+ export default {
64
+ module: {
65
+ rules: [{
66
+ test: /\.(png|gif|svg)$/,
67
+ type: 'asset',
68
+ parser: {
69
+ dataUrlCondition: {
70
+ maxSize: 50_000,
71
+ },
72
+ },
73
+ }],
74
+ },
75
+ };
76
+ ```
77
+
78
+ ## apply-global-define
79
+
80
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/627e3b66b31f9490499dcd611b535db5/8c20fc89fbe02011c38ffb850486db0452ffdcfa).
81
+
82
+ 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.
83
+
84
+ Use `DefinePlugin` to replace `global` with `globalThis`. Skipped for `target: 'node'`.
85
+
86
+ ### ❌ Example of incorrect code
87
+
88
+ ```js
89
+ export default {
90
+ entry: './client/index.js',
91
+ };
92
+ ```
93
+
94
+ ### βœ… Example of correct code
95
+
96
+ ```js
97
+ import {rspack} from '@rspack/core';
98
+
99
+ export default {
100
+ entry: './client/index.js',
101
+ plugins: [
102
+ new rspack.DefinePlugin({
103
+ global: 'globalThis',
104
+ }),
105
+ ],
106
+ };
107
+ ```
108
+
109
+ ## apply-library-object
110
+
111
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9b85da3e385d1c254f4103db8952323/1b7c7c9e8568bba9070469ced671c1dd5022bed3).
112
+
113
+ ### ❌ Example of incorrect code
114
+
115
+ ```js
116
+ export default {
117
+ output: {
118
+ library: 'Deepword',
119
+ libraryTarget: 'var',
120
+ libraryExport: 'default',
121
+ },
122
+ };
123
+ ```
124
+
125
+ ### βœ… Example of correct code
126
+
127
+ ```js
128
+ export default {
129
+ output: {
130
+ library: {
131
+ name: 'Deepword',
132
+ type: 'var',
133
+ export: 'default',
134
+ },
135
+ },
136
+ };
137
+ ```
138
+
139
+ ## apply-rspack-import
140
+
141
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/6af7c06cecb5e2b5717271dddd783fcf/fb2b92aa42bb5c1dadd2d5d5fb98ca06d2b6ee0d).
142
+
143
+ ### ❌ Example of incorrect code
144
+
145
+ ```js
146
+ import webpack from 'webpack';
147
+
148
+ const {EnvironmentPlugin} = webpack;
149
+ ```
150
+
151
+ ### βœ… Example of correct code
152
+
153
+ ```js
154
+ import {rspack} from '@rspack/core';
155
+
156
+ const {EnvironmentPlugin} = rspack;
157
+ ```
158
+
159
+ ## apply-swc-loader
160
+
161
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d26a479c7f677bcfe74f9f946c514983/c88686d689396c1e47b22d8aee24e737eff7e24f).
162
+
163
+ ### ❌ Example of incorrect code
164
+
165
+ ```js
166
+ export default {
167
+ module: {
168
+ rules: [{
169
+ test: /\.js$/,
170
+ loader: 'babel-loader',
171
+ }],
172
+ },
173
+ };
174
+ ```
175
+
176
+ ### βœ… Example of correct code
177
+
178
+ ```js
179
+ export default {
180
+ module: {
181
+ rules: [{
182
+ test: /\.js$/,
183
+ loader: 'builtin:swc-loader',
184
+ options: {
185
+ jsc: {
186
+ parser: {
187
+ syntax: 'ecmascript',
188
+ },
189
+ },
190
+ env: {
191
+ targets: 'defaults',
192
+ },
193
+ },
194
+ }],
195
+ },
196
+ };
197
+ ```
198
+
199
+ ## remove-webpackbar
200
+
201
+ ### ❌ Example of incorrect code
202
+
203
+ ```js
204
+ import WebpackBar from 'webpackbar';
205
+
206
+ export default {
207
+ plugins: [new WebpackBar()],
208
+ };
209
+ ```
210
+
211
+ ### βœ… Example of correct code
212
+
213
+ ```js
214
+ export default {
215
+ plugins: [],
216
+ };
217
+ ```
218
+
219
+ ## License
220
+
221
+ MIT
@@ -0,0 +1,13 @@
1
+ export default {
2
+ module: {
3
+ rules: [{
4
+ test: /\.(png|gif|svg|woff|woff2|eot|ttf)$/,
5
+ type: 'asset',
6
+ parser: {
7
+ dataUrlCondition: {
8
+ maxSize: 50_000,
9
+ },
10
+ },
11
+ }],
12
+ },
13
+ };
@@ -0,0 +1,13 @@
1
+ export default {
2
+ module: {
3
+ rules: [{
4
+ test: /\.(png|gif|svg|woff|woff2|eot|ttf)$/,
5
+ type: 'asset',
6
+ parser: {
7
+ dataUrlCondition: {
8
+ maxSize: 50_000,
9
+ },
10
+ },
11
+ }],
12
+ },
13
+ };
@@ -0,0 +1,13 @@
1
+ export default {
2
+ module: {
3
+ rules: [{
4
+ test: /\.(png|gif|svg|woff|woff2|eot|ttf)$/,
5
+ use: {
6
+ loader: 'url-loader',
7
+ options: {
8
+ limit: 50_000,
9
+ },
10
+ },
11
+ }],
12
+ },
13
+ };
@@ -0,0 +1,9 @@
1
+ const rules = clean([{
2
+ test: /\.css$/,
3
+ use: [
4
+ 'style-loader',
5
+ 'css-loader',
6
+ 'clean-css-loader',
7
+ ],
8
+ }]);
9
+
@@ -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,11 @@
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
+ ],
11
+ };
@@ -0,0 +1,12 @@
1
+ import {rspack} from '@rspack/core';
2
+
3
+ const {DefinePlugin} = rspack;
4
+
5
+ export default {
6
+ entry: './client/index.js',
7
+ plugins: [
8
+ new DefinePlugin({
9
+ global: 'globalThis',
10
+ }),
11
+ ],
12
+ };
@@ -0,0 +1,10 @@
1
+ import {rspack} from '@rspack/core';
2
+
3
+ export default {
4
+ entry: './client/index.js',
5
+ plugins: [
6
+ new rspack.DefinePlugin({
7
+ global: 'globalThis',
8
+ }),
9
+ ],
10
+ };
@@ -0,0 +1 @@
1
+ export default config;
@@ -0,0 +1,13 @@
1
+ import {rspack} from '@rspack/core';
2
+
3
+ export default {
4
+ entry: {
5
+ edward: './client/edward.js',
6
+ },
7
+ output: {
8
+ filename: '[name].js',
9
+ },
10
+ plugins: [new rspack.DefinePlugin({
11
+ global: 'globalThis',
12
+ })],
13
+ };
@@ -0,0 +1,8 @@
1
+ export default {
2
+ entry: {
3
+ edward: './client/edward.js',
4
+ },
5
+ output: {
6
+ filename: '[name].js',
7
+ },
8
+ };
@@ -0,0 +1,3 @@
1
+ export default {
2
+ name: 'hello',
3
+ };
@@ -0,0 +1,9 @@
1
+ import {rspack} from '@rspack/core';
2
+ import path from 'node:path';
3
+
4
+ export default {
5
+ entry: './client/index.js',
6
+ plugins: [new rspack.DefinePlugin({
7
+ global: 'globalThis',
8
+ })],
9
+ };
@@ -0,0 +1,5 @@
1
+ import path from 'node:path';
2
+
3
+ export default {
4
+ entry: './client/index.js',
5
+ };
@@ -0,0 +1,5 @@
1
+ export default {
2
+ output: {
3
+ filename: '[name].js',
4
+ },
5
+ };
@@ -0,0 +1,13 @@
1
+ import {rspack} from '@rspack/core';
2
+
3
+ export default {
4
+ entry: {
5
+ deepword: './client/index.js',
6
+ },
7
+ plugins: [
8
+ new rspack.EnvironmentPlugin(['NODE_ENV']),
9
+ new rspack.DefinePlugin({
10
+ global: 'globalThis',
11
+ }),
12
+ ],
13
+ };
@@ -0,0 +1,6 @@
1
+ const plugins = [];
2
+
3
+ export default {
4
+ entry: './client/index.js',
5
+ plugins,
6
+ };
@@ -0,0 +1,10 @@
1
+ import {rspack} from '@rspack/core';
2
+
3
+ export default {
4
+ entry: {
5
+ deepword: './client/index.js',
6
+ },
7
+ plugins: [
8
+ new rspack.EnvironmentPlugin(['NODE_ENV']),
9
+ ],
10
+ };
@@ -0,0 +1,4 @@
1
+ export default {
2
+ target: ['web', 'node'],
3
+ entry: './server/index.js',
4
+ };
@@ -0,0 +1,4 @@
1
+ export default {
2
+ target: 'node',
3
+ entry: './server/index.js',
4
+ };
@@ -0,0 +1,9 @@
1
+ import {rspack} from '@rspack/core';
2
+
3
+ export default {
4
+ target: ['web'],
5
+ entry: './client/index.js',
6
+ plugins: [new rspack.DefinePlugin({
7
+ global: 'globalThis',
8
+ })],
9
+ };
@@ -0,0 +1,4 @@
1
+ export default {
2
+ target: ['web'],
3
+ entry: './client/index.js',
4
+ };
@@ -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,7 @@
1
+ export default {
2
+ output: {
3
+ library: 'edward',
4
+ libraryTarget: 'var',
5
+ libraryExport: exportName,
6
+ },
7
+ };
@@ -0,0 +1,9 @@
1
+ export default {
2
+ output: {
3
+ library: {
4
+ name: 'edward',
5
+ type: 'var',
6
+ },
7
+ libraryTarget: 'var',
8
+ },
9
+ };
@@ -0,0 +1,10 @@
1
+ export default {
2
+ output: {
3
+ library: {
4
+ name: 'Fileop',
5
+ type: 'var',
6
+ export: 'default',
7
+ },
8
+ filename: '[name].js',
9
+ },
10
+ };
@@ -0,0 +1,9 @@
1
+ export default {
2
+ output: {
3
+ library: {
4
+ name: 'Fileop',
5
+ type: 'var',
6
+ },
7
+ filename: '[name].js',
8
+ },
9
+ };
@@ -0,0 +1,7 @@
1
+ export default {
2
+ output: {
3
+ library: 'Fileop',
4
+ libraryTarget: 'var',
5
+ filename: '[name].js',
6
+ },
7
+ };
@@ -0,0 +1,6 @@
1
+ export default {
2
+ output: {
3
+ libraryTarget: 'var',
4
+ libraryExport: 'default',
5
+ },
6
+ };
@@ -0,0 +1,6 @@
1
+ export default {
2
+ output: {
3
+ library: 'edward',
4
+ libraryTarget: type,
5
+ },
6
+ };
@@ -0,0 +1,8 @@
1
+ export default {
2
+ output: {
3
+ library: 'Fileop',
4
+ libraryTarget: 'var',
5
+ libraryExport: 'default',
6
+ filename: '[name].js',
7
+ },
8
+ };
@@ -0,0 +1,10 @@
1
+ export default {
2
+ output: {
3
+ library: {
4
+ name: 'Fileop',
5
+ type: 'var',
6
+ export: 'default',
7
+ },
8
+ filename: '[name].js',
9
+ },
10
+ };
@@ -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,3 @@
1
+ import {rspack} from '@rspack/core';
2
+
3
+ const {EnvironmentPlugin} = rspack;
@@ -0,0 +1,7 @@
1
+ import {rspack} from '@rspack/core';
2
+
3
+ const {
4
+ NormalModuleReplacementPlugin,
5
+ EnvironmentPlugin,
6
+ ProvidePlugin,
7
+ } = rspack;
@@ -0,0 +1,7 @@
1
+ import webpack from 'webpack';
2
+
3
+ const {
4
+ NormalModuleReplacementPlugin,
5
+ EnvironmentPlugin,
6
+ ProvidePlugin,
7
+ } = webpack;
@@ -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,12 @@
1
+ export default {
2
+ module: {
3
+ rules: [{
4
+ test: /\.js$/,
5
+ exclude: /node_modules/,
6
+ loader: 'babel-loader',
7
+ options: {
8
+ babelrc: true,
9
+ },
10
+ }],
11
+ },
12
+ };
@@ -0,0 +1,9 @@
1
+ export default {
2
+ module: {
3
+ rules: [{
4
+ test: /\.js$/,
5
+ exclude: /node_modules/,
6
+ loader: 'babel-loader',
7
+ }],
8
+ },
9
+ };
@@ -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
+ };
@@ -0,0 +1,7 @@
1
+ import webpack from 'webpack';
2
+
3
+ export default {
4
+ plugins: [
5
+ new webpack.EnvironmentPlugin(['NODE_ENV']),
6
+ ],
7
+ };
@@ -0,0 +1,7 @@
1
+ import webpack from 'webpack';
2
+
3
+ export default {
4
+ plugins: [
5
+ new webpack.EnvironmentPlugin(['NODE_ENV']),
6
+ ],
7
+ };
@@ -0,0 +1,9 @@
1
+ import webpack from 'webpack';
2
+ import WebpackBar from 'webpackbar';
3
+
4
+ export default {
5
+ plugins: [
6
+ new WebpackBar(),
7
+ new webpack.EnvironmentPlugin(['NODE_ENV']),
8
+ ],
9
+ };
@@ -0,0 +1,6 @@
1
+ export const report = () => `Remove 'WebpackBar'`;
2
+
3
+ export const replace = () => ({
4
+ 'import WebpackBar from "webpackbar"': '',
5
+ 'new WebpackBar()': '',
6
+ });
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@putout/plugin-rspack",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
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
+ }