Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
refractor filename modify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lcxfs1991 committed Feb 21, 2017
1 parent 1e90af9 commit c525c1f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 27 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ new ExtractTextPlugin(options: filename | object)
|Name|Type|Description|
|:--:|:--:|:----------|
|**`id`**|`{String}`|Unique ident for this plugin instance. (For advanced usage only, by default automatically generated)|
|**`filename`**|`{String|Object}`|Name of the result file. May contain `[name]`, `[id]` and `[contenthash]`|
|**`filename`**|`{String|Function}`|Name of the result file. May contain `[name]`, `[id]` and `[contenthash]`|
|**`allChunks`**|`{Boolean}`|Extract from all additional chunks too (by default it extracts only from the initial chunk(s))|
|**`disable`**|`{Boolean}`|Disables the plugin|
|**`ignoreOrder`**|`{Boolean}`|Disables order check (useful for CSS Modules!), `false` by default|
Expand Down Expand Up @@ -157,21 +157,17 @@ module.exports = {

### Modify filename

`filename` parameter could be `Object`. It accepts `format` and `modify` callback as attributes.
In the following config, before `modify` callback is called, the css path would be `css/js/a.css`.
After `modify` callback, it is transformed to `css/a.css`.
`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`.


```js
entry: {
'js/a': "./a"
},
plugins: [
new ExtractTextPlugin({
filename: {
format: 'css/[name].css',
modify: (filename) => {
return filename.replace('css/js', 'css');
}
filename: (getPath) => {
return getPath('css/[name].css').replace('css/js', 'css');
},
allChunks: true
})
Expand Down
14 changes: 3 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,15 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
});
var chunk = extractedChunk.originalChunk;
var source = this.renderExtractedChunk(extractedChunk);

var format = filename;

if (isObject(filename)) {
format = filename.format;
}

var file = compilation.getPath(format, {
var getPath = (format) => compilation.getPath(format, {
chunk: chunk
}).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() {
return loaderUtils.getHashDigest(source.source(), arguments[1], arguments[2], parseInt(arguments[3], 10));
});

if (isFunction(filename.modify)) {
file = filename.modify(file);
}

var file = (isFunction(filename)) ? filename(getPath) : getPath(filename);

compilation.assets[file] = source;
chunk.files.push(file);
}
Expand Down
2 changes: 1 addition & 1 deletion schema/plugin-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"description": "The filename and path that ExtractTextPlugin will extract to",
"modes": {
"type": "string",
"type": "object"
"type": "function"
}
},
"ignoreOrder": {
Expand Down
2 changes: 2 additions & 0 deletions test/cases/multiple-entries-filename/expected/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a
b
2 changes: 2 additions & 0 deletions test/cases/multiple-entries-filename/expected/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a
c
9 changes: 3 additions & 6 deletions test/cases/multiple-entries-filename/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ module.exports = {
},
plugins: [
new ExtractTextPlugin({
filename: {
format: 'txt/[name].txt',
modify: (filename) => {
return filename.replace('txt/js', 'txt');
}
filename: (getPath) => {
return getPath('txt/[name].txt').replace('txt/js', '');
},
allChunks: true
})
]
};
};

0 comments on commit c525c1f

Please sign in to comment.