Skip to content

Commit 2bd1fa2

Browse files
committed
added new option remove to define custom RegExp for generated assets that must be removed
1 parent bd54db0 commit 2bd1fa2

File tree

8 files changed

+84
-33
lines changed

8 files changed

+84
-33
lines changed

CHANGELOG.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
## 0.7.3 (Jan 30, 2022) The last stable version
1+
## 0.8.0 (2022-04-01) The last stable version
2+
- added new option `remove` to define custom RegExp for generated assets that must be removed
3+
- update packages
4+
5+
## 0.7.3 (2022-01-30)
26
- added color verbose output via ANSI color library - ansis
37
- added the test case for styles imported from javascript
48

5-
## 0.7.2 (Dec 13, 2021)
9+
## 0.7.2 (2021-12-13)
610
- added new option `enable` to enable / disable the plugin, e.g. by development
711
- added supports of `RegExp` for option `extensions`
812
- remove deprecated option `silent`, use `verbose` to show process information (no braking change)
913
- added GitHub workflow + codecov
1014
- update packages
1115
- update readme
1216

13-
## 0.7.1 (Jan 14, 2021)
17+
## 0.7.1 (2021-01-14)
1418
### Bugfixes
1519
- fix the issue infinite recursion by collect of resources from dependency modules by usage in react app some big components with many thousands dependencies
1620

17-
## 0.7.0 (Dec 21, 2020)
21+
## 0.7.0 (2020-12-21)
1822
### Breaking change
1923
- deprecate the `silent` option, it will be removed on Juni 30, 2021. Use option `verbose: true` to show in console each removed empty file. Defaults, `verbose: false`.
2024

@@ -25,7 +29,7 @@
2529
- webpack setting `externals.jquery: 'jQuery'` or other external libs
2630
- The issue if first in webpack entries are a styles and then a scripts.
2731

28-
## 0.6.4 (Dec 19, 2020)
32+
## 0.6.4 (2020-12-19)
2933
- fix the error: `Maximum call stack size exceeded` with webpack setting `optimization.concatenateModules: true`and usage in script imports from `react` and `redux`
3034
- added the test case for single style without a scripts in webpack config
3135
- added silent mode in tests to suppress output log info in the console
@@ -34,14 +38,14 @@
3438
- added default value of `ignore` as `['/node_modules/']` to ignore resources from `node_modules` path
3539
- update npm packages.
3640

37-
## 0.6.3 (Oct 25, 2020)
41+
## 0.6.3 (2020-10-25)
3842
- fix BREAKING CHANGE in Webpack 5: no more changes should happen to `Compilation.assets`
3943
- update code accord new API
4044

41-
## 0.6.2 (Oct 24, 2020)
45+
## 0.6.2 (2020-10-24)
4246
Update npm packages.
4347

