@shuvi/toolpack 1.0.63 → 2.0.0-dev.6

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 (55) hide show
  1. package/lib/utils/tsCheckerRspackPlugin.d.ts +4 -0
  2. package/lib/utils/tsCheckerRspackPlugin.js +7 -0
  3. package/lib/webpack/config/base.rspack.d.ts +35 -0
  4. package/lib/webpack/config/base.rspack.js +329 -0
  5. package/lib/webpack/config/browser.rspack.d.ts +3 -0
  6. package/lib/webpack/config/browser.rspack.js +228 -0
  7. package/lib/webpack/config/{browser.d.ts → browser.webpack.d.ts} +1 -1
  8. package/lib/webpack/config/{browser.js → browser.webpack.js} +11 -11
  9. package/lib/webpack/config/index.d.ts +4 -4
  10. package/lib/webpack/config/index.js +4 -4
  11. package/lib/webpack/config/index.webpack.d.ts +4 -0
  12. package/lib/webpack/config/index.webpack.js +20 -0
  13. package/lib/webpack/config/node.rspack.d.ts +2 -0
  14. package/lib/webpack/config/node.rspack.js +52 -0
  15. package/lib/webpack/config/{node.d.ts → node.webpack.d.ts} +1 -1
  16. package/lib/webpack/config/{node.js → node.webpack.js} +7 -7
  17. package/lib/webpack/config/parts/external.d.ts +1 -1
  18. package/lib/webpack/config/parts/helpers.rspack.d.ts +49 -0
  19. package/lib/webpack/config/parts/helpers.rspack.js +106 -0
  20. package/lib/webpack/config/parts/{helpers.d.ts → helpers.webpack.d.ts} +2 -2
  21. package/lib/webpack/config/parts/{style.d.ts → style.rspack.d.ts} +1 -1
  22. package/lib/webpack/config/parts/style.rspack.js +294 -0
  23. package/lib/webpack/config/parts/style.webpack.d.ts +12 -0
  24. package/lib/webpack/config/parts/{style.js → style.webpack.js} +2 -2
  25. package/lib/webpack/dynamic-dll/dep/getModuleExports.d.ts +1 -1
  26. package/lib/webpack/index.d.ts +5 -3
  27. package/lib/webpack/index.js +13 -8
  28. package/lib/webpack/index.webpack.d.ts +5 -0
  29. package/lib/webpack/index.webpack.js +29 -0
  30. package/lib/webpack/plugins/chunk-names-plugin.d.ts +3 -0
  31. package/lib/webpack/plugins/chunk-names-plugin.js +3 -0
  32. package/lib/webpack/plugins/copy-file-plugin.rspack.d.ts +14 -0
  33. package/lib/webpack/plugins/copy-file-plugin.rspack.js +88 -0
  34. package/lib/webpack/plugins/module-replace-plugin/dynamic-loader-options-hack.d.ts +8 -0
  35. package/lib/webpack/plugins/module-replace-plugin/dynamic-loader-options-hack.js +16 -0
  36. package/lib/webpack/plugins/module-replace-plugin/plugin.rspack.d.ts +38 -0
  37. package/lib/webpack/plugins/module-replace-plugin/plugin.rspack.js +224 -0
  38. package/lib/webpack/plugins/module-replace-plugin/stub-loader.d.ts +26 -0
  39. package/lib/webpack/plugins/module-replace-plugin/stub-loader.js +47 -1
  40. package/lib/webpack/plugins/module-replace-plugin/stub-loader.rspack.d.ts +31 -0
  41. package/lib/webpack/plugins/module-replace-plugin/stub-loader.rspack.js +89 -0
  42. package/lib/webpack/plugins/require-cache-hot-reloader-plugin.rspack.d.ts +13 -0
  43. package/lib/webpack/plugins/require-cache-hot-reloader-plugin.rspack.js +47 -0
  44. package/lib/webpack/rspack.d.ts +197 -0
  45. package/lib/webpack/rspack.js +192 -0
  46. package/lib/webpack/types.rspack.d.ts +45 -0
  47. package/lib/webpack/{types.d.ts → types.webpack.d.ts} +1 -1
  48. package/lib/webpack/types.webpack.js +2 -0
  49. package/lib/webpack/webpack.d.ts +1 -1
  50. package/lib/webpack/webpack.js +2 -2
  51. package/package.json +10 -5
  52. /package/lib/webpack/config/{base.d.ts → base.webpack.d.ts} +0 -0
  53. /package/lib/webpack/config/{base.js → base.webpack.js} +0 -0
  54. /package/lib/webpack/config/parts/{helpers.js → helpers.webpack.js} +0 -0
  55. /package/lib/webpack/{types.js → types.rspack.js} +0 -0
