Skip to content

Commit 7d24229

Browse files
refactor: remove moduleFilename option in favor filename option
1 parent 23c7b9f commit 7d24229

File tree

6 files changed

+51
-32
lines changed

6 files changed

+51
-32
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,11 @@ module.exports = {
511511
};
512512
```
513513

514-
### Module Filename Option
514+
### Filename Option as function
515515

516-
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.
516+
With the `filename` option you can use chunk data to customize the filename.
517+
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.
518+
In the example below, we'll use `filename` to output the generated css into a different directory.
517519

518520
**webpack.config.js**
519521

@@ -523,7 +525,8 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
523525
module.exports = {
524526
plugins: [
525527
new MiniCssExtractPlugin({
526-
moduleFilename: ({ name }) => `${name.replace('/js/', '/css/')}.css`,
528+
filename: ({ pathData }) =>
529+
`${pathData.chunk.name.replace('/js/', '/css/')}.css`,
527530
}),
528531
],
529532
module: {

src/index.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const pluginName = 'mini-css-extract-plugin';
2121
const REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/i;
2222
const REGEXP_CONTENTHASH = /\[contenthash(?::(\d+))?\]/i;
2323
const REGEXP_NAME = /\[name\]/i;
24-
const REGEXP_PLACEHOLDERS = /\[(name|id|chunkhash)\]/g;
2524
const DEFAULT_FILENAME = '[name].css';
2625

2726
class CssDependencyTemplate {
@@ -108,7 +107,6 @@ class MiniCssExtractPlugin {
108107
this.options = Object.assign(
109108
{
110109
filename: DEFAULT_FILENAME,
111-
moduleFilename: () => this.options.filename || DEFAULT_FILENAME,
112110
ignoreOrder: false,
113111
},
114112
options
@@ -117,15 +115,24 @@ class MiniCssExtractPlugin {
117115
if (!this.options.chunkFilename) {
118116
const { filename } = this.options;
119117

120-
// Anything changing depending on chunk is fine
121-
if (filename.match(REGEXP_PLACEHOLDERS)) {
122-
this.options.chunkFilename = filename;
118+
if (typeof filename !== 'function') {
119+
const hasName = filename.includes('[name]');
120+
const hasId = filename.includes('[id]');
121+
const hasChunkHash = filename.includes('[chunkhash]');
122+
const hasContentHash = filename.includes('[contenthash]');
123+
124+
// Anything changing depending on chunk is fine
125+
if (hasChunkHash || hasContentHash || hasName || hasId) {
126+
this.options.chunkFilename = filename;
127+
} else {
128+
// Otherwise prefix "[id]." in front of the basename to make it changing
129+
this.options.chunkFilename = filename.replace(
130+
/(^|\/)([^/]*(?:\?|$))/,
131+
'$1[id].$2'
132+
);
133+
}
123134
} else {
124-
// Elsewise prefix '[id].' in front of the basename to make it changing
125-
this.options.chunkFilename = filename.replace(
126-
/(^|\/)([^/]*(?:\?|$))/,
127-
'$1[id].$2'
128-
);
135+
this.options.chunkFilename = '[id].css';
129136
}
130137
}
131138
}
@@ -149,6 +156,9 @@ class MiniCssExtractPlugin {
149156
(module) => module.type === MODULE_TYPE
150157
);
151158

159+
const filenameTemplate =
160+
chunk.filenameTemplate || this.options.filename;
161+
152162
if (renderedModules.length > 0) {
153163
result.push({
154164
render: () =>
@@ -158,8 +168,7 @@ class MiniCssExtractPlugin {
158168
renderedModules,
159169
compilation.runtimeTemplate.requestShortener
160170
),
161-
filenameTemplate: ({ chunk: chunkData }) =>
162-
this.options.moduleFilename(chunkData),
171+
filenameTemplate,
163172
pathOptions: {
164173
chunk,
165174
contentHashType: MODULE_TYPE,
@@ -178,6 +187,9 @@ class MiniCssExtractPlugin {
178187
(module) => module.type === MODULE_TYPE
179188
);
180189

190+
const filenameTemplate =
191+
chunk.filenameTemplate || this.options.chunkFilename;
192+
181193
if (renderedModules.length > 0) {
182194
result.push({
183195
render: () =>
@@ -187,7 +199,7 @@ class MiniCssExtractPlugin {
187199
renderedModules,
188200
compilation.runtimeTemplate.requestShortener
189201
),
190-
filenameTemplate: this.options.chunkFilename,
202+
filenameTemplate,
191203
pathOptions: {
192204
chunk,
193205
contentHashType: MODULE_TYPE,

src/plugin-options.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
"additionalProperties": true,
44
"properties": {
55
"filename": {
6-
"type": "string"
6+
"anyOf": [
7+
{
8+
"type": "string"
9+
},
10+
{
11+
"instanceof": "Function"
12+
}
13+
]
714
},
815
"chunkFilename": {
916
"type": "string"
1017
},
11-
"moduleFilename": {
12-
"instanceof": "Function"
13-
},
1418
"ignoreOrder": {
1519
"type": "boolean"
1620
}

test/__snapshots__/validate-plugin-options.test.js.snap

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ exports[`validate options should throw an error on the "chunkFilename" option wi
77

88
exports[`validate options should throw an error on the "filename" option with "true" value 1`] = `
99
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
10-
- options.filename should be a string."
10+
- options.filename should be one of these:
11+
string | function
12+
Details:
13+
* options.filename should be a string.
14+
* options.filename should be an instance of function."
1115
`;
1216

1317
exports[`validate options should throw an error on the "ignoreOrder" option with "1" value 1`] = `
1418
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
1519
- options.ignoreOrder should be a boolean."
1620
`;
17-
18-
exports[`validate options should throw an error on the "moduleFilename" option with "true" value 1`] = `
19-
"Invalid options object. Mini CSS Extract Plugin has been initialized using an options object that does not match the API schema.
20-
- options.moduleFilename should be an instance of function."
21-
`;

test/cases/moduleFilename/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ module.exports = {
1717
},
1818
plugins: [
1919
new Self({
20-
moduleFilename: ({ name }) => `${name.replace('/js/', '/css/')}.css`,
20+
filename: (pathData) => {
21+
return `${pathData.chunk.name.replace('/js/', '/css/')}.css`;
22+
},
2123
}),
2224
],
2325
};

test/validate-plugin-options.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import MiniCssExtractPlugin from '../src';
33
describe('validate options', () => {
44
const tests = {
55
filename: {
6-
success: ['[name].css'],
6+
success: [
7+
'[name].css',
8+
({ pathData }) => `${pathData.chunk.name.replace('/js/', '/css/')}.css`,
9+
],
710
failure: [true],
811
},
912
chunkFilename: {
1013
success: ['[id].css'],
1114
failure: [true],
1215
},
13-
moduleFilename: {
14-
success: [({ name }) => `${name.replace('/js/', '/css/')}.css`],
15-
failure: [true],
16-
},
1716
ignoreOrder: {
1817
success: [true, false],
1918
failure: [1],

0 commit comments

Comments
 (0)