Skip to content

Commit c525c1f

Browse files
committed
refractor filename modify logic
1 parent 1e90af9 commit c525c1f

File tree

6 files changed

+16
-27
lines changed

6 files changed

+16
-27
lines changed

README.md

+5-9
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ new ExtractTextPlugin(options: filename | object)
6868
|Name|Type|Description|
6969
|:--:|:--:|:----------|
7070
|**`id`**|`{String}`|Unique ident for this plugin instance. (For advanced usage only, by default automatically generated)|
71-
|**`filename`**|`{String|Object}`|Name of the result file. May contain `[name]`, `[id]` and `[contenthash]`|
71+
|**`filename`**|`{String|Function}`|Name of the result file. May contain `[name]`, `[id]` and `[contenthash]`|
7272
|**`allChunks`**|`{Boolean}`|Extract from all additional chunks too (by default it extracts only from the initial chunk(s))|
7373
|**`disable`**|`{Boolean}`|Disables the plugin|
7474
|**`ignoreOrder`**|`{Boolean}`|Disables order check (useful for CSS Modules!), `false` by default|
@@ -157,21 +157,17 @@ module.exports = {
157157

158158
### Modify filename
159159

160-
`filename` parameter could be `Object`. It accepts `format` and `modify` callback as attributes.
161-
In the following config, before `modify` callback is called, the css path would be `css/js/a.css`.
162-
After `modify` callback, it is transformed to `css/a.css`.
160+
`filename` parameter could be `Function`. It passes `getPath` to process the format like `css/[name].css` and returns the real file name, `css/js/a.css`. You can replace `css/js` with `css` then you will get the new path `css/a.css`.
161+
163162

164163
```js
165164
entry: {
166165
'js/a': "./a"
167166
},
168167
plugins: [
169168
new ExtractTextPlugin({
170-
filename: {
171-
format: 'css/[name].css',
172-
modify: (filename) => {
173-
return filename.replace('css/js', 'css');
174-
}
169+
filename: (getPath) => {
170+
return getPath('css/[name].css').replace('css/js', 'css');
175171
},
176172
allChunks: true
177173
})

index.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -329,23 +329,15 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
329329
});
330330
var chunk = extractedChunk.originalChunk;
331331
var source = this.renderExtractedChunk(extractedChunk);
332-
333-
var format = filename;
334-
335-
if (isObject(filename)) {
336-
format = filename.format;
337-
}
338332

339-
var file = compilation.getPath(format, {
333+
var getPath = (format) => compilation.getPath(format, {
340334
chunk: chunk
341335
}).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() {
342336
return loaderUtils.getHashDigest(source.source(), arguments[1], arguments[2], parseInt(arguments[3], 10));
343337
});
344-
345-
if (isFunction(filename.modify)) {
346-
file = filename.modify(file);
347-
}
348338

339+
var file = (isFunction(filename)) ? filename(getPath) : getPath(filename);
340+
349341
compilation.assets[file] = source;
350342
chunk.files.push(file);
351343
}

schema/plugin-schema.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"description": "The filename and path that ExtractTextPlugin will extract to",
2424
"modes": {
2525
"type": "string",
26-
"type": "object"
26+
"type": "function"
2727
}
2828
},
2929
"ignoreOrder": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a
2+
b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a
2+
c

test/cases/multiple-entries-filename/webpack.config.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ module.exports = {
66
},
77
plugins: [
88
new ExtractTextPlugin({
9-
filename: {
10-
format: 'txt/[name].txt',
11-
modify: (filename) => {
12-
return filename.replace('txt/js', 'txt');
13-
}
9+
filename: (getPath) => {
10+
return getPath('txt/[name].txt').replace('txt/js', '');
1411
},
1512
allChunks: true
1613
})
1714
]
18-
};
15+
};

0 commit comments

Comments
 (0)