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

Commit

Permalink
Merge pull request #423 from lcxfs1991/master
Browse files Browse the repository at this point in the history
refractor filename modify logic
  • Loading branch information
bebraw committed Feb 26, 2017
2 parents 7c65449 + e6c6624 commit c9a19ad
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,5 @@
/coverage

/.idea

.DS_Store
21 changes: 20 additions & 1 deletion README.md
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">Maintainers</h2>

<table>
Expand Down
14 changes: 13 additions & 1 deletion index.js
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
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
@@ -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
@@ -0,0 +1 @@
a
4 changes: 4 additions & 0 deletions test/cases/multiple-entries-filename/b.js
@@ -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
@@ -0,0 +1 @@
b
1 change: 1 addition & 0 deletions test/cases/multiple-entries-filename/c.txt
@@ -0,0 +1 @@
c
2 changes: 2 additions & 0 deletions test/cases/multiple-entries-filename/expected/a.txt
@@ -0,0 +1,2 @@
a
b
2 changes: 2 additions & 0 deletions test/cases/multiple-entries-filename/expected/b.txt
@@ -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
@@ -0,0 +1,2 @@
a
b
2 changes: 2 additions & 0 deletions test/cases/multiple-entries-filename/expected/txt/b.txt
@@ -0,0 +1,2 @@
a
c
15 changes: 15 additions & 0 deletions test/cases/multiple-entries-filename/webpack.config.js
@@ -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
})
]
};

0 comments on commit c9a19ad

Please sign in to comment.