Skip to content

Commit 13e9cbf

Browse files
kevinchappellevilebottnawi
authored andcommittedMay 10, 2019
feat(options): add moduleFilename option (#381)
1 parent b0a0355 commit 13e9cbf

File tree

7 files changed

+98
-58
lines changed

7 files changed

+98
-58
lines changed
 

Diff for: ‎README.md

+10
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ module.exports = {
342342
};
343343
```
344344

345+
#### Module Filename Option
346+
347+
With the `moduleFilename` option you can use chunk data to customize the filename. This is particularly useful when dealing with multiple entry points and wanting to get more control out of the filename for a given entry point/chunk. In the example below, we'll use `moduleFilename` to output the generated css into a different directory.
348+
349+
```javascript
350+
const miniCssExtractPlugin = new MiniCssExtractPlugin({
351+
moduleFilename: ({ name }) => `${name.replace('/js/', '/css/')}.css`
352+
})
353+
```
354+
345355
#### Long Term Caching
346356

347357
For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.

Diff for: ‎package-lock.json

+56-56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎src/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
1717
const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
1818
const REGEXP_NAME = /\[name\]/i;
1919
const REGEXP_PLACEHOLDERS = /\[(name|id|chunkhash)\]/g;
20+
const DEFAULT_FILENAME = '[name].css';
2021

2122
class CssDependency extends webpack.Dependency {
2223
constructor(
@@ -122,7 +123,8 @@ class MiniCssExtractPlugin {
122123
constructor(options) {
123124
this.options = Object.assign(
124125
{
125-
filename: '[name].css',
126+
filename: DEFAULT_FILENAME,
127+
moduleFilename: () => options.filename || DEFAULT_FILENAME,
126128
},
127129
options
128130
);
@@ -195,7 +197,8 @@ class MiniCssExtractPlugin {
195197
renderedModules,
196198
compilation.runtimeTemplate.requestShortener
197199
),
198-
filenameTemplate: this.options.filename,
200+
filenameTemplate: ({ chunk: chunkData }) =>
201+
this.options.moduleFilename(chunkData),
199202
pathOptions: {
200203
chunk,
201204
contentHashType: MODULE_TYPE,

Diff for: ‎test/cases/moduleFilename/expected/demo/css/main.css

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
body { background: purple; }
2+

Diff for: ‎test/cases/moduleFilename/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';

Diff for: ‎test/cases/moduleFilename/style.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { background: purple; }

Diff for: ‎test/cases/moduleFilename/webpack.config.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const Self = require('../../../');
2+
3+
module.exports = {
4+
entry: {
5+
'demo/js/main': './index.js',
6+
},
7+
module: {
8+
rules: [
9+
{
10+
test: /\.css$/,
11+
use: [Self.loader, 'css-loader'],
12+
},
13+
],
14+
},
15+
output: {
16+
filename: '[name].js',
17+
},
18+
plugins: [
19+
new Self({
20+
moduleFilename: ({ name }) => `${name.replace('/js/', '/css/')}.css`,
21+
}),
22+
],
23+
};

0 commit comments

Comments
 (0)
Please sign in to comment.