Skip to content

Commit 0a2175a

Browse files
committed
feat: support [email protected]
1 parent 0323f9b commit 0a2175a

File tree

6 files changed

+49
-95
lines changed

6 files changed

+49
-95
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ npm install --save css-split-webpack-plugin
2121
Simply add an instance of `CSSSplitWebpackPlugin` to your list of plugins in your webpack configuration file _after_ `ExtractTextPlugin`. That's it!
2222

2323
```javascript
24-
var ExtractTextPlugin = require('extract-text-webpack-plugin');
24+
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
2525
var CSSSplitWebpackPlugin = require('../').default;
2626

2727
module.exports = {
@@ -35,11 +35,14 @@ module.exports = {
3535
module: {
3636
loaders: [{
3737
test: /\.css$/,
38-
loader: ExtractTextPlugin.extract('style-loader', 'css-loader'),
38+
use: [
39+
MiniCssExtractPlugin.loader,
40+
'css-loader',
41+
],
3942
}],
4043
},
4144
plugins: [
42-
new ExtractTextPlugin('styles.css'),
45+
new MiniCssExtractPlugin({filename: 'style.css'}),
4346
new CSSSplitWebpackPlugin({size: 4000}),
4447
],
4548
};

example/basic/webpack.config.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
22
const CSSSplitWebpackPlugin = require('../../').default;
33

4-
const miniCssConfig = {
5-
loader: MiniCssExtractPlugin.loader,
6-
options: {
7-
// you can specify a publicPath here
8-
// by default it uses publicPath in webpackOptions.output
9-
publicPath: '../',
10-
hmr: process.env.NODE_ENV === 'development',
11-
},
12-
};
13-
144
module.exports = {
155
entry: './index.js',
166
context: __dirname,
@@ -23,7 +13,7 @@ module.exports = {
2313
rules: [{
2414
test: /\.css$/,
2515
use: [
26-
miniCssConfig,
16+
MiniCssExtractPlugin.loader,
2717
'css-loader',
2818
],
2919
}],

example/less/webpack.config.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
22
const CSSSplitWebpackPlugin = require('../../').default;
33

4-
const miniCssConfig = {
5-
loader: MiniCssExtractPlugin.loader,
6-
options: {
7-
// you can specify a publicPath here
8-
// by default it uses publicPath in webpackOptions.output
9-
publicPath: '../',
10-
hmr: process.env.NODE_ENV === 'development',
11-
},
12-
};
13-
144
module.exports = {
155
entry: './index.js',
166
context: __dirname,
@@ -23,7 +13,7 @@ module.exports = {
2313
rules: [{
2414
test: /\.less$/,
2515
use: [
26-
miniCssConfig,
16+
MiniCssExtractPlugin.loader,
2717
'css-loader',
2818
'less-loader',
2919
],

package.json

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,22 @@
2828
"babel-preset-flow": "^6.23.0",
2929
"chai": "^3.5.0",
3030
"cross-env": "^6.0.3",
31-
"css-loader": "^3.2.0",
31+
"css-loader": "^6.2.0",
3232
"eslint": "^4.6.0",
3333
"eslint-config-metalab": "^7.0.1",
34-
"extract-text-webpack-plugin": "^3.0.2",
35-
"less": "^3.10.3",
36-
"less-loader": "^5.0.0",
34+
"less": "^4.1.1",
35+
"less-loader": "^10.0.1",
3736
"memory-fs": "^0.3.0",
38-
"mini-css-extract-plugin": "^0.8.0",
37+
"mini-css-extract-plugin": "^2.2.0",
3938
"mocha": "^2.4.5",
40-
"optimize-css-assets-webpack-plugin": "^3.2.0",
41-
"style-loader": "^0.13.1",
42-
"webpack": "^4.41.2"
39+
"webpack": "^5.50.0"
4340
},
4441
"dependencies": {
4542
"loader-utils": "^1.1.0",
4643
"postcss": "^6.0.14",
47-
"webpack-sources": "^1.0.2"
44+
"webpack-sources": "^3.2.0"
4845
},
4946
"peerDependencies": {
50-
"webpack": ">=1"
47+
"webpack": ">=5"
5148
}
5249
}

src/index.js

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import chunk from './chunk';
33
import {SourceMapSource, RawSource} from 'webpack-sources';
44
import {interpolateName} from 'loader-utils';
55

6+
const name = 'CSSSplitWebpackPlugin';
7+
68
/**
79
* Detect if a file should be considered for CSS splitting.
810
* @param {String} name Name of the file.
@@ -133,8 +135,8 @@ export default class CSSSplitWebpackPlugin {
133135
chunksMapping(compilation, chunks, done) {
134136
const assets = compilation.assets;
135137
const publicPath = strip(compilation.options.output.publicPath || './');
136-
const promises = chunks.map((chunk) => {
137-
const input = chunk.files.filter(isCSS);
138+
const promises = Array.from(chunks).map((chunk) => {
139+
const input = Array.from(chunk.files).filter(isCSS);
138140
const items = input.map((name) => this.file(name, assets[name]));
139141
return Promise.all(items).then((entries) => {
140142
entries.forEach((entry) => {
@@ -145,23 +147,25 @@ export default class CSSSplitWebpackPlugin {
145147
}
146148
// Inject the new files into the chunk.
147149
entry.chunks.forEach((file) => {
148-
assets[file.name] = file;
149-
chunk.files.push(file.name);
150+
const name = file.name // RawSource
151+
|| file._name; // SourceMapSource
152+
assets[name] = file;
153+
chunk.files.add(name);
150154
});
151-
const content = entry.chunks.map((file) => {
155+
const content = Array.from(entry.chunks).map((file) => {
152156
return `@import "${publicPath}/${file._name}";`;
153157
}).join('\n');
154158
const imports = this.options.imports({
155159
...entry,
156160
content,
157161
});
158162
if (!this.options.preserve) {
159-
chunk.files.splice(chunk.files.indexOf(entry.file), 1);
163+
chunk.files.delete(entry.file);
160164
delete assets[entry.file];
161165
}
162166
if (imports) {
163167
assets[imports] = new RawSource(content);
164-
chunk.files.push(imports);
168+
chunk.files.add(imports);
165169
}
166170
});
167171
return Promise.resolve();
@@ -187,35 +191,19 @@ export default class CSSSplitWebpackPlugin {
187191
// Run on `emit` when user specifies the compiler phase
188192
// Due to the incorrect css split + optimization behavior
189193
// Expected: css split should happen after optimization
190-
if (compiler.hooks) {
191-
compiler.hooks.emit.tapAsync('css-split-emit', (compilation, done) => {
192-
return this.chunksMapping(compilation, compilation.chunks, done);
193-
});
194-
} else {
195-
compiler.plugin('emit', (compilation, done) => {
196-
return this.chunksMapping(compilation, compilation.chunks, done);
197-
});
198-
}
194+
compiler.hooks.emit.tapAsync(name, (compilation, done) => {
195+
return this.chunksMapping(compilation, compilation.chunks, done);
196+
});
199197
} else {
200198
// Only run on `this-compilation` to avoid injecting the plugin into
201-
// sub-compilers as happens when using the `extract-text-webpack-plugin`.
202-
// eslint-disable-next-line no-lonely-if
203-
if (compiler.hooks) {
204-
compiler.hooks.thisCompilation.tap('css-split-this-compilation',
205-
(compilation) => {
206-
compilation.hooks.optimizeChunkAssets.tapAsync(
207-
'css-split-optimize-chunk-assets',
208-
(chunks, done) => {
209-
return this.chunksMapping(compilation, chunks, done);
210-
});
211-
});
212-
} else {
213-
compiler.plugin('this-compilation', (compilation) => {
214-
compilation.plugin('optimize-chunk-assets', (chunks, done) => {
199+
// sub-compilers as happens when using the `mini-css-extract-plugin`.
200+
compiler.hooks.thisCompilation.tap(name, (compilation) => {
201+
// TODO there is no async hook for optimizing chunks
202+
compilation.hooks.optimizeChunkAssets.tapAsync(
203+
name, (chunks, done) => {
215204
return this.chunksMapping(compilation, chunks, done);
216205
});
217-
});
218-
}
206+
});
219207
}
220208
}
221209
}

test/spec/index.spec.js

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import _webpack from 'webpack';
22
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
3-
import OptimizeCssPlugin from 'optimize-css-assets-webpack-plugin';
43
import CSSSplitWebpackPlugin from '../../src';
54
import path from 'path';
65
import MemoryFileSystem from 'memory-fs';
@@ -9,16 +8,6 @@ import {expect} from 'chai';
98
const basic = path.join('.', 'basic', 'index.js');
109
const less = path.join('.', 'less', 'index.js');
1110

12-
const miniCssConfig = {
13-
loader: MiniCssExtractPlugin.loader,
14-
options: {
15-
// you can specify a publicPath here
16-
// by default it uses publicPath in webpackOptions.output
17-
publicPath: '../',
18-
hmr: process.env.NODE_ENV === 'development',
19-
},
20-
};
21-
2211
const config = (options, entry = basic, {
2312
plugins,
2413
...extra
@@ -33,15 +22,9 @@ const config = (options, entry = basic, {
3322
},
3423
module: {
3524
rules: [{
36-
test: /\.css$/,
25+
test: /\.(css|less)$/,
3726
use: [
38-
miniCssConfig,
39-
'css-loader',
40-
],
41-
}, {
42-
test: /\.less$/,
43-
use: [
44-
miniCssConfig,
27+
MiniCssExtractPlugin.loader,
4528
'css-loader',
4629
'less-loader',
4730
],
@@ -67,10 +50,18 @@ const webpack = (options, inst, extra) => {
6750
expect(err).to.be.null;
6851
const stats = _stats.toJson();
6952
const files = {};
53+
const readFile = (name) => compiler.outputFileSystem.readFileSync(
54+
path.join(configuration.output.path, name)
55+
);
7056
stats.assets.forEach((asset) => {
71-
files[asset.name] = compiler.outputFileSystem.readFileSync(
72-
path.join(configuration.output.path, asset.name)
73-
);
57+
// eslint-disable-next-line no-console
58+
files[asset.name] = readFile(asset.name);
59+
60+
if (asset.related[0]) {
61+
asset.related.forEach((related) => {
62+
files[related.name] = readFile(related.name);
63+
});
64+
}
7465
});
7566
resolve({stats, files});
7667
});
@@ -175,12 +166,7 @@ describe('CSSSplitWebpackPlugin', () => {
175166
webpack({
176167
size: 3,
177168
defer: true,
178-
}, basic, {
179-
devtool: false,
180-
plugins: [
181-
new OptimizeCssPlugin(),
182-
],
183-
}).then(({stats, files}) => {
169+
}, basic, {devtool: false}).then(({stats, files}) => {
184170
expect(files).to.not.have.property('styles-1.css.map');
185171
expect(stats.assetsByChunkName)
186172
.to.have.property('main')

0 commit comments

Comments
 (0)