@@ -0,0 +1,294 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.withStyle = withStyle;
30
+ const constants_1 = require("@shuvi/shared/constants");
31
+ const Rule_1 = __importDefault(require("rspack-chain/src/Rule"));
32
+ const core_1 = require("@rspack/core");
33
+ const loaderUtils = __importStar(require("loader-utils"));
34
+ const path = __importStar(require("path"));
35
+ const helpers_rspack_1 = require("./helpers.rspack");
36
+ const regexLikeIndexModule = /(?<!pages[\\/])index\.(scss|sass|css)$/;
37
+ function getCSSModuleLocalIdent(context, _, exportName, options) {
38
+ const relativePath = path
39
+ .relative(context.rootContext, context.resourcePath)
40
+ .replace(/\\+/g, '/');
41
+ // Generate a more meaningful name (parent folder) when the user names the
42
+ // file `index.css`.
43
+ const fileNameOrFolder = regexLikeIndexModule.test(relativePath)
44
+ ? '[folder]'
45
+ : '[name]';
46
+ // Generate a hash to make the class name unique.
47
+ const hash = loaderUtils.getHashDigest(Buffer.from(`filePath:${relativePath}#className:${exportName}`), 'md5', 'base64', 5);
48
+ // Have webpack interpolate the `[folder]` or `[name]` to its real value.
49
+ return (loaderUtils
50
+ .interpolateName(context, fileNameOrFolder + '_' + exportName + '__' + hash, options)
51
+ // Replace invalid symbols with underscores instead of escaping
52
+ // https://mathiasbynens.be/notes/css-escapes#identifiers-strings
53
+ .replace(/[^a-zA-Z0-9-_]/g, '_')
54
+ // "they cannot start with a digit, two hyphens, or a hyphen followed by a digit [sic]"
55
+ // https://www.w3.org/TR/CSS21/syndata.html#characters
56
+ .replace(/^(\d|--|-\d)/, '__$1'));
57
+ }
58
+ // style files regexes
59
+ const cssRegex = /\.css$/;
60
+ const cssModuleQueryRegex = /cssmodules/;
61
+ const sassRegex = /\.(scss|sass)$/;
62
+ function ssrCssRule({ test, resourceQuery, scss, lightningCss }) {
63
+ const rule = new Rule_1.default();
64
+ rule.test(test);
65
+ if (resourceQuery) {
66
+ rule.resourceQuery(resourceQuery);
67
+ }
68
+ if (lightningCss) {
69
+ rule
70
+ .use('lightningcss-loader')
71
+ .loader('@shuvi/lightningcss-loader')
72
+ .options({
73
+ sourceMap: false,
74
+ importLoaders: scss ? 1 : 0,
75
+ esModule: true,
76
+ modules: {
77
+ getLocalIdent: () => '[name]_[local]__[hash]',
78
+ exportOnlyLocals: true
79
+ }
80
+ });
81
+ }
82
+ else {
83
+ rule
84
+ .use('css-loader')
85
+ .loader(require.resolve('css-loader'))
86
+ .options({
87
+ sourceMap: false,
88
+ importLoaders: scss ? 1 : 0,
89
+ esModule: true,
90
+ modules: {
91
+ getLocalIdent: getCSSModuleLocalIdent,
92
+ exportOnlyLocals: true
93
+ }
94
+ });
95
+ }
96
+ if (scss) {
97
+ rule
98
+ .use('sass-loader')
99
+ .loader(require.resolve('sass-loader'))
100
+ .options({
101
+ sourceMap: false,
102
+ sassOptions: {
103
+ outputStyle: 'expanded'
104
+ }
105
+ });
106
+ }
107
+ return rule;
108
+ }
109
+ function cssRule({ publicPath, test, resourceQuery, cssModule, lightningCss, extractCss, sourceMap, scss }) {
110
+ const rule = new Rule_1.default();
111
+ rule.test(test);
112
+ if (resourceQuery) {
113
+ rule.resourceQuery(resourceQuery);
114
+ }
115
+ // A global CSS import always has side effects. Webpack will tree
116
+ // shake the CSS without this option if the issuer claims to have
117
+ // no side-effects.
118
+ // See https://github.com/webpack/webpack/issues/6571
119
+ rule.set('sideEffects', true);
120
+ if (extractCss) {
121
+ rule
122
+ .use('extract-loader')
123
+ .loader(core_1.rspack.CssExtractRspackPlugin.loader)
124
+ .options(Object.assign({}, (publicPath && (0, helpers_rspack_1.shouldUseRelativeAssetPaths)(publicPath)
125
+ ? {
126
+ // path relative to outdir from the generated css file
127
+ publicPath: '../../'
128
+ }
129
+ : {})));
130
+ }
131
+ else {
132
+ /**
133
+ * @unsupported Rspack does not support style-loader (inserts styles into DOM) in the same way as Webpack.
134
+ * TODO: Replace with Rspack's recommended approach for injecting styles in development.
135
+ */
136
+ rule
137
+ .use('style-loader')
138
+ .loader(require.resolve('style-loader'))
139
+ .options({
140
+ insert: new Function('element', `
141
+ // These elements should always exist. If they do not,
142
+ // this code should fail.
143
+ var anchorElement = document.querySelector("#${constants_1.DEV_STYLE_ANCHOR_ID}");
144
+ var parentNode = anchorElement.parentNode; // Normally <head>
145
+
146
+ // Each style tag should be placed right before our
147
+ // anchor. By inserting before and not after, we do not
148
+ // need to track the last inserted element.
149
+ parentNode.insertBefore(element, anchorElement);
150
+ `),
151
+ esModule: true
152
+ });
153
+ }
154
+ if (lightningCss) {
155
+ rule
156
+ .use('lightningcss-loader')
157
+ .loader('@shuvi/lightningcss-loader')
158
+ .options(Object.assign({ sourceMap, importLoaders: scss ? 2 : 1, esModule: true }, (cssModule && {
159
+ modules: {
160
+ getLocalIdent: () => '[name]_[local]__[hash]',
161
+ exportOnlyLocals: false
162
+ }
163
+ })));
164
+ }
165
+ else {
166
+ rule
167
+ .use('css-loader')
168
+ .loader(require.resolve('css-loader'))
169
+ .options(Object.assign({ sourceMap, importLoaders: scss ? 2 : 1, esModule: true }, (cssModule && {
170
+ modules: {
171
+ getLocalIdent: getCSSModuleLocalIdent,
172
+ exportOnlyLocals: false
173
+ }
174
+ })));
175
+ rule
176
+ .use('postcss-loader')
177
+ .loader(require.resolve('postcss-loader'))
178
+ .options({
179
+ sourceMap,
180
+ postcssOptions: {
181
+ plugins: [
182
+ // Make Flexbox behave like the spec cross-browser.
183
+ require('postcss-flexbugs-fixes'),
184
+ // Run Autoprefixer and compile new CSS features.
185
+ require('postcss-preset-env')({
186
+ autoprefixer: {
187
+ flexbox: 'no-2009'
188
+ },
189
+ stage: 3
190
+ })
191
+ ]
192
+ }
193
+ });
194
+ }
195
+ if (scss) {
196
+ rule
197
+ .use('sass-loader')
198
+ .loader(require.resolve('sass-loader'))
199
+ .options({
200
+ sourceMap,
201
+ sassOptions: {
202
+ outputStyle: 'expanded'
203
+ }
204
+ });
205
+ }
206
+ return rule;
207
+ }
208
+ function withStyle(chain, { extractCss, sourceMap, ssr, publicPath, lightningCss, filename, chunkFilename }) {
209
+ const oneOfs = chain.module.rule('main').oneOfs;
210
+ if (ssr) {
211
+ oneOfs.set('css-module',
212
+ // @ts-ignore
213
+ ssrCssRule({
214
+ test: cssRegex,
215
+ resourceQuery: cssModuleQueryRegex,
216
+ scss: false,
217
+ lightningCss
218
+ }).after('js'));
219
+ oneOfs.set('scss-module',
220
+ // @ts-ignore
221
+ ssrCssRule({
222
+ test: sassRegex,
223
+ resourceQuery: cssModuleQueryRegex,
224
+ scss: true,
225
+ lightningCss
226
+ }).after('css-module'));
227
+ // ignore noraml css module
228
+ const ignoreRule = new Rule_1.default();
229
+ ignoreRule
230
+ .test([cssRegex, sassRegex])
231
+ .use('ignore-loader')
232
+ .loader(require.resolve('ignore-loader'))
233
+ .end()
234
+ .after('scss-module');
235
+ // @ts-ignore
236
+ oneOfs.set('ignore', ignoreRule);
237
+ return chain;
238
+ }
239
+ if (extractCss) {
240
+ chain.plugin('mini-css-extract-plugin').use(core_1.rspack.CssExtractRspackPlugin, [
241
+ {
242
+ filename,
243
+ chunkFilename
244
+ }
245
+ ]);
246
+ }
247
+ oneOfs.set('css-module',
248
+ // @ts-ignore
249
+ cssRule({
250
+ test: cssRegex,
251
+ resourceQuery: cssModuleQueryRegex,
252
+ cssModule: true,
253
+ lightningCss,
254
+ scss: false,
255
+ extractCss,
256
+ sourceMap,
257
+ publicPath
258
+ }).after('js'));
259
+ oneOfs.set('css',
260
+ // @ts-ignore
261
+ cssRule({
262
+ test: cssRegex,
263
+ cssModule: false,
264
+ lightningCss,
265
+ scss: false,
266
+ extractCss,
267
+ sourceMap,
268
+ publicPath
269
+ }).after('css-module'));
270
+ oneOfs.set('scss-module',
271
+ // @ts-ignore
272
+ cssRule({
273
+ test: sassRegex,
274
+ resourceQuery: cssModuleQueryRegex,
275
+ cssModule: true,
276
+ lightningCss,
277
+ scss: true,
278
+ extractCss,
279
+ sourceMap,
280
+ publicPath
281
+ }).after('css'));
282
+ oneOfs.set('scss',
283
+ // @ts-ignore
284
+ cssRule({
285
+ test: sassRegex,
286
+ cssModule: false,
287
+ lightningCss,
288
+ scss: true,
289
+ extractCss,
290
+ sourceMap,
291
+ publicPath
292
+ }).after('scss-module'));
293
+ return chain;
294
+ }
@@ -0,0 +1,12 @@
1
+ import { WebpackChain as Config } from '../base.webpack';
2
+ interface StyleOptions {
3
+ filename: string;
4
+ chunkFilename: string;
5
+ publicPath?: string;
6
+ extractCss?: boolean;
7
+ sourceMap?: boolean;
8
+ ssr?: boolean;
9
+ lightningCss?: boolean;
10
+ }
11
+ export declare function withStyle(chain: Config, { extractCss, sourceMap, ssr, publicPath, lightningCss, filename, chunkFilename }: StyleOptions): Config;
12
+ export {};
@@ -32,7 +32,7 @@ const Rule_1 = __importDefault(require("webpack-chain/src/Rule"));
32
32
  const mini_css_extract_plugin_1 = __importDefault(require("mini-css-extract-plugin"));
