Skip to content

Commit 30c25ce

Browse files
committed
chore: 更新 Node.js 版本要求至 20.18.0,修正代码格式和注释
1 parent 76e0a6f commit 30c25ce

File tree

4 files changed

+61
-58
lines changed

4 files changed

+61
-58
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,29 @@ pnpm add --save-dev @winner-fed/webpack5-remove-use-strict-plugin
3232
### CommonJS 方式
3333

3434
```js
35-
const Webpack5RemoveUseStrictPlugin = require('@winner-fed/webpack5-remove-use-strict-plugin');
35+
const Webpack5RemoveUseStrictPlugin = require('@winner-fed/webpack5-remove-use-strict-plugin')
3636

3737
module.exports = {
3838
// ... 其他 webpack 配置
3939
plugins: [
4040
// ... 其他插件
41-
new Webpack5RemoveUseStrictPlugin()
42-
]
43-
};
41+
new Webpack5RemoveUseStrictPlugin(),
42+
],
43+
}
4444
```
4545

4646
### ES Module 方式
4747

4848
```js
49-
import Webpack5RemoveUseStrictPlugin from '@winner-fed/webpack5-remove-use-strict-plugin';
49+
import Webpack5RemoveUseStrictPlugin from '@winner-fed/webpack5-remove-use-strict-plugin'
5050

5151
export default {
5252
// ... 其他 webpack 配置
5353
plugins: [
5454
// ... 其他插件
55-
new Webpack5RemoveUseStrictPlugin()
56-
]
57-
};
55+
new Webpack5RemoveUseStrictPlugin(),
56+
],
57+
}
5858
```
5959

6060
## 工作原理

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"webpack": "^5.99.8"
6262
},
6363
"engines": {
64-
"node": ">=16.18.0"
64+
"node": ">=20.18.0"
6565
},
6666
"prettier": "@sxzz/prettier-config"
6767
}

src/index.ts

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,74 @@
1-
/*
2-
* @FilePath: /webpack5-remove-use-strict-plugin/src/index.ts
3-
*/
4-
51
/**
62
* Webpack 5 plugin to remove "use strict" from the generated code
73
* 专为 Webpack 5 设计的移除 "use strict" 插件
84
*/
95

106
// 导入webpack类型
11-
import type { Compiler, Compilation, WebpackPluginInstance, sources } from 'webpack';
7+
import type {
8+
Compilation,
9+
Compiler,
10+
sources,
11+
WebpackPluginInstance,
12+
} from 'webpack'
1213

1314
// 匹配所有 "use strict" 和 'use strict' 声明(带或不带分号)
14-
const USE_STRICT_REGEX = /(\'|\")use\s+strict(\'|\")\;?/gm;
15+
const USE_STRICT_REGEX = /('|")use\s+strict('|");?/g
1516

1617
class Webpack5RemoveUseStrictPlugin implements WebpackPluginInstance {
17-
private readonly pluginName: string;
18+
private readonly pluginName: string
1819

1920
constructor() {
20-
this.pluginName = "Webpack5RemoveUseStrictPlugin";
21+
this.pluginName = 'Webpack5RemoveUseStrictPlugin'
2122
}
2223

2324
apply(compiler: Compiler): void {
2425
// 只使用 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]
4044

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+
}
5156

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, '')
5560

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+
}
6265
}
63-
}
64-
});
65-
}
66-
);
67-
});
66+
})
67+
},
68+
)
69+
},
70+
)
6871
}
6972
}
7073

71-
export default Webpack5RemoveUseStrictPlugin;
74+
export default Webpack5RemoveUseStrictPlugin

tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import Webpack5RemoveUseStrictPlugin from '../src'
44
test('插件实例化', () => {
55
const plugin = new Webpack5RemoveUseStrictPlugin()
66
assert.ok(plugin instanceof Webpack5RemoveUseStrictPlugin)
7-
})
7+
})

0 commit comments

Comments
 (0)