diff --git a/.gitignore b/.gitignore index 8abe4be2..fbc409e4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ /coverage /.idea + +.DS_Store diff --git a/README.md b/README.md index 8c8cd3ac..93c38ee8 100644 --- a/README.md +++ b/README.md @@ -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| @@ -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 + }) +] +``` +

Maintainers

diff --git a/index.js b/index.js index cb1d3259..e6e12b23 100644 --- a/index.js +++ b/index.js @@ -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 }; }; @@ -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); } diff --git a/schema/plugin-schema.json b/schema/plugin-schema.json index 14575863..7b507d17 100644 --- a/schema/plugin-schema.json +++ b/schema/plugin-schema.json @@ -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)", diff --git a/test/cases/multiple-entries-filename/a.js b/test/cases/multiple-entries-filename/a.js new file mode 100644 index 00000000..b3a9ca9d --- /dev/null +++ b/test/cases/multiple-entries-filename/a.js @@ -0,0 +1,4 @@ +require.ensure([], function() { + require("./a.txt"); + require("./b.txt"); +}); diff --git a/test/cases/multiple-entries-filename/a.txt b/test/cases/multiple-entries-filename/a.txt new file mode 100644 index 00000000..78981922 --- /dev/null +++ b/test/cases/multiple-entries-filename/a.txt @@ -0,0 +1 @@ +a diff --git a/test/cases/multiple-entries-filename/b.js b/test/cases/multiple-entries-filename/b.js new file mode 100644 index 00000000..d7f9f0f4 --- /dev/null +++ b/test/cases/multiple-entries-filename/b.js @@ -0,0 +1,4 @@ +require.ensure([], function() { + require("./a.txt"); + require("./c.txt"); +}); diff --git a/test/cases/multiple-entries-filename/b.txt b/test/cases/multiple-entries-filename/b.txt new file mode 100644 index 00000000..61780798 --- /dev/null +++ b/test/cases/multiple-entries-filename/b.txt @@ -0,0 +1 @@ +b diff --git a/test/cases/multiple-entries-filename/c.txt b/test/cases/multiple-entries-filename/c.txt new file mode 100644 index 00000000..f2ad6c76 --- /dev/null +++ b/test/cases/multiple-entries-filename/c.txt @@ -0,0 +1 @@ +c diff --git a/test/cases/multiple-entries-filename/expected/a.txt b/test/cases/multiple-entries-filename/expected/a.txt new file mode 100755 index 00000000..422c2b7a --- /dev/null +++ b/test/cases/multiple-entries-filename/expected/a.txt @@ -0,0 +1,2 @@ +a +b diff --git a/test/cases/multiple-entries-filename/expected/b.txt b/test/cases/multiple-entries-filename/expected/b.txt new file mode 100755 index 00000000..0f7bc766 --- /dev/null +++ b/test/cases/multiple-entries-filename/expected/b.txt @@ -0,0 +1,2 @@ +a +c diff --git a/test/cases/multiple-entries-filename/expected/txt/.DS_Store b/test/cases/multiple-entries-filename/expected/txt/.DS_Store new file mode 100644 index 00000000..fa278feb Binary files /dev/null and b/test/cases/multiple-entries-filename/expected/txt/.DS_Store differ diff --git a/test/cases/multiple-entries-filename/expected/txt/a.txt b/test/cases/multiple-entries-filename/expected/txt/a.txt new file mode 100644 index 00000000..422c2b7a --- /dev/null +++ b/test/cases/multiple-entries-filename/expected/txt/a.txt @@ -0,0 +1,2 @@ +a +b diff --git a/test/cases/multiple-entries-filename/expected/txt/b.txt b/test/cases/multiple-entries-filename/expected/txt/b.txt new file mode 100644 index 00000000..0f7bc766 --- /dev/null +++ b/test/cases/multiple-entries-filename/expected/txt/b.txt @@ -0,0 +1,2 @@ +a +c diff --git a/test/cases/multiple-entries-filename/webpack.config.js b/test/cases/multiple-entries-filename/webpack.config.js new file mode 100644 index 00000000..2337be5a --- /dev/null +++ b/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 + }) + ] +}; \ No newline at end of file