Skip to content

Commit ae578ef

Browse files
authored
Merge pull request #1 from lwr/next
breaking: change to webpack 5
2 parents db94f27 + a1a0271 commit ae578ef

File tree

10 files changed

+128
-135
lines changed

10 files changed

+128
-135
lines changed

.eslintignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
dist/**/*
1+
dist
22
node_modules
3-
example

.eslintrc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
extends:
2-
- metalab
3-
rules:
4-
metalab/filenames/match-exported: 0
2+
- standard
3+
env:
4+
mocha: true

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea
12
node_modules
23
*.log
34
dist

.npmignore

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
node_modules
2-
examples
3-
*.log
4-
coverage
1+
/*
2+
!/lib

README.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ npm install --save fast-css-split-webpack-plugin
1818

1919
## Usage
2020

21-
Simply add an instance of `FastCSSSplitWebpackPlugin` to your list of plugins in your webpack configuration file _after_ `ExtractTextPlugin`. That's it!
21+
Simply add an instance of `FastCSSSplitWebpackPlugin` to your list of plugins in your webpack configuration file _after_ `MiniCssExtractPlugin`. That's it!
2222

2323
```javascript
24-
var ExtractTextPlugin = require('extract-text-webpack-plugin');
25-
var FastCSSSplitWebpackPlugin = require('../');
24+
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
25+
var FastCSSSplitWebpackPlugin = require('fast-css-split-webpack-plugin');
2626

2727
module.exports = {
28+
mode: 'development',
2829
entry: './index.js',
2930
context: __dirname,
3031
output: {
@@ -33,13 +34,16 @@ module.exports = {
3334
filename: 'bundle.js',
3435
},
3536
module: {
36-
loaders: [{
37+
rules: [{
3738
test: /\.css$/,
38-
loader: ExtractTextPlugin.extract('style-loader', 'css-loader'),
39-
}],
39+
use: [MiniCssExtractPlugin.loader, {
40+
loader: 'css-loader',
41+
options: {sourceMap: true}
42+
}]
43+
}]
4044
},
4145
plugins: [
42-
new ExtractTextPlugin('styles.css'),
46+
new MiniCssExtractPlugin({filename: 'styles.css'}),
4347
new FastCSSSplitWebpackPlugin({size: 4000}),
4448
],
4549
};
@@ -55,9 +59,8 @@ The following configuration options are available:
5559

5660
**preserve**: `default: false`. Keep the original unsplit file as well. Sometimes this is desirable if you want to target a specific browser (IE) with the split files and then serve the unsplit ones to everyone else.
5761

58-
**defer**: `default: 'false'`. You can pass `true` here to cause this plugin to split the CSS on the `emit` phase. Sometimes this is needed if you have other plugins that operate on the CSS also in the emit phase. Unfortunately by doing this you potentially lose chunk linking and source maps. Use only when necessary.
62+
**defer**: `default: 'false'`. You can pass `true` here to cause this plugin to split the CSS on the `afterProcessAssets` phase. Sometimes this is needed if you have other plugins that operate on the CSS also in the same phase. Unfortunately by doing this you potentially lose chunk linking and source maps. Use only when necessary.
5963

6064
[webpack]: http://webpack.github.io/
61-
[herp]: https://github.com/ONE001/css-file-rules-webpack-separator
6265
[postcss]: https://github.com/postcss/postcss
6366
[postcss-chunk]: https://github.com/mattfysh/postcss-chunk

example/basic/webpack.config.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
var ExtractTextPlugin = require('extract-text-webpack-plugin')
2-
var FastCSSSplitWebpackPlugin = require('../../')
1+
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
2+
const FastCSSSplitWebpackPlugin = require('../../')
3+
4+
/* eslint object-curly-spacing: [error, never] */
5+
/* eslint node/no-path-concat: off */
36

