File tree Expand file tree Collapse file tree 3 files changed +121
-0
lines changed Expand file tree Collapse file tree 3 files changed +121
-0
lines changed Original file line number Diff line number Diff line change 1+ # webpack-fix-style-only-entries
2+
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.
4+
5+ You can find more info by reading the following issues:
6+
7+ - https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/518
8+ - https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151
9+
10+ ## How it works
11+ It just find js files from chunks of css only entries and remove the js file rom the compilation.
12+
13+ ## How to use
14+ Require and add to webpack.config plugins.
15+
16+ 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.
17+
18+ ``` javascript
19+ // ... other plugins
20+ const MiniCssExtractPlugin = require (" mini-css-extract-plugin" );
21+ const FixStyleOnlyEntriesPlugin = require (" webpack-fix-style-only-entries" );
22+
23+ module .exports = {
24+ entry: {
25+ " main" : " ./app/main.js"
26+ " styles" : [" ./common/styles.css" , " ./app/styles.css" ]
27+ },
28+ module: {
29+ {
30+ test: / \. css$ / ,
31+ use: [
32+ MiniCssExtractPlugin .loader ,
33+ ' css-loader' ,
34+ ]
35+ },
36+ ]
37+ },
38+ plugins: [
39+ new FixStyleOnlyEntriesPlugin (),
40+ new MiniCssExtractPlugin ({
41+ filename: " [name].[chunkhash:8].css" ,
42+ }),
43+ ],
44+ };
45+ ```
46+
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'] }), `
Original file line number Diff line number Diff line change 1+ const NAME = "webpack-fix-style-only-entries" ;
2+
3+ class WebpackFixStyleOnlyEntriesPlugin {
4+ constructor ( options ) {
5+ this . apply = this . apply . bind ( this ) ;
6+
7+ options = options || { } ;
8+ let extensions = options . extensions || [ "less" , "scss" , "css" ] ;
9+ this . extensions = extensions . map ( e => ( e [ 0 ] === "." ? e : "." + e ) ) ;
10+ }
11+
12+ apply ( compiler ) {
13+ compiler . hooks . compilation . tap ( NAME , compilation => {
14+ 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+ }
30+
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+ }
43+ }
44+ }
45+ } ) ;
46+ } ) ;
47+ }
48+ }
49+
50+ module . exports = WebpackFixStyleOnlyEntriesPlugin ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " webpack-fix-style-only-entries" ,
3+ "version" : " 0.0.1" ,
4+ "description" : " Package to fix style (css/sass/less) only entries generating a extra js file." ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " echo \" Error: no test specified\" && exit 1"
8+ },
9+ "keywords" : [
10+ " webpack" ,
11+ " fix" ,
12+ " css" ,
13+ " js" ,
14+ " style"
15+ ],
16+ "author" : " fqborges (https://github.com/fqborges)" ,
17+ "license" : " ISC" ,
18+ "repository" : " github:fqborges/webpack-fix-style-only-entries"
19+ }
You can’t perform that action at this time.
0 commit comments