|
1 |
| -/* |
2 |
| - * @FilePath: /webpack5-remove-use-strict-plugin/src/index.ts |
3 |
| - */ |
4 |
| - |
5 | 1 | /**
|
6 | 2 | * Webpack 5 plugin to remove "use strict" from the generated code
|
7 | 3 | * 专为 Webpack 5 设计的移除 "use strict" 插件
|
8 | 4 | */
|
9 | 5 |
|
10 | 6 | // 导入webpack类型
|
11 |
| -import type { Compiler, Compilation, WebpackPluginInstance, sources } from 'webpack'; |
| 7 | +import type { |
| 8 | + Compilation, |
| 9 | + Compiler, |
| 10 | + sources, |
| 11 | + WebpackPluginInstance, |
| 12 | +} from 'webpack' |
12 | 13 |
|
13 | 14 | // 匹配所有 "use strict" 和 'use strict' 声明(带或不带分号)
|
14 |
| -const USE_STRICT_REGEX = /(\'|\")use\s+strict(\'|\")\;?/gm; |
| 15 | +const USE_STRICT_REGEX = /('|")use\s+strict('|");?/g |
15 | 16 |
|
16 | 17 | class Webpack5RemoveUseStrictPlugin implements WebpackPluginInstance {
|
17 |
| - private readonly pluginName: string; |
| 18 | + private readonly pluginName: string |
18 | 19 |
|
19 | 20 | constructor() {
|
20 |
| - this.pluginName = "Webpack5RemoveUseStrictPlugin"; |
| 21 | + this.pluginName = 'Webpack5RemoveUseStrictPlugin' |
21 | 22 | }
|
22 | 23 |
|
23 | 24 | apply(compiler: Compiler): void {
|
24 | 25 | // 只使用 webpack 5 标准的 processAssets 钩子
|
25 |
| - compiler.hooks.compilation.tap(this.pluginName, (compilation: Compilation) => { |
26 |
| - // 注册资源处理钩子 - 使用 PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE 阶段 |
27 |
| - // 这个阶段在资源生成后,但在最终输出前执行 |
28 |
| - compilation.hooks.processAssets.tap( |
29 |
| - { |
30 |
| - name: this.pluginName, |
31 |
| - // 确保在资源优化阶段执行,以便处理所有经过其他插件处理的资源 |
32 |
| - stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, |
33 |
| - }, |
34 |
| - (assets: Record<string, sources.Source>) => { |
35 |
| - // 遍历所有资源 |
36 |
| - Object.keys(assets).forEach((filename) => { |
37 |
| - // 只处理 JavaScript 文件 |
38 |
| - if (filename.endsWith('.js')) { |
39 |
| - const asset = assets[filename]; |
| 26 | + compiler.hooks.compilation.tap( |
| 27 | + this.pluginName, |
| 28 | + (compilation: Compilation) => { |
| 29 | + // 注册资源处理钩子 - 使用 PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE 阶段 |
| 30 | + // 这个阶段在资源生成后,但在最终输出前执行 |
| 31 | + compilation.hooks.processAssets.tap( |
| 32 | + { |
| 33 | + name: this.pluginName, |
| 34 | + // 确保在资源优化阶段执行,以便处理所有经过其他插件处理的资源 |
| 35 | + stage: |
| 36 | + compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, |
| 37 | + }, |
| 38 | + (assets: Record<string, sources.Source>) => { |
| 39 | + // 遍历所有资源 |
| 40 | + Object.keys(assets).forEach((filename) => { |
| 41 | + // 只处理 JavaScript 文件 |
| 42 | + if (filename.endsWith('.js')) { |
| 43 | + const asset = assets[filename] |
40 | 44 |
|
41 |
| - // 获取资源内容 - 使用标准的 source() 方法获取字符串 |
42 |
| - let sourceCode: string; |
43 |
| - try { |
44 |
| - // 确保将 source() 返回值转为字符串 |
45 |
| - const source = asset.source(); |
46 |
| - sourceCode = typeof source === 'string' ? source : source.toString(); |
47 |
| - } catch (e) { |
48 |
| - console.error(`获取 ${filename} 源码时出错:`, e); |
49 |
| - return; |
50 |
| - } |
| 45 | + // 获取资源内容 - 使用标准的 source() 方法获取字符串 |
| 46 | + let sourceCode: string |
| 47 | + try { |
| 48 | + // 确保将 source() 返回值转为字符串 |
| 49 | + const source = asset.source() |
| 50 | + sourceCode = |
| 51 | + typeof source === 'string' ? source : source.toString() |
| 52 | + } catch (error) { |
| 53 | + console.error(`获取 ${filename} 源码时出错:`, error) |
| 54 | + return |
| 55 | + } |
51 | 56 |
|
52 |
| - if (sourceCode) { |
53 |
| - // 移除所有 "use strict" 声明 |
54 |
| - const newSource = sourceCode.replace(USE_STRICT_REGEX, ''); |
| 57 | + if (sourceCode) { |
| 58 | + // 移除所有 "use strict" 声明 |
| 59 | + const newSource = sourceCode.replaceAll(USE_STRICT_REGEX, '') |
55 | 60 |
|
56 |
| - // 使用 webpack-sources 的 RawSource 替换内容 |
57 |
| - const { RawSource } = compiler.webpack.sources; |
58 |
| - compilation.updateAsset( |
59 |
| - filename, |
60 |
| - new RawSource(newSource) |
61 |
| - ); |
| 61 | + // 使用 webpack-sources 的 RawSource 替换内容 |
| 62 | + const { RawSource } = compiler.webpack.sources |
| 63 | + compilation.updateAsset(filename, new RawSource(newSource)) |
| 64 | + } |
62 | 65 | }
|
63 |
| - } |
64 |
| - }); |
65 |
| - } |
66 |
| - ); |
67 |
| - }); |
| 66 | + }) |
| 67 | + }, |
| 68 | + ) |
| 69 | + }, |
| 70 | + ) |
68 | 71 | }
|
69 | 72 | }
|
70 | 73 |
|
71 |
| -export default Webpack5RemoveUseStrictPlugin; |
| 74 | +export default Webpack5RemoveUseStrictPlugin |
0 commit comments