33
33
  const loaderUtils = __importStar(require("loader-utils"));
34
34
  const path = __importStar(require("path"));
35
- const helpers_1 = require("./helpers");
35
+ const helpers_webpack_1 = require("./helpers.webpack");
36
36
  const regexLikeIndexModule = /(?<!pages[\\/])index\.(scss|sass|css)$/;
37
37
  function getCSSModuleLocalIdent(context, _, exportName, options) {
38
38
  const relativePath = path
@@ -121,7 +121,7 @@ function cssRule({ publicPath, test, resourceQuery, cssModule, lightningCss, ext
121
121
  rule
122
122
  .use('extract-loader')
123
123
  .loader(mini_css_extract_plugin_1.default.loader)
124
- .options(Object.assign({}, (publicPath && (0, helpers_1.shouldUseRelativeAssetPaths)(publicPath)
124
+ .options(Object.assign({}, (publicPath && (0, helpers_webpack_1.shouldUseRelativeAssetPaths)(publicPath)
125
125
  ? {
126
126
  // path relative to outdir from the generated css file
127
127
  publicPath: '../../'
@@ -2,6 +2,6 @@ export declare function getModuleExports({ content, libraryPath }: {
2
2
  libraryPath: string;
3
3
  content: string;
4
4
  }): Promise<{
5
- exports: any;
5
+ exports: string[] | readonly import("es-module-lexer", { with: { "resolution-mode": "import" } }).ExportSpecifier[];
6
6
  isCJS: boolean;
7
7
  }>;
@@ -1,5 +1,7 @@
1
- export * from './webpack';
1
+ export * from './rspack';
2
2
  /**
3
- * resolve webpack sub module from the same webpack module
3
+ * resolve rspack sub module from the same rspack module
4
+ * @unsupported Rspack does not support submodule resolution like Webpack.
5
+ * TODO: Remove or replace after Rspack support is available.
4
6
  */
5
- export declare function resolveWebpackModule(path: string): any;
7
+ export declare function resolveRspackModule(path: string): any;
@@ -14,16 +14,21 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.resolveWebpackModule = resolveWebpackModule;
17
+ exports.resolveRspackModule = resolveRspackModule;
18
18
  const path_1 = require("path");
19
- const webpackResolveContext = (0, path_1.join)((0, path_1.dirname)(require.resolve('webpack/package.json')), '../');
20
- __exportStar(require("./webpack"), exports);
19
+ const rspackResolveContext = (0, path_1.join)((0, path_1.dirname)(require.resolve('@rspack/core/package.json')), '../');
20
+ __exportStar(require("./rspack"), exports);
21
21
  /**
22
- * resolve webpack sub module from the same webpack module
22
+ * resolve rspack sub module from the same rspack module
23
+ * @unsupported Rspack does not support submodule resolution like Webpack.
24
+ * TODO: Remove or replace after Rspack support is available.
23
25
  */
24
- function resolveWebpackModule(path) {
25
- if (!path.startsWith('webpack/')) {
26
- console.error('path need startWith "webpack/" to resolve webpack module');
26
+ function resolveRspackModule(path) {
27
+ if (!path.startsWith('@rspack/core/')) {
28
+ console.error('path need startWith "@rspack/core/" to resolve rspack module');
27
29
  }
28
- return require(`${webpackResolveContext}/${path}`);
30
+ const p = require(`${rspackResolveContext}/${path}`);
31
+ // debug
32
+ console.debug(`[rspack][resolveRspackModule] ${path} -> ${p}`);
33
+ return p;
29
34
  }
@@ -0,0 +1,5 @@
1
+ export * from './webpack';
2
+ /**
3
+ * resolve webpack sub module from the same webpack module
4
+ */
5
+ export declare function resolveWebpackModule(path: string): any;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.resolveWebpackModule = resolveWebpackModule;
18
+ const path_1 = require("path");
19
+ const webpackResolveContext = (0, path_1.join)((0, path_1.dirname)(require.resolve('webpack/package.json')), '../');
20
+ __exportStar(require("./webpack"), exports);
21
+ /**
22
+ * resolve webpack sub module from the same webpack module
23
+ */
24
+ function resolveWebpackModule(path) {
25
+ if (!path.startsWith('webpack/')) {
26
+ console.error('path need startWith "webpack/" to resolve webpack module');
27
+ }
28
+ return require(`${webpackResolveContext}/${path}`);
29
+ }
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Unused
3
+ */
1
4
  import { Compiler } from 'webpack';
2
5
  export default class ChunkNamesPlugin {
3
6
  apply(compiler: Compiler): void;
@@ -1,5 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Unused
5
+ */
3
6
  const webpack_1 = require("webpack");
4
7
  // This plugin mirrors webpack 3 `filename` and `chunkfilename` behavior
5
8
  // This fixes https://github.com/webpack/webpack/issues/6598
@@ -0,0 +1,14 @@
1
+ import * as Rspack from '../../webpack';
2
+ export declare class CopyFilePlugin {
3
+ private filePath;
4
+ private name;
5
+ private cacheKey;
6
+ private info?;
7
+ constructor({ filePath, cacheKey, name, info }: {
8
+ filePath: string;
9
+ cacheKey: string;
10
+ name: string;
11
+ info?: object;
12
+ });
13
+ apply(compiler: Rspack.Compiler): void;
14
+ }
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.CopyFilePlugin = void 0;
36
+ const fs_1 = require("fs");
37
+ const Rspack = __importStar(require("../../webpack"));
38
+ const loaderUtils = __importStar(require("loader-utils"));
39
+ const PLUGIN_NAME = 'CopyFilePlugin';
40
+ class CopyFilePlugin {
41
+ constructor({ filePath, cacheKey, name, info }) {
42
+ this.filePath = filePath;
43
+ this.cacheKey = cacheKey;
44
+ this.name = name;
45
+ this.info = info;
46
+ }
47
+ apply(compiler) {
48
+ // compiler.options.output.filename
49
+ compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
50
+ const cache = compilation.getCache('CopyFilePlugin');
51
+ const hook = compilation.hooks.processAssets;
52
+ hook.tapPromise({
53
+ name: PLUGIN_NAME,
54
+ stage: Rspack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
55
+ }, () => __awaiter(this, void 0, void 0, function* () {
56
+ var _a;
57
+ if (cache) {
58
+ const cachedResult = yield cache.getPromise(this.filePath, this.cacheKey);
59
+ if (cachedResult) {
60
+ const { file, source } = cachedResult;
61
+ compilation.emitAsset(file, source, Object.assign({}, this.info));
62
+ return;
63
+ }
64
+ }
65
+ const content = yield fs_1.promises.readFile(this.filePath, 'utf8');
66
+ const hash = loaderUtils.interpolateName({
67
+ // we only care about hash, so use a fake path is ok
68
+ resourcePath: '/fake/path'
69
+ }, '[hash:8]', {
70
+ content
71
+ });
72
+ const file = compilation.getAssetPath(((_a = compiler.options.output.filename) === null || _a === void 0 ? void 0 : _a.toString()) || this.name, {
73
+ contentHash: hash,
74
+ chunk: { id: this.name, name: this.name }
75
+ });
76
+ const source = new Rspack.sources.RawSource(content);
77
+ if (cache) {
78
+ yield cache.storePromise(this.filePath, this.cacheKey, {
79
+ file,
80
+ source
81
+ });
82
+ }
83
+ compilation.emitAsset(file, source, Object.assign({}, this.info));
84
+ }));
85
+ });
86
+ }
87
+ }
88
+ exports.CopyFilePlugin = CopyFilePlugin;
@@ -0,0 +1,8 @@
1
+ declare const _default: {
2
+ setData(key: string, val: any): void;
3
+ /**
4
+ * return the value and delete the key
5
+ */
6
+ pop(key: string): any;
7
+ };
8
+ export default _default;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const dataMap = new Map();
4
+ exports.default = {
5
+ setData(key, val) {
6
+ dataMap.set(key, val);
7
+ },
8
+ /**
9
+ * return the value and delete the key
10
+ */
11
+ pop(key) {
12
+ const val = dataMap.get(key);
13
+ dataMap.delete(key);
14
+ return val;
15
+ }
16
+ };
@@ -0,0 +1,38 @@
1
+ import * as Rspack from '../../webpack';
2
+ /**
3
+ * Rspack plugin interface
4
+ */
5
+ interface RspackPlugin {
6
+ apply(compiler: Rspack.Compiler): void;
7
+ }
8
+ export type ConfigItem = {
9
+ resourceQuery: Function | RegExp;
10
+ module: string;
11
+ };
12
+ export interface ModuleReplacePluginOptions {
13
+ modules: ConfigItem[];
14
+ }
15
+ export interface Loader {
16
+ loader: string;
17
+ options: Record<string, any>;
18
+ }
19
+ export interface ModuleInfo {
20
+ action: typeof ModuleAction[keyof typeof ModuleAction];
21
+ compiler: Rspack.Compiler;
22
+ replacedModule: string;
23
+ loaders: Loader[];
24
+ }
25
+ declare const ModuleAction: {
26
+ readonly REPLACE: "replace";
27
+ readonly RESTORE: "restore";
28
+ };
29
+ export default class ModuleReplacePlugin implements RspackPlugin {
30
+ private _options;
31
+ static restoreModule(id: string): false | Promise<any>;
32
+ static replaceModule(id: string): false | void;
33
+ constructor(options: Partial<ModuleReplacePluginOptions>);
34
+ apply(compiler: Rspack.Compiler): void;
35
+ private _handleBuildModule;
36
+ private _collectModules;
37
+ }
38
+ export {};