Skip to content

Commit 0a5ec81

Browse files
committed
fix: keep extracted wordpress dependencies
1 parent c8ce306 commit 0a5ec81

File tree

165 files changed

+791
-313
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+791
-313
lines changed

.editorconfig

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ root = true
44
end_of_line = lf
55
insert_final_newline = true
66

7-
[test/cases/**.html]
8-
end_of_line = lf
9-
# disable final new line for expected html files
10-
insert_final_newline = false
7+
[{*.cjs,*.js}]
8+
indent_size = 2
9+
tab_width = 2
10+
11+
[{*.htm,*.html,*.pug,*.md}]
12+
indent_size = 2
13+
tab_width = 2

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
"options": {
1616
"tabWidth": 4
1717
}
18+
},
19+
{
20+
"files": ["*.md"],
21+
"options": {
22+
"tabWidth": 2
23+
}
1824
}
1925
]
2026
}

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.8.3 (2022-09-04)
2+
- docs: update readme
3+
4+
## 0.8.2 (2022-09-04)
5+
- fix: keep extracted wordpress dependencies, #9
6+
- test: refactor tests
7+
- chore: update npm packages
8+
19
## 0.8.1 (2022-06-14)
210
- fix: add supports for TypeScript
311
- chore: update packages

README.md

Lines changed: 11 additions & 14 deletions
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.8.1)
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.3)
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)
@@ -54,32 +54,29 @@ Define Pug files in webpack entry:
5454
const PugPlugin = require('pug-plugin');
5555
module.exports = {
5656
entry: {
57-
// all sources of scripts and styles can be used directly in Pug
58-
// here should be defined Pug templates only
59-
index: './src/views/index.pug', // output index.html
60-
about: './src/views/about/index.pug' // output about.html
61-
// ..
57+
// all script and style sources can be defined directly in Pug
58+
index: './src/views/index.pug', // => dist/index.html
59+
about: './src/views/about/index.pug' // => dist/about.html
6260
},
6361
plugins: [
6462
// enable using Pug files in webpack entry
6563
new PugPlugin({
66-
modules: [
67-
// enable extract CSS from source of styles used in Pug
68-
PugPlugin.extractCss({
69-
// output filename of styles
70-
filename: 'assets/css/[name].[contenthash:8].css',
71-
}),
72-
],
64+
// extract CSS from style sources defined in Pug
65+
extractCss: {
66+
// output filename of styles
67+
filename: 'assets/css/[name].[contenthash:8].css',
68+
},
7369
}),
7470
],
75-
// ...
7671
};
7772
```
73+
7874
Use source files of styles and scripts directly in Pug:
7975
```pug
8076
link(href=require('./styles.scss') rel='stylesheet')
8177
script(src=require('./main.js'))
8278
```
79+
8380
Generated HTML contains hashed CSS and JS output filenames:
8481
```html
8582
<link href="/assets/css/styles.05e4dd86.css" rel="stylesheet">

package.json

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-remove-empty-scripts",
3-
"version": "0.8.1",
3+
"version": "0.8.3",
44
"description": "Webpack 5 plugin to remove empty scripts generated by usage only style in webpack entry.",
55
"keywords": [
66
"webpack",
@@ -36,22 +36,13 @@
3636
"access": "public",
3737
"registry": "https://registry.npmjs.org/"
3838
},
39-
39+
"main": "src/index.js",
4040
"scripts": {
4141
"test": "jest --detectOpenHandles --config ./test/jest.config.js",
4242
"test:coverage": "jest --detectOpenHandles --collectCoverage --config ./test/jest.config.js",
4343
"publish:public": "npm publish --access public",
4444
"publish:beta": "npm publish --tag beta"
4545
},
46-
"jest": {
47-
"testEnvironment": "node",
48-
"coverageDirectory": "./coverage/",
49-
"collectCoverage": true
50-
},
51-
"main": "src/index.js",
52-
"directories": {
53-
"test": "test"
54-
},
5546
"files": [
5647
"src",
5748
"CHANGELOG.md",
@@ -65,22 +56,22 @@
6556
"webpack": ">=5.32.0"
6657
},
6758
"dependencies": {
68-
"ansis": "^1.3.6"
59+
"ansis": "^1.4.0"
6960
},
7061
"devDependencies": {
71-
"@babel/core": "^7.18.5",
72-
"@babel/preset-env": "^7.18.2",
73-
"@types/jest": "^28.1.1",
62+
"@babel/core": "^7.18.13",
63+
"@babel/preset-env": "^7.18.10",
64+
"@test-fixtures/lorem" : "0.0.2",
65+
"@types/jest": "^28.1.8",
66+
"@wordpress/scripts": "^23.4.0",
67+
"@wordpress/dependency-extraction-webpack-plugin": "^4.0.0",
7468
"css-loader": "^6.7.1",
75-
"fs": "^0.0.1-security",
76-
"html-loader": "^3.1.0",
77-
"jest": "^28.1.1",
78-
"mini-css-extract-plugin": "^2.6.0",
79-
"moment": "^2.29.3",
80-
"rimraf": "^3.0.2",
81-
"terser-webpack-plugin": "^5.3.3",
82-
"webpack": "^5.73.0",
83-
"webpack-hot-middleware": "^2.25.1",
69+
"html-loader": "^4.1.0",
70+
"jest": "^28.1.3",
71+
"mini-css-extract-plugin": "^2.6.1",
72+
"terser-webpack-plugin": "^5.3.6",
73+
"webpack": "^5.74.0",
74+
"webpack-hot-middleware": "^2.25.2",
8475
"webpack-merge": "^5.8.0"
8576
}
8677
}

src/index.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
const path = require('path');
66
const ansis = require('ansis');
77

8-
const plugin = 'remove-empty-scripts';
8+
const { outToConsole } = require('./utils');
9+
10+
const pluginName = 'remove-empty-scripts';
911
const defaultOptions = {
1012
enabled: true,
1113
verbose: false,
@@ -19,6 +21,9 @@ const defaultOptions = {
1921
let dependencyId = 1;
2022

2123
class WebpackRemoveEmptyScriptsPlugin {
24+
outputPath = '';
25+
trash = [];
26+
2227
constructor (options) {
2328
this.apply = this.apply.bind(this);
2429
this.options = Object.assign({}, defaultOptions, options);
@@ -40,26 +45,29 @@ class WebpackRemoveEmptyScriptsPlugin {
4045
apply (compiler) {
4146
if (!this.enabled) return;
4247

43-
const { remove: removeAssets, ignore: ignoreEntryResource, extensions: styleExtensionRegexp } = this.options;
48+
// clear cache for webpack dev server
49+
this.trash = [];
50+
this.outputPath = compiler.options.output.path;
4451

45-
compiler.hooks.compilation.tap(plugin, compilation => {
52+
compiler.hooks.thisCompilation.tap(pluginName, compilation => {
4653
const resourcesCache = [];
4754

48-
compilation.hooks.chunkAsset.tap(plugin, (chunk, filename) => {
55+
compilation.hooks.chunkAsset.tap(pluginName, (chunk, filename) => {
56+
const { remove: removeAssets, ignore: ignoreEntryResource, extensions: styleExtensionRegexp } = this.options;
4957
if (!removeAssets.test(filename)) return;
5058

51-
const outputPath = compiler.options.output.path;
5259
const chunkGraph = compilation.chunkGraph;
5360
let entryResources = [];
5461

5562
for (const module of chunkGraph.getChunkEntryModulesIterable(chunk)) {
5663
if (!compilation.modules.has(module)) {
5764
throw new Error(
58-
`\n${ansis.black.bgRed(`[${plugin}]`)} entry module in chunk but not in compilation ${chunk.debugId} ${module.debugId}`
65+
`\n${ansis.black.bgRed(
66+
`[${pluginName}]`)} entry module in chunk but not in compilation ${chunk.debugId} ${module.debugId}`,
5967
);
6068
}
6169

62-
const moduleResources = collectEntryResources(compilation, module, resourcesCache);
70+
const moduleResources = getEntryResources(compilation, module, resourcesCache);
6371
entryResources = entryResources.concat(moduleResources);
6472
}
6573

@@ -72,24 +80,29 @@ class WebpackRemoveEmptyScriptsPlugin {
7280

7381
if (isEmptyScript) {
7482
if (this.verbose) {
75-
const outputFile = path.join(outputPath, filename);
76-
console.log(
77-
`${ansis.black.bgYellow(`[${plugin}]`)} remove ${ansis.cyan(outputFile)}`,
83+
const outputFile = path.join(this.outputPath, filename);
84+
outToConsole(
85+
`${ansis.black.bgYellow(`[${pluginName}]`)} remove ${ansis.cyan(outputFile)}\n`,
7886
);
7987
}
80-
81-
chunk.files.delete(filename);
82-
compilation.deleteAsset(filename);
88+
// note: do not delete here compilation empty assets, do it in 'afterProcessAssets' only
89+
this.trash.push(filename);
8390
}
8491
});
92+
93+
// Delete empty scripts only after processing all plugins,
94+
// otherwise, by usage some plugins, the necessary files may be not created.
95+
compilation.hooks.afterProcessAssets.tap(pluginName, () => {
96+
this.trash.forEach(file => compilation.deleteAsset(file));
97+
});
8598
});
8699
}
87100
}
88101

89-
function collectEntryResources (compilation, module, cache) {
102+
function getEntryResources (compilation, module, cache) {
90103
const moduleGraph = compilation.moduleGraph,
91104
index = moduleGraph.getPreOrderIndex(module),
92-
propNameDependencyId = '__dependencyWebpackRemoveEmptyScriptsUniqueId',
105+
propNameDependencyId = '__webpackRemoveEmptyScriptsUniqueId',
93106
resources = [];
94107

95108
// the index can be null
@@ -108,7 +121,6 @@ function collectEntryResources (compilation, module, cache) {
108121

109122
if (module.dependencies) {
110123
module.dependencies.forEach(dependency => {
111-
112124
let module = moduleGraph.getModule(dependency),
113125
originModule = moduleGraph.getParentModule(dependency),
114126
nextModule = module || originModule,
@@ -119,11 +131,8 @@ function collectEntryResources (compilation, module, cache) {
119131
useNextModule = true;
120132
}
121133

122-
// debug info
123-
//console.log('::: module ::: ', useNextModule ? '' : '-----', dependency[propNameDependencyId]);
124-
125134
if (nextModule && useNextModule) {
126-
const dependencyResources = collectEntryResources(compilation, nextModule, cache);
135+
const dependencyResources = getEntryResources(compilation, nextModule, cache);
127136

128137
for (let i = 0, length = dependencyResources.length; i !== length; i++) {
129138
const file = dependencyResources[i];

src/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const outToConsole = (...args) => process.stdout.write(args.join(' ') + '\n');
2+
3+
module.exports = {
4+
outToConsole,
5+
};

test/cases/vendor+multi-js-entry-css-entry/style.css renamed to test/cases/css-entry-only/expected/assets/css/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ body {
33
width: 100%;
44
height: 100%;
55
}
6+

test/cases/webpack-concatenate-modules/style.css renamed to test/cases/css-entry-only/expected/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ body {
33
width: 100%;
44
height: 100%;
55
}
6+
File renamed without changes.

0 commit comments

Comments
 (0)