47
module.exports = {
8+
mode: 'development',
59
entry: './index.js',
610
context: __dirname,
711
output: {
@@ -10,19 +14,17 @@ module.exports = {
1014
filename: 'bundle.js'
1115
},
1216
module: {
13-
loaders: [{
17+
rules: [{
1418
test: /\.css$/,
15-
loader: ExtractTextPlugin.extract.length !== 1
16-
? ExtractTextPlugin.extract('style-loader', 'css-loader')
17-
: ExtractTextPlugin.extract({
18-
fallbackLoader: 'style-loader',
19-
loader: 'css-loader'
20-
})
19+
use: [MiniCssExtractPlugin.loader, {
20+
loader: 'css-loader',
21+
options: {sourceMap: true}
22+
}]
2123
}]
2224
},
2325
devtool: 'source-map',
2426
plugins: [
25-
new ExtractTextPlugin('styles.css'),
27+
new MiniCssExtractPlugin({filename: 'styles.css'}),
2628
new FastCSSSplitWebpackPlugin({size: 3, imports: true})
2729
]
2830
}

example/less/webpack.config.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
var ExtractTextPlugin = require('extract-text-webpack-plugin')
2-
var FastCSSSplitWebpackPlugin = require('../../')
1+
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
2+
const FastCSSSplitWebpackPlugin = require('../../')
3+
4+
/* eslint object-curly-spacing: [error, never] */
5+
/* eslint node/no-path-concat: off */
36

47
module.exports = {
8+
mode: 'development',
59
entry: './index.js',
610
context: __dirname,
711
output: {
@@ -10,16 +14,20 @@ module.exports = {
1014
filename: 'bundle.js'
1115
},
1216
module: {
13-
loaders: [{
17+
rules: [{
1418
test: /\.less$/,
15-
loader: ExtractTextPlugin.extract(
16-
'css?-url&-autoprefixer&sourceMap!less?sourceMap'
17-
)
19+
use: [MiniCssExtractPlugin.loader, {
20+
loader: 'css-loader',
21+
options: {sourceMap: true, url: false}
22+
}, {
23+
loader: 'less-loader',
24+
options: {sourceMap: true}
25+
}]
1826
}]
1927
},
2028
devtool: 'source-map',
2129
plugins: [
22-
new ExtractTextPlugin('styles.css'),
30+
new MiniCssExtractPlugin({filename: 'styles.css'}),
2331
new FastCSSSplitWebpackPlugin({size: 3})
2432
]
2533
}

lib/index.js

+34-51
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const path = require('path')
44
const split = require('css-split')
5-
const { RawSource } = require('webpack-sources')
5+
const { sources: { RawSource }, Compilation } = require('webpack')
66
const { interpolateName } = require('loader-utils')
77

88
/**
@@ -68,27 +68,32 @@ const normalizeImports = (input, preserve) => {
6868
class FastCSSSplitWebpackPlugin {
6969
/**
7070
* Create new instance of FastCSSSplitWebpackPlugin.
71-
* @param {Number} size Maximum number of rules for a single file.
72-
* @param {Boolean|String} imports Truish to generate an additional import
71+
* @param [options]
72+
* @param {Number} [options.size] Maximum number of rules for a single file.
73+
* @param {Boolean|String} [options.imports] Truish to generate an additional import
7374
* asset. When a boolean use the default name for the asset.
74-
* @param {String} filename Control the generated split file name.
75-
* @param {Boolean} defer Defer splitting until the `emit` phase. Normally
76-
* only needed if something else in your pipeline is mangling things at
77-
* the emit phase too.
78-
* @param {Boolean} preserve True to keep the original unsplit file.
75+
* @param {String} [options.filename] Control the generated split file name.
76+
* @param {Number} [options.stage] only valid while [options.defer] is false
77+
* see <https://webpack.js.org/api/compilation-hooks/#additional-assets>
78+
* @param {Boolean} [options.defer] Defer splitting until the `afterProcessAssets`
79+
* phase. Normally only needed if something else in your pipeline is mangling
80+
* things at the same phase too.
81+
* @param {Boolean} [options.preserve] True to keep the original unsplit file.
7982
*/
8083
constructor ({
8184
size = 4000,
8285
imports = false,
8386
filename = '[name]-[part].[ext]',
8487
preserve,
88+
stage = Compilation.PROCESS_ASSETS_STAGE_DEV_TOOLING,
8589
defer = false
86-
}) {
90+
} = {}) {
8791
this.options = {
8892
size,
8993
imports: normalizeImports(imports, preserve),
9094
filename: nameInterpolator(filename),
9195
preserve,
96+
stage,
9297
defer
9398
}
9499
}
@@ -97,7 +102,6 @@ class FastCSSSplitWebpackPlugin {
97102
* Generate the split chunks for a given CSS file.
98103
* @param {String} key Name of the file.
99104
* @param {Object} asset Valid webpack Source object.
100-
* @returns {Promise} Promise generating array of new files.
101105
*/
102106
file (key, asset) {
103107
const input = {
@@ -124,12 +128,12 @@ class FastCSSSplitWebpackPlugin {
124128
}
125129
}
126130

127-
chunksMapping (compilation, chunks, done) {
131+
chunksMapping (compilation) {
128132
const assets = compilation.assets
129133
const publicPath = strip(compilation.options.output.publicPath || './')
130134

131-
chunks.map((chunk) => {
132-
const input = chunk.files.filter(isCSS)
135+
for (const chunk of compilation.chunks) {
136+
const input = [...chunk.files].filter(isCSS)
133137
const entries = input.map((name) => this.file(name, assets[name]))
134138

135139
entries.forEach((entry) => {
@@ -145,7 +149,7 @@ class FastCSSSplitWebpackPlugin {
145149
// - ...
146150
entry.chunks.forEach((file) => {
147151
assets[file.fullname] = file
148-
chunk.files.push(file.fullname)
152+
chunk.files.add(file.fullname)
149153
})
150154

151155
// generate imports file content
@@ -162,7 +166,7 @@ class FastCSSSplitWebpackPlugin {
162166

163167
// if chunks.length == 1, the original chunk will be always preserved
164168
if (entry.chunks.length > 1 && !this.options.preserve) {
165-
chunk.files.splice(chunk.files.indexOf(entry.file), 1)
169+
chunk.files.delete(entry.file)
166170
delete assets[entry.file]
167171
}
168172

@@ -176,12 +180,10 @@ class FastCSSSplitWebpackPlugin {
176180
imports = `${entry.dirname}/${imports}`
177181
}
178182
assets[imports] = importsFile
179-
chunk.files.push(imports)
183+
chunk.files.add(imports)
180184
}
181185
})
182-
})
183-
184-
done()
186+
}
185187
}
186188

187189
/**
@@ -195,44 +197,25 @@ class FastCSSSplitWebpackPlugin {
195197
* @returns {void}
196198
*/
197199
apply (compiler) {
198-
// for webpack 4
199-
if (compiler.hooks) {
200-
const plugin = {
201-
name: 'FastCssSplitPlugin'
202-
}
203-
204-
if (this.options.defer) {
205-
// Run on `emit` when user specifies the compiler phase
200+
const name = 'FastCssSplitPlugin'
201+
const { defer, stage } = this.options
202+
203+
// Only run on `thisCompilation` to avoid injecting the plugin into
204+
// sub-compilers as happens when using the `mini-css-extract-plugin`.
205+
compiler.hooks.thisCompilation.tap(name, compilation => {
206+
if (defer) {
207+
// Run on `afterProcessAssets` when user specifies the compiler phase
206208
// Due to the incorrect css split + optimization behavior
207209
// Expected: css split should happen after optimization
208-
compiler.hooks.emit.tapAsync(plugin, (compilation, done) => {
209-
this.chunksMapping(compilation, compilation.chunks, done)
210+
compilation.hooks.afterProcessAssets.tap(name, () => {
211+
this.chunksMapping(compilation)
210212
})
211213
} else {
212-
// use compilation instead of this-compilation, just like other plugins do
213-
compiler.hooks.compilation.tap(plugin, compilation => {
214-
compilation.hooks.optimizeChunkAssets.tapAsync(plugin, (chunks, done) => {
215-
this.chunksMapping(compilation, chunks, done)
216-
})
214+
compilation.hooks.processAssets.tap({ name, stage }, () => {
215+
this.chunksMapping(compilation)
217216
})
218217
}
219-
} else {
220-
if (this.options.defer) {
221-
// Run on `emit` when user specifies the compiler phase
222-
// Due to the incorrect css split + optimization behavior
223-
// Expected: css split should happen after optimization
224-
compiler.plugin('emit', (compilation, done) => {
225-
return this.chunksMapping(compilation, compilation.chunks, done)
226-
})
227-
} else {
228-
// use compilation instead of this-compilation, just like other plugins do
229-
compiler.plugin('compilation', (compilation) => {
230-
compilation.plugin('optimize-chunk-assets', (chunks, done) => {
231-
return this.chunksMapping(compilation, chunks, done)
232-
})
233-
})
234-
}
235-
}
218+
})
236219
}
237220
}
238221

package.json

+14-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "fast-css-split-webpack-plugin",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"author": "yibn2008 <[email protected]>",
55
"license": "CC0-1.0",
6-
"repository": "metalabdesign/css-split-webpack-plugin",
6+
"repository": "yibn2008/fast-css-split-webpack-plugin",
77
"main": "lib/index.js",
88
"keywords": [
99
"webpack-plugin",
@@ -15,39 +15,31 @@
1515
"spec": "NODE_ENV=test ./node_modules/.bin/_mocha --timeout 5000 -r adana-dump -R spec --recursive test/spec/**/*.spec.js"
1616
},
1717
"standard": {
18-
"global": [
19-
"describe",
20-
"it",
21-
"beforeEach",
22-
"afterEach"
23-
],
24-
"ignore": [
25-
"/test",
26-
"/example"
18+
"env": [
19+
"mocha"
2720
]
2821
},
2922
"devDependencies": {
3023
"adana-cli": "^0.1.1",
3124
"adana-dump": "^0.1.0",
3225
"adana-format-lcov": "^0.1.1",
3326
"chai": "^3.5.0",
34-
"css-loader": "^0.23.1",
35-
"extract-text-webpack-plugin": "^1.0.1",
36-
"less": "^2.7.1",
37-
"less-loader": "^2.2.3",
27+
"css-loader": "^6.2.0",
28+
"mini-css-extract-plugin": "^2.2.0",
29+
"less": "^4.1.1",
30+
"less-loader": "^10.0.1",
3831
"memory-fs": "^0.3.0",
3932
"mocha": "3.x",
40-
"optimize-css-assets-webpack-plugin": "^3.2.0",
41-
"standard": "^11.0.1",
42-
"style-loader": "^0.13.1",
43-
"webpack": "^1.13.0"
33+
"css-minimizer-webpack-plugin": "^3.0.2",
34+
"standard": "^16.0.3",
35+
"style-loader": "^3.2.1",
36+
"webpack": "^5.50.0"
4437
},
4538
"dependencies": {
4639
"css-split": "^1.1.3",
47-
"loader-utils": "^1.1.0",
48-
"webpack-sources": "^1.0.2"
40+
"loader-utils": "^1.4.0"
4941
},
5042
"peerDependencies": {
51-
"webpack": ">=1"
43+
"webpack": ">=5"
5244
}
5345
}

0 commit comments

Comments
 (0)