44-
## 0.6.1 (Oct 20, 2020)
48+
## 0.6.1 (2020-10-20)
4549
The fork of original [webpack-fix-style-only-entries](https://github.com/fqborges/webpack-fix-style-only-entries) (ver. 0.6.0) for support only Webpack 5 and above.
4650
The Webpack 4 is no longer supported.
4751

README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![npm](https://img.shields.io/npm/v/webpack-remove-empty-scripts/latest?logo=npm&color=brightgreen "npm package")](https://www.npmjs.com/package/webpack-remove-empty-scripts/v/0.7.3)
1+
[![npm](https://img.shields.io/npm/v/webpack-remove-empty-scripts/latest?logo=npm&color=brightgreen "npm package")](https://www.npmjs.com/package/webpack-remove-empty-scripts/v/0.8.0)
22
[![node](https://img.shields.io/node/v/webpack-remove-empty-scripts)](https://nodejs.org)
33
[![node](https://img.shields.io/github/package-json/dependency-version/webdiscus/webpack-remove-empty-scripts/peer/webpack)](https://webpack.js.org/)
44
[![codecov](https://codecov.io/gh/webdiscus/webpack-remove-empty-scripts/branch/master/graph/badge.svg)](https://codecov.io/gh/webdiscus/webpack-remove-empty-scripts)
@@ -40,11 +40,11 @@ You can find more info by the following issues:
4040
- [mini-css-extract-plugin issue](https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151)
4141

4242
## NEW
43-
> The `experimental` version [`0.8.0`](https://github.com/webdiscus/webpack-remove-empty-scripts) has **_new improved and fast algorithm_** to detect generated needless empty js files.\
43+
> The `experimental` version `^1.x.x` has **_new improved and fast algorithm_** to detect generated needless empty js files.\
4444
> Please test your project before using it in production.\
4545
> If you have a problem with the new version, please create a [new issue](https://github.com/webdiscus/webpack-remove-empty-scripts/issues).
4646
47-
> :warning: The last stable release is `0.7.3` in the branch [`stable`](https://github.com/webdiscus/webpack-remove-empty-scripts/tree/stable).
47+
> :warning: The last stable release is `0.8.0` in the branch [`master`](https://github.com/webdiscus/webpack-remove-empty-scripts/tree/master).
4848
4949
## Propose
5050
If you use the `mini-css-extract-plugin` only to extract `css` files from styles defined in webpack entry
@@ -129,11 +129,16 @@ Enable / disable the plugin.
129129
### `extensions`
130130
Type: `RegExp` Default: `/\.(css|scss|sass|less|styl)([?].*)?$/`
131131
Note: the Regexp should have the query part at end `([?].*)?$` to match assets like `style.css?key=val` <br>
132-
Type: `string[]` Default: `['css', 'scss', 'sass', 'less', 'styl']`. It is automatically converted to type `RegExp`.
132+
Type: `string[]` Default: `['css', 'scss', 'sass', 'less', 'styl']`. It is automatically converted to type `RegExp`. \
133+
Search for empty js files in source files only with these extensions.
133134

134135
### `ignore`
135136
Type: `string | RegExp | string[] | RegExp[]` Default: `null`<br>
136-
Match resource path to be ignored.
137+
Ignore source files.
138+
139+
### `remove`
140+
Type: `RegExp` Default: `/\.(js|mjs)$/`<br>
141+
Remove generated scripts.
137142

138143
### `verbose`
139144
Type: `boolean` Default: `false`<br>
@@ -165,6 +170,11 @@ Give an especial extension to your file, for example `.css.js`:
165170
new RemoveEmptyScriptsPlugin({ extensions: /\.(css.js)$/ })
166171
```
167172

173+
### Remove generated scripts `*.js` `*.mjs` except `*.rem.js` `*.rem.mjs`
174+
```JavaScript
175+
new RemoveEmptyScriptsPlugin({ remove: /(?<!\.rem)\.(js|mjs)$/ })
176+
```
177+
168178
### Recursive ignore all js files from directory, for example `my-workers/`
169179
```JavaScript
170180
new RemoveEmptyScriptsPlugin({

package.json

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
{
22
"name": "webpack-remove-empty-scripts",
3-
"version": "0.7.3",
4-
"description": "Webpack 5 plugin to remove empty scripts generated by usage only styles in webpack entry.",
3+
"version": "0.8.0",
4+
"description": "Webpack 5 plugin to remove empty scripts generated by usage only style in webpack entry.",
55
"keywords": [
66
"webpack",
77
"remove",
88
"empty",
99
"script",
1010
"style",
11+
"entry",
1112
"extract",
1213
"scss",
1314
"sass",
1415
"css",
15-
"js",
16-
"entry",
17-
"css-extract"
16+
"js"
1817
],
1918
"license": "ISC",
2019
"author": "webdiscus (https://github.com/webdiscus)",
@@ -40,7 +39,9 @@
4039

4140
"scripts": {
4241
"test": "jest --detectOpenHandles --config ./test/jest.config.js",
43-
"test:coverage": "jest --detectOpenHandles --collectCoverage --config ./test/jest.config.js"
42+
"test:coverage": "jest --detectOpenHandles --collectCoverage --config ./test/jest.config.js",
43+
"publish:public": "npm publish --access public",
44+
"publish:beta": "npm publish --tag beta"
4445
},
4546
"jest": {
4647
"testEnvironment": "node",
@@ -67,18 +68,18 @@
6768
"ansis": "^1.3.4"
6869
},
6970
"devDependencies": {
70-
"@babel/core": "^7.16.12",
71+
"@babel/core": "^7.17.8",
7172
"@babel/preset-env": "^7.16.11",
72-
"@types/jest": "^27.4.0",
73-
"css-loader": "^6.5.1",
73+
"@types/jest": "^27.4.1",
74+
"css-loader": "^6.7.1",
7475
"fs": "^0.0.1-security",
7576
"html-loader": "^3.1.0",
76-
"jest": "^27.4.7",
77-
"mini-css-extract-plugin": "^2.5.3",
77+
"jest": "^27.5.1",
78+
"mini-css-extract-plugin": "^2.6.0",
7879
"moment": "^2.29.1",
7980
"rimraf": "^3.0.2",
80-
"terser-webpack-plugin": "^5.3.0",
81-
"webpack": "^5.67.0",
81+
"terser-webpack-plugin": "^5.3.1",
82+
"webpack": "^5.70.0",
8283
"webpack-hot-middleware": "^2.25.1",
8384
"webpack-merge": "^5.8.0"
8485
}

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const defaultOptions = {
1010
enabled: true,
1111
verbose: false,
1212
extensions: ['css', 'scss', 'sass', 'less', 'styl'],
13-
scriptExtensionsTest: /\.(js|mjs)$/,
1413
ignore: [],
14+
remove: /\.(js|mjs)$/,
1515
};
1616

1717
// Save unique id in dependency object as marker of 'analysed module'
@@ -40,13 +40,13 @@ class WebpackRemoveEmptyScriptsPlugin {
4040
apply (compiler) {
4141
if (!this.enabled) return;
4242

43-
const { ignore: ignoreEntryResource, extensions: styleExtensionRegexp } = this.options;
43+
const { remove: removeAssets, ignore: ignoreEntryResource, extensions: styleExtensionRegexp } = this.options;
4444

4545
compiler.hooks.compilation.tap(plugin, compilation => {
4646
const resourcesCache = [];
4747

4848
compilation.hooks.chunkAsset.tap(plugin, (chunk, filename) => {
49-
if (!defaultOptions.scriptExtensionsTest.test(filename)) return;
49+
if (!removeAssets.test(filename)) return;
5050

5151
const outputPath = compiler.options.output.path;
5252
const chunkGraph = compilation.chunkGraph;
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"style.css",
3+
"style.rem.css",
4+
"style.rem.js"
5+
]

test/cases/option-remove/style.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: red;
3+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const WebpackRemoveEmptyScripts = require('../../../src/index.js');
2+
3+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
4+
5+
module.exports = {
6+
entry: {
7+
'style': './style.css',
8+
'style.rem': './style.css',
9+
},
10+
module: {
11+
rules: [
12+
{
13+
test: /\.css$/,
14+
use: [MiniCssExtractPlugin.loader, 'css-loader'],
15+
},
16+
],
17+
},
18+
plugins: [
19+
new WebpackRemoveEmptyScripts({
20+
verbose: true,
21+
remove: /(?<!\.rem)\.(js|mjs)$/, // generated assets `*.js` must be removed, but not `*.rem.js`
22+
}),
23+
new MiniCssExtractPlugin({
24+
filename: '[name].css',
25+
}),
26+
],
27+
};

test/index.integration.test.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import { readDirRecursiveSync } from './utils/file';
2-
31
const fs = require('fs');
42
const path = require('path');
53
const rimraf = require('rimraf');
64
const webpack = require('webpack');
75
const { merge } = require('webpack-merge');
86

7+
import { readDirRecursiveSync } from './utils/file';
8+
99
//const modes = ['development'];
1010
const modes = ['development', 'production'];
11+
const cases = fs.readdirSync(path.join(__dirname, 'cases'));
1112

1213
// if not empty, then test only this cases
1314
const testOnly = [
14-
//'css-import',
1515
//'option-extension-array',
1616
//'option-extension-regexp',
1717
//'option-ignore-array',
1818
//'option-ignore-string',
19+
// 'option-remove',
20+
//'css-import',
1921
//'css-entry-only',
2022
//'css-entry-with-ignored-hmr',
2123
//'css-entry-with-query',
@@ -34,7 +36,6 @@ const testOnly = [
3436
//'webpack-concatenate-modules',
3537
];
3638

37-
const cases = fs.readdirSync(path.join(__dirname, 'cases'));
3839

3940
beforeAll(() => {
4041
rimraf.sync(path.join(__dirname, 'output'));

0 commit comments

Comments
 (0)