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

refractor filename modify logic #423

Merged
merged 14 commits into from
Feb 26, 2017
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
/coverage

/.idea

.DS_Store
21 changes: 20 additions & 1 deletion 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}`|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 @@ -155,6 +155,25 @@ module.exports = {
}
```

### Modify filename

`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: (getPath) => {
return getPath('css/[name].css').replace('css/js', 'css');
},
allChunks: true
})
]
```

<h2 align="center">Maintainer</h2>

<table>
Expand Down
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ function isString(a) {
return typeof a === "string";
}

function isFunction(a) {
return isType('Function', a);
}

function isType(type, obj) {
return Object.prototype.toString.call(obj) === '[object ' + type + ']';
}

ExtractTextPlugin.loader = function(options) {
return { loader: require.resolve("./loader"), options: options };
};
Expand Down Expand Up @@ -317,11 +325,15 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
});
var chunk = extractedChunk.originalChunk;
var source = this.renderExtractedChunk(extractedChunk);
var file = compilation.getPath(filename, {

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));
});

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

compilation.assets[file] = source;
chunk.files.push(file);
}
Expand Down
5 changes: 4 additions & 1 deletion schema/plugin-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
},
"filename": {
"description": "The filename and path that ExtractTextPlugin will extract to",
"type": "string"
"modes": {
"type": "string",
"type": "function"
}
},
"ignoreOrder": {
"description": "Ignore dependency order (useful for CSS Modules)",
Expand Down
4 changes: 4 additions & 0 deletions test/cases/multiple-entries-filename/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require.ensure([], function() {
require("./a.txt");
require("./b.txt");
});
1 change: 1 addition & 0 deletions test/cases/multiple-entries-filename/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
4 changes: 4 additions & 0 deletions test/cases/multiple-entries-filename/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require.ensure([], function() {
require("./a.txt");
require("./c.txt");
});
1 change: 1 addition & 0 deletions test/cases/multiple-entries-filename/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions test/cases/multiple-entries-filename/c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c
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
Binary file not shown.
2 changes: 2 additions & 0 deletions test/cases/multiple-entries-filename/expected/txt/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/txt/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a
c
15 changes: 15 additions & 0 deletions test/cases/multiple-entries-filename/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var ExtractTextPlugin = require("../../../");
module.exports = {
entry: {
'js/a': "./a",
'js/b': "./b"
},
plugins: [
new ExtractTextPlugin({
filename: (getPath) => {
return getPath('txt/[name].txt').replace('txt/js', '');
},
allChunks: true
})
]
};