Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: onEmit configuration option #322

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -96,6 +96,14 @@ module.exports = {
// both options are optional
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
// optional callback allows post-processing of the modules produced by the plugin. `modules` is a Set in the
// final output order; you can mutate the set, or return a new set. `chunk.name` provides the name of the chunk
onEmit: function(chunk, modules) {
if (chunk.name === 'some-chunk') {
return processModules(modules);
}
},
}),
})
],
module: {
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Expand Up @@ -500,6 +500,14 @@ class MiniCssExtractPlugin {
modules.sort((a, b) => a.index2 - b.index2);
usedModules = modules;
}

if (this.options.onEmit) {
const replaced = this.options.onEmit(chunk, usedModules);
if (replaced) {
usedModules = replaced;
}
}

const source = new ConcatSource();
const externalsSource = new ConcatSource();
for (const m of usedModules) {
Expand Down
1 change: 1 addition & 0 deletions test/cases/on-emit/a.css
@@ -0,0 +1 @@
body { background: red; }
1 change: 1 addition & 0 deletions test/cases/on-emit/b.css
@@ -0,0 +1 @@
body { background: green; }
4 changes: 4 additions & 0 deletions test/cases/on-emit/expected/main.css
@@ -0,0 +1,4 @@
body { background: green; }

body { background: red; }

2 changes: 2 additions & 0 deletions test/cases/on-emit/index.js
@@ -0,0 +1,2 @@
import './a.css';
import './b.css';
24 changes: 24 additions & 0 deletions test/cases/on-emit/webpack.config.js
@@ -0,0 +1,24 @@
const Self = require('../../../');

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: [Self.loader, 'css-loader'],
},
],
},
plugins: [
new Self({
filename: '[name].css',
onEmit: function(chunk, modules) {
if (chunk.name !== 'main') {
throw new Error('Chunk object was not passed correctly');
}
return new Set(Array.from(modules).reverse());
},
}),
],
};