Skip to content

Commit e810aac

Browse files
committed
refractor filename modify logic
1 parent bf8a6f9 commit e810aac

File tree

13 files changed

+87
-5
lines changed

13 files changed

+87
-5
lines changed

README.md

+24-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|Object}`|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,29 @@ module.exports = {
155155
}
156156
```
157157

158+
### Modify filename
159+
160+
`filename` paramter 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`.
163+
164+
```js
165+
entry: {
166+
'js/a': "./a"
167+
},
168+
plugins: [
169+
new ExtractTextPlugin({
170+
filename: {
171+
format: 'css/[name].css',
172+
modify: (filename) => {
173+
return filename.replace('css/js', 'css');
174+
}
175+
},
176+
allChunks: true
177+
})
178+
]
179+
```
180+
158181
<h2 align="center">Maintainer</h2>
159182

160183
<table>

index.js

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

155+
function isObject(a) {
156+
return isType('Object', a);
157+
}
158+
159+
function isFunction(a) {
160+
return isType('Function', a);
161+
}
162+
163+
function isType(type, obj) {
164+
return Object.prototype.toString.call(obj) === '[object ' + type + ']';
165+
}
166+
155167
ExtractTextPlugin.loader = function(options) {
156168
return { loader: require.resolve("./loader"), options: options };
157169
};
@@ -317,12 +329,23 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
317329
});
318330
var chunk = extractedChunk.originalChunk;
319331
var source = this.renderExtractedChunk(extractedChunk);
320-
var file = compilation.getPath(filename, {
332+
333+
var format = filename;
334+
335+
if (isObject(filename)) {
336+
format = filename.format;
337+
}
338+
339+
var file = compilation.getPath(format, {
321340
chunk: chunk
322341
}).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() {
323342
return loaderUtils.getHashDigest(source.source(), arguments[1], arguments[2], parseInt(arguments[3], 10));
324343
});
325-
file = (options.filenamefilter) ? options.filenamefilter(file) : file;
344+
345+
if (isFunction(filename.modify)) {
346+
file = filename.modify(file);
347+
}
348+
326349
compilation.assets[file] = source;
327350
chunk.files.push(file);
328351
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "extract-text-webpack-plugin-steamer",
2+
"name": "extract-text-webpack-plugin",
33
"version": "2.0.0-rc.3",
44
"author": "Tobias Koppers @sokra",
55
"description": "Extract text from bundle into a file.",

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": "object"
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
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,18 @@
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: {
10+
format: 'txt/[name].txt',
11+
modify: (filename) => {
12+
return filename.replace('txt/js', 'txt');
13+
}
14+
},
15+
allChunks: true
16+
})
17+
]
18+
};

0 commit comments

Comments
 (0)