Skip to content

Commit c9a19ad

Browse files
authored
Merge pull request #423 from lcxfs1991/master
refractor filename modify logic
2 parents 7c65449 + e6c6624 commit c9a19ad

File tree

15 files changed

+73
-3
lines changed

15 files changed

+73
-3
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
/coverage
77

88
/.idea
9+
10+
.DS_Store

README.md

+20-1
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}`|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|
@@ -155,6 +155,25 @@ module.exports = {
155155
}
156156
```
157157

158+
### Modify filename
159+
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+
162+
163+
```js
164+
entry: {
165+
'js/a': "./a"
166+
},
167+
plugins: [
168+
new ExtractTextPlugin({
169+
filename: (getPath) => {
170+
return getPath('css/[name].css').replace('css/js', 'css');
171+
},
172+
allChunks: true
173+
})
174+
]
175+
```
176+
158177
<h2 align="center">Maintainers</h2>
159178

160179
<table>

index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ function isString(a) {
152152
return typeof a === "string";
153153
}
154154

155+
function isFunction(a) {
156+
return isType('Function', a);
157+
}
158+
159+
function isType(type, obj) {
160+
return Object.prototype.toString.call(obj) === '[object ' + type + ']';
161+
}
162+
155163
ExtractTextPlugin.loader = function(options) {
156164
return { loader: require.resolve("./loader"), options: options };
157165
};
@@ -317,11 +325,15 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
317325
});
318326
var chunk = extractedChunk.originalChunk;
319327
var source = this.renderExtractedChunk(extractedChunk);
320-
var file = compilation.getPath(filename, {
328+
329+
var getPath = (format) => compilation.getPath(format, {
321330
chunk: chunk
322331
}).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() {
323332
return loaderUtils.getHashDigest(source.source(), arguments[1], arguments[2], parseInt(arguments[3], 10));
324333
});
334+
335+
var file = (isFunction(filename)) ? filename(getPath) : getPath(filename);
336+
325337
compilation.assets[file] = source;
326338
chunk.files.push(file);
327339
}

schema/plugin-schema.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
},
2222
"filename": {
2323
"description": "The filename and path that ExtractTextPlugin will extract to",
24-
"type": "string"
24+
"modes": {
25+
"type": "string",
26+
"type": "function"
27+
}
2528
},
2629
"ignoreOrder": {
2730
"description": "Ignore dependency order (useful for CSS Modules)",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require.ensure([], function() {
2+
require("./a.txt");
3+
require("./b.txt");
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require.ensure([], function() {
2+
require("./a.txt");
3+
require("./c.txt");
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c
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
Binary file not shown.
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var ExtractTextPlugin = require("../../../");
2+
module.exports = {
3+
entry: {
4+
'js/a': "./a",
5+
'js/b': "./b"
6+
},
7+
plugins: [
8+
new ExtractTextPlugin({
9+
filename: (getPath) => {
10+
return getPath('txt/[name].txt').replace('txt/js', '');
11+
},
12+
allChunks: true
13+
})
14+
]
15+
};

0 commit comments

Comments
 (0)