Skip to content

Commit 2b62834

Browse files
fqborgeswebdiscus
authored andcommitted
version bump 0.6.0
1 parent 7141bcb commit 2b62834

Some content is hidden

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

69 files changed

+8953
-43
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
_ignored
3+
node_modules

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"trailingComma": "es5"
3+
}

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## 0.6.0 (Oct 13, 2020)
2+
Being overly careful here, this version is not breaking for almost all the existing users. It could possibly break in some edge cases, since it changes how modules are collected (from global to one each compilation) or if you have a workaround for a working webpack multi configuration.
3+
* BREAKING (POSSIBLY): Use a dedicated cache for every compilation (Prevent arbitrary files deletion when using Webpack with multi configurations) (PR [#39](https://github.com/fqborges/webpack-fix-style-only-entries/pull/39))
4+
5+
## 0.5.2 (Oct 07, 2020)
6+
* Webpack 5 support using ModuleGraph API (PR [#38](https://github.com/fqborges/webpack-fix-style-only-entries/pull/38))
7+
* npm audit fix: ([ea9dd7](https://github.com/fqborges/webpack-fix-style-only-entries/commit/ea9dd7bd7ade5b16623064a4850de39545e1e18e))
8+
9+
## 0.5.1 (Jun 13, 2020)
10+
* Fix Maximum call stack size exceeded (PR [#34](https://github.com/fqborges/webpack-fix-style-only-entries/pull/34))
11+
* Added CHANGELOG ([3e3767](https://github.com/fqborges/webpack-fix-style-only-entries/commit/3e3767d8998a53d807b5d5b10643d05b800aa79a))
12+
13+
## 0.5.0 (May 18, 2020)
14+
15+
* fix for [#23](https://github.com/fqborges/webpack-fix-style-only-entries/issues/23) ([37d350](https://github.com/fqborges/webpack-fix-style-only-entries/commit/37d350eec83f49c0b12729a93aa6cf2f96d92d0b))
16+
* fix for [#24](https://github.com/fqborges/webpack-fix-style-only-entries/issues/24) ([d92bec](https://github.com/fqborges/webpack-fix-style-only-entries/commit/d92bec4be5fe4b97a8651a9206fa2281ce1424be))
17+
* BREAKING: added styl and sass to default extensions ([247a5c](https://github.com/fqborges/webpack-fix-style-only-entries/commit/247a5c9f963e4d7598539056ab3f709b0558b4ec))
18+
* added LICENSE.txt ([220e20](https://github.com/fqborges/webpack-fix-style-only-entries/commit/220e20eeb9bc95652a942812a424aadd92ec7d1f))
19+
20+
## 0.4.0 (Sep 8, 2019)
21+
* feat: add support for module js files (PR [#21](https://github.com/fqborges/webpack-fix-style-only-entries/pull/21))
22+

LICENSE.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ISC License
2+
3+
Copyright (c) 2020, fqborges
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
# webpack-fix-style-only-entries
1+
[![npm version](https://badge.fury.io/js/webpack-fix-style-only-entries.svg)](https://www.npmjs.com/package/webpack-fix-style-only-entries)
22

3-
This is a small plugin developed to solve the problem of having a style only entry (css/sass/less) generating an extra [name].js file.
3+
# [webpack-fix-style-only-entries](https://www.npmjs.com/package/webpack-fix-style-only-entries)
4+
5+
This is a small plugin developed to solve the problem of having a style only entry (css/sass/less/stylus) generating an extra js file.
46

57
You can find more info by reading the following issues:
68

79
- https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/518
810
- https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151
911

12+
View on: [Github](https://github.com/fqborges/webpack-fix-style-only-entries) - [npm](https://www.npmjs.com/package/webpack-fix-style-only-entries)
13+
1014
## How it works
11-
It just find js files from chunks of css only entries and remove the js file rom the compilation.
15+
It just find js files from chunks of css only entries and remove the js file from the compilation.
1216

1317
## How to use
18+
install using your package manager of choice:
19+
- npm: `npm install -D webpack-fix-style-only-entries`
20+
- yarn: `yarn add -D webpack-fix-style-only-entries`
21+
1422
Require and add to webpack.config plugins.
1523

1624
Warning: this plugin does not load styles or split your bundles, it just fix chunks of css only entries by removing the (almost) empty js file.
@@ -44,9 +52,22 @@ module.exports = {
4452
};
4553
```
4654

47-
### Options
48-
- extensions: file extensions for styles.
49-
- type: Array[string]
50-
- default: ["less", "scss", "css"]
51-
- optional
52-
- Example: to identify only 'foo' and 'bar' extensions as styles: `new FixStyleOnlyEntriesPlugin({ extensions:['foo', 'bar'] }),`
55+
## Options
56+
57+
| Name | Type | Default | Description |
58+
|------------|--------------- |----------------------------------------|-------------|
59+
| extensions | Array[string] | ["less", "scss", "css", "styl","sass"] | file extensions for styles |
60+
| silent | boolean | false | supress logs to console |
61+
| ignore | string or RegExp | undefined | match resource to be ignored |
62+
63+
### Example config:
64+
// to identify only 'foo' and 'bar' extensions as styles
65+
new FixStyleOnlyEntriesPlugin({ extensions:['foo', 'bar'] }),
66+
67+
## Recipes
68+
69+
### I use a javascript entry to styles:
70+
Give an especial extension to your file (`.css.js` for example) and configure `new FixStyleOnlyEntriesPlugin({ extensions:['css.js'] })`. See: https://github.com/fqborges/webpack-fix-style-only-entries/issues/8.
71+
72+
### I use webpack-hot-middleware:
73+
Configure this plugin as `new FixStyleOnlyEntriesPlugin({ ignore: 'webpack-hot-middleware' })`. See: https://github.com/fqborges/webpack-fix-style-only-entries/issues/12 and https://github.com/fqborges/webpack-fix-style-only-entries/blob/master/test/cases/css-entry-with-ignored-hmr/webpack.config.js.

index.js

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,103 @@
11
const NAME = "webpack-fix-style-only-entries";
22

3+
const defaultOptions = {
4+
extensions: ["less", "scss", "css", "sass", "styl"],
5+
silent: false,
6+
ignore: undefined,
7+
};
8+
39
class WebpackFixStyleOnlyEntriesPlugin {
410
constructor(options) {
511
this.apply = this.apply.bind(this);
612

7-
options = options || {};
8-
let extensions = options.extensions || ["less", "scss", "css"];
9-
this.extensions = extensions.map(e => (e[0] === "." ? e : "." + e));
13+
this.options = Object.assign({}, defaultOptions, options);
1014
}
1115

1216
apply(compiler) {
17+
const extensionsWithoutDots = this.options.extensions.map(e =>
18+
e[0] === "." ? e.substring(1) : e
19+
);
20+
21+
const patternOneOfExtensions = extensionsWithoutDots
22+
.map(ext => escapeRegExp(ext))
23+
.join("|");
24+
25+
const reStylesResource = new RegExp(
26+
`[.](${patternOneOfExtensions})([?].*)?$`
27+
);
28+
1329
compiler.hooks.compilation.tap(NAME, compilation => {
30+
const resourcesCache = [];
1431
compilation.hooks.chunkAsset.tap(NAME, (chunk, file) => {
15-
if (chunk.hasEntryModule()) {
16-
let resources;
17-
if (typeof chunk.entryModule.resource == "string") {
18-
resources = [chunk.entryModule.resource];
19-
} else {
20-
if (
21-
chunk.entryModule.dependencies &&
22-
chunk.entryModule.dependencies.length
23-
) {
24-
const modules = chunk.entryModule.dependencies.map(
25-
dep => dep.module
26-
);
27-
resources = modules.map(m => m.resource);
28-
}
29-
}
32+
if (!file.endsWith(".js") && !file.endsWith(".mjs")) return;
33+
if (!chunk.hasEntryModule()) return;
3034

31-
if (resources) {
32-
if (
33-
resources.every(resource =>
34-
this.extensions.find(ext => resource.endsWith(ext))
35-
)
36-
) {
37-
if (file.endsWith(".js")) {
38-
console.log("removing js from style only module: " + file);
39-
chunk.files = chunk.files.filter(f => f != file);
40-
delete compilation.assets[file];
41-
}
42-
}
35+
const rawResources = collectEntryResources(compilation, chunk.entryModule, resourcesCache);
36+
const resources = this.options.ignore
37+
? rawResources.filter(r => !r.match(this.options.ignore))
38+
: rawResources;
39+
40+
const isStyleOnly =
41+
resources.length &&
42+
resources.every(resource => reStylesResource.test(resource));
43+
if (isStyleOnly) {
44+
if (!this.options.silent) {
45+
console.error(
46+
"webpack-fix-style-only-entries: removing js from style only module: " +
47+
file
48+
);
4349
}
50+
chunk.files = chunk.files.filter(f => f != file);
51+
delete compilation.assets[file];
4452
}
4553
});
4654
});
4755
}
4856
}
4957

58+
function collectEntryResources(compilation, module, cache) {
59+
// module.index is unique per compilation
60+
// module.id can be null, not used here
61+
if (cache[module.index] !== undefined) {
62+
return cache[module.index];
63+
}
64+
65+
if (typeof module.resource == "string") {
66+
const resources = [module.resource];
67+
cache[module.index] = resources;
68+
return resources;
69+
}
70+
71+
const resources = [];
72+
if (module.dependencies) {
73+
const hasModuleGraphSupport = compilation.hasOwnProperty('moduleGraph');
74+
module.dependencies.forEach(dep => {
75+
if(dep) {
76+
const module = hasModuleGraphSupport ? compilation.moduleGraph.getModule(dep) : dep.module;
77+
const originModule = hasModuleGraphSupport ? compilation.moduleGraph.getParentModule(dep) : dep.originModule;
78+
const nextModule = module || originModule;
79+
if (nextModule) {
80+
const depResources = collectEntryResources(compilation, nextModule, cache);
81+
for (let index = 0, length = depResources.length; index !== length; index++) {
82+
resources.push(depResources[index]);
83+
}
84+
}
85+
}
86+
});
87+
}
88+
89+
cache[module.index] = resources;
90+
return resources;
91+
}
92+
93+
// https://github.com/lodash/lodash/blob/4.17.11/lodash.js#L14274
94+
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
95+
const reHasRegExpChar = RegExp(reRegExpChar.source);
96+
function escapeRegExp(string) {
97+
string = String(string);
98+
return string && reHasRegExpChar.test(string)
99+
? string.replace(reRegExpChar, "\\$&")
100+
: string;
101+
}
102+
50103
module.exports = WebpackFixStyleOnlyEntriesPlugin;

0 commit comments

Comments
 (0)