|
1 | | -# ts-starter [](https://npmjs.com/package/ts-starter) |
| 1 | +# webpack5-remove-use-strict-plugin |
2 | 2 |
|
3 | | -[](https://github.com/sxzz/ts-starter/actions/workflows/unit-test.yml) |
| 3 | +[](https://npmjs.com/package/@winner-fed/webpack5-remove-use-strict-plugin) |
4 | 4 |
|
5 | | -My awesome typescript library |
| 5 | +专为 Webpack 5 设计的移除 "use strict" 声明的插件。此插件可以从生成的 JavaScript 代码中删除所有 'use strict' 和 "use strict" 声明。 |
6 | 6 |
|
7 | | -<!-- Remove belows --> |
| 7 | +## 背景 |
8 | 8 |
|
9 | | -Forked from [egoist/ts-starter](https://github.com/egoist/ts-starter) |
| 9 | +在某些特定场景下,你可能需要移除 Webpack 生成代码中的 "use strict" 声明: |
10 | 10 |
|
11 | | -## Using this template |
| 11 | +- 当你需要在不支持严格模式的环境中运行代码 |
| 12 | +- 当你的代码依赖于非严格模式的行为 |
| 13 | +- 当你需要与特定的、不兼容严格模式的旧代码或库集成 |
12 | 14 |
|
13 | | -- Search `ts-starter` and replace it with your custom package name. |
14 | | -- Search `sxzz` and replace it with your name. |
15 | | -- Remove sponsors below. |
| 15 | +## 安装 |
16 | 16 |
|
17 | | -Features: |
| 17 | +```bash |
| 18 | +# 使用 npm |
| 19 | +npm install --save-dev @winner-fed/webpack5-remove-use-strict-plugin |
| 20 | + |
| 21 | +# 使用 yarn |
| 22 | +yarn add --dev @winner-fed/webpack5-remove-use-strict-plugin |
18 | 23 |
|
19 | | -- Package manager [pnpm](https://pnpm.js.org/), safe and fast |
20 | | -- Bundle with blazing fast [tsdown](https://github.com/sxzz/tsdown) |
21 | | -- Test with [Vitest](https://vitest.dev) |
| 24 | +# 使用 pnpm |
| 25 | +pnpm add --save-dev @winner-fed/webpack5-remove-use-strict-plugin |
| 26 | +``` |
22 | 27 |
|
23 | | -<!-- Remove aboves --> |
| 28 | +## 使用方法 |
24 | 29 |
|
25 | | -## Install |
| 30 | +在你的 Webpack 配置文件中引入并使用此插件: |
26 | 31 |
|
27 | | -```bash |
28 | | -npm i ts-starter |
| 32 | +### CommonJS 方式 |
| 33 | + |
| 34 | +```js |
| 35 | +const Webpack5RemoveUseStrictPlugin = require('@winner-fed/webpack5-remove-use-strict-plugin').default; |
| 36 | + |
| 37 | +module.exports = { |
| 38 | + // ... 其他 webpack 配置 |
| 39 | + plugins: [ |
| 40 | + // ... 其他插件 |
| 41 | + new Webpack5RemoveUseStrictPlugin() |
| 42 | + ] |
| 43 | +}; |
| 44 | +``` |
| 45 | + |
| 46 | +### ES Module 方式 |
| 47 | + |
| 48 | +```js |
| 49 | +import Webpack5RemoveUseStrictPlugin from '@winner-fed/webpack5-remove-use-strict-plugin'; |
| 50 | + |
| 51 | +export default { |
| 52 | + // ... 其他 webpack 配置 |
| 53 | + plugins: [ |
| 54 | + // ... 其他插件 |
| 55 | + new Webpack5RemoveUseStrictPlugin() |
| 56 | + ] |
| 57 | +}; |
29 | 58 | ``` |
30 | 59 |
|
31 | | -## Sponsors |
| 60 | +## 工作原理 |
| 61 | + |
| 62 | +该插件通过 Webpack 5 的 `processAssets` 钩子在优化资源阶段工作,可以移除所有生成的 JavaScript 文件中的 'use strict' 和 "use strict" 声明(不管有没有分号)。 |
| 63 | + |
| 64 | +插件会: |
| 65 | + |
| 66 | +1. 监听 Webpack 的 `compilation` 钩子 |
| 67 | +2. 在 `PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE` 阶段处理所有 JavaScript 资源 |
| 68 | +3. 使用正则表达式查找并移除所有 "use strict" 声明 |
| 69 | +4. 使用 Webpack 的 `RawSource` 更新资源内容 |
| 70 | + |
| 71 | +## 兼容性 |
32 | 72 |
|
33 | | -<p align="center"> |
34 | | - <a href="https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg"> |
35 | | - <img src='https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg'/> |
36 | | - </a> |
37 | | -</p> |
| 73 | +本插件仅兼容 Webpack 5+,不支持 Webpack 4 或更早版本。 |
38 | 74 |
|
39 | | -## License |
| 75 | +## 许可证 |
40 | 76 |
|
41 | | -[MIT](./LICENSE) License © 2025 [三咲智子 Kevin Deng](https://github.com/sxzz) |
| 77 | +[MIT](./LICENSE) 许可证 |
0 commit comments