From 16ada23d7811d04b216487b3bc08872ce0f12315 Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Fri, 1 Jul 2016 02:29:26 +0800 Subject: [PATCH 01/10] add filename filter to modify path and filename --- README.md | 9 +++++++++ index.js | 1 + 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 02ed7181..12dd3148 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,15 @@ new ExtractTextPlugin([id: string], filename: string, [options]) * `options` * `allChunks` extract from all additional chunks too (by default it extracts only from the initial chunk(s)) * `disable` disables the plugin + * `filenamefilter` function to modify path and filename before files are emitted + ``` + { + filenamefilter: function(filename) { + // modify filename + return filename; + } + } + ``` The `ExtractTextPlugin` generates an output file per entry, so you must use `[name]`, `[id]` or `[contenthash]` when using multiple entries. diff --git a/index.js b/index.js index 82f1acff..a1d4fde9 100644 --- a/index.js +++ b/index.js @@ -302,6 +302,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) { }).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() { return loaderUtils.getHashDigest(source.source(), arguments[1], arguments[2], parseInt(arguments[3], 10)); }); + file = (options.filenamefilter) ? options.filenamefilter(file) : file; compilation.assets[file] = source; chunk.files.push(file); } From fa1a4295337daa51ed58a37d93dfbbbf074ef31f Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Fri, 1 Jul 2016 02:30:55 +0800 Subject: [PATCH 02/10] fix README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 12dd3148..6d2112cf 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ new ExtractTextPlugin([id: string], filename: string, [options]) * `allChunks` extract from all additional chunks too (by default it extracts only from the initial chunk(s)) * `disable` disables the plugin * `filenamefilter` function to modify path and filename before files are emitted + ``` { filenamefilter: function(filename) { From 1411ae0e73efa8282c5117581237a5aca4806fda Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Sun, 10 Jul 2016 10:47:06 +0800 Subject: [PATCH 03/10] add temp repo README --- README.md | 3 +++ package.json | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6d2112cf..541e5aae 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,9 @@ module.exports = { }; ``` +## Maybe a temp repo +This maybe a temporary repository. Once ```extract-text-webpack-plugin``` merge my pull request, please stick to the origin repo. + ## License MIT (http://www.opensource.org/licenses/mit-license.php) diff --git a/package.json b/package.json index 1bca3408..492d3ef8 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "extract-text-webpack-plugin", + "name": "extract-text-webpack-plugin-steamer", "version": "1.0.1", "author": "Tobias Koppers @sokra", "description": "Extract text from bundle into a file.", @@ -24,10 +24,10 @@ "style-loader": "^0.13.0", "webpack": "^1.12.2" }, - "homepage": "http://github.com/webpack/extract-text-webpack-plugin", + "homepage": "http://github.com/lcxfs1991/extract-text-webpack-plugin", "repository": { "type": "git", - "url": "http://github.com/webpack/extract-text-webpack-plugin.git" + "url": "http://github.com/lcxfs1991/extract-text-webpack-plugin.git" }, "license": "MIT", "scripts": { From a5729c740cb3096b0019ce80ef26291a3a7aefa0 Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Mon, 23 Jan 2017 10:06:43 +0800 Subject: [PATCH 04/10] add filename filter --- README.md | 9 +++++++++ index.js | 1 + package.json | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d013a792..28f80817 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,15 @@ new ExtractTextPlugin(options: filename | object) * `options.allChunks: boolean` extract from all additional chunks too (by default it extracts only from the initial chunk(s)) * `options.disable: boolean` disables the plugin * `options.id: string` Unique ident for this plugin instance. (For advanced usage only, by default automatically generated) +* `filenamefilter` function to modify path and filename before files are emitted +``` +{ + filenamefilter: function(filename) { + // modify filename + return filename; + } +} +``` The `ExtractTextPlugin` generates an output file per entry, so you must use `[name]`, `[id]` or `[contenthash]` when using multiple entries. diff --git a/index.js b/index.js index dac5ee5c..ccc79ac3 100644 --- a/index.js +++ b/index.js @@ -309,6 +309,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) { }).replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, function() { return loaderUtils.getHashDigest(source.source(), arguments[1], arguments[2], parseInt(arguments[3], 10)); }); + file = (options.filenamefilter) ? options.filenamefilter(file) : file; compilation.assets[file] = source; chunk.files.push(file); } diff --git a/package.json b/package.json index b0009e7d..c5496d43 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "extract-text-webpack-plugin", + "name": "extract-text-webpack-plugin-steamer", "version": "2.0.0-beta.5", "author": "Tobias Koppers @sokra", "description": "Extract text from bundle into a file.", From 454e0b9b127b72d7101e5674dc9b46ed3274138f Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Mon, 23 Jan 2017 10:11:38 +0800 Subject: [PATCH 05/10] improve readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8665223b..343c08df 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ new ExtractTextPlugin(options: filename | object) * `options.disable: boolean` disables the plugin * `options.id: string` Unique ident for this plugin instance. (For advanced usage only, by default automatically generated) * `filenamefilter` function to modify path and filename before files are emitted -``` +``` javascript { filenamefilter: function(filename) { // modify filename From e810aac9f06c688d035e475173251b2f68d09a70 Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Tue, 21 Feb 2017 23:31:26 +0800 Subject: [PATCH 06/10] refractor filename modify logic --- README.md | 25 +++++++++++++++- index.js | 27 ++++++++++++++++-- package.json | 2 +- schema/plugin-schema.json | 5 +++- test/cases/multiple-entries-filename/a.js | 4 +++ test/cases/multiple-entries-filename/a.txt | 1 + test/cases/multiple-entries-filename/b.js | 4 +++ test/cases/multiple-entries-filename/b.txt | 1 + test/cases/multiple-entries-filename/c.txt | 1 + .../expected/txt/.DS_Store | Bin 0 -> 6148 bytes .../expected/txt/a.txt | 2 ++ .../expected/txt/b.txt | 2 ++ .../webpack.config.js | 18 ++++++++++++ 13 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 test/cases/multiple-entries-filename/a.js create mode 100644 test/cases/multiple-entries-filename/a.txt create mode 100644 test/cases/multiple-entries-filename/b.js create mode 100644 test/cases/multiple-entries-filename/b.txt create mode 100644 test/cases/multiple-entries-filename/c.txt create mode 100644 test/cases/multiple-entries-filename/expected/txt/.DS_Store create mode 100644 test/cases/multiple-entries-filename/expected/txt/a.txt create mode 100644 test/cases/multiple-entries-filename/expected/txt/b.txt create mode 100644 test/cases/multiple-entries-filename/webpack.config.js diff --git a/README.md b/README.md index 39f8eacc..653147e7 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|Object}`|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,29 @@ module.exports = { } ``` +### Modify filename + +`filename` paramter could be `Object`. It accepts `format` and `modify` callback as attributes. +In the following config, before `modify` callback is called, the css path would be `css/js/a.css`. +After `modify` callback, it is transformed to `css/a.css`. + +```js +entry: { + 'js/a': "./a" +}, +plugins: [ + new ExtractTextPlugin({ + filename: { + format: 'css/[name].css', + modify: (filename) => { + return filename.replace('css/js', 'css'); + } + }, + allChunks: true + }) +] +``` +

Maintainer

diff --git a/index.js b/index.js index 017b977f..c8e5d15d 100644 --- a/index.js +++ b/index.js @@ -152,6 +152,18 @@ function isString(a) { return typeof a === "string"; } +function isObject(a) { + return isType('Object', a); +} + +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,12 +329,23 @@ ExtractTextPlugin.prototype.apply = function(compiler) { }); var chunk = extractedChunk.originalChunk; var source = this.renderExtractedChunk(extractedChunk); - var file = compilation.getPath(filename, { + + var format = filename; + + if (isObject(filename)) { + format = filename.format; + } + + var file = 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)); }); - file = (options.filenamefilter) ? options.filenamefilter(file) : file; + + if (isFunction(filename.modify)) { + file = filename.modify(file); + } + compilation.assets[file] = source; chunk.files.push(file); } diff --git a/package.json b/package.json index c901b7a1..737108e7 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "extract-text-webpack-plugin-steamer", + "name": "extract-text-webpack-plugin", "version": "2.0.0-rc.3", "author": "Tobias Koppers @sokra", "description": "Extract text from bundle into a file.", diff --git a/schema/plugin-schema.json b/schema/plugin-schema.json index 14575863..e74edc41 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": "object" + } }, "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/txt/.DS_Store b/test/cases/multiple-entries-filename/expected/txt/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fa278febfe6d67c5939713db36a6ff0abe8f0d7e GIT binary patch literal 6148 zcmeHKF-`+P474GUBGFt)xi3KC2d5|$srdjLl#$W|iSjz04Kucbayp$vMFC^Uo?WkJ zS2x9Z*UWtT_IhvDnb`(Tv=4^4ah*Q1tI7~P_Bibi$Cu%79A>pk33Au=Y);9dGa_KzfrGONW0#ZN<{C5S=vuT@~L`5ke1*E{Y0{lNTII%Ar6XVx`!0S8L zS40}d@szsD%{}1qm_y3fnnG}!$e@X#g?H+eqd{Xt+*~hWhHuw|#Y^aTLgvliU6Sm^p bi@L@!>iWVlG3dw#9jJ={b&*Md-%#KSo%1xL literal 0 HcmV?d00001 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..4c7cf94e --- /dev/null +++ b/test/cases/multiple-entries-filename/webpack.config.js @@ -0,0 +1,18 @@ +var ExtractTextPlugin = require("../../../"); +module.exports = { + entry: { + 'js/a': "./a", + 'js/b': "./b" + }, + plugins: [ + new ExtractTextPlugin({ + filename: { + format: 'txt/[name].txt', + modify: (filename) => { + return filename.replace('txt/js', 'txt'); + } + }, + allChunks: true + }) + ] +}; From 41764e90abe40ac4ca7d22d975bfde86c4c8c261 Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Tue, 21 Feb 2017 23:32:44 +0800 Subject: [PATCH 07/10] refractor filename modify logic --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 737108e7..29a4b8d5 100644 --- a/package.json +++ b/package.json @@ -29,10 +29,10 @@ "style-loader": "^0.13.0", "webpack": "^2.2.0" }, - "homepage": "http://github.com/lcxfs1991/extract-text-webpack-plugin", + "homepage": "http://github.com/webpack/extract-text-webpack-plugin", "repository": { "type": "git", - "url": "http://github.com/lcxfs1991/extract-text-webpack-plugin.git" + "url": "http://github.com/webpack/extract-text-webpack-plugin.git" }, "license": "MIT", "scripts": { From 1e90af971b0fa4cdaaf6c220a5e9ce8a986bfd66 Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Wed, 22 Feb 2017 00:15:23 +0800 Subject: [PATCH 08/10] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 653147e7..3af25cfb 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ module.exports = { ### Modify filename -`filename` paramter could be `Object`. It accepts `format` and `modify` callback as attributes. +`filename` parameter could be `Object`. It accepts `format` and `modify` callback as attributes. In the following config, before `modify` callback is called, the css path would be `css/js/a.css`. After `modify` callback, it is transformed to `css/a.css`. From c525c1ffebd032ebbcd9507a31abb365f453010b Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Wed, 22 Feb 2017 01:25:48 +0800 Subject: [PATCH 09/10] refractor filename modify logic --- README.md | 14 +++++--------- index.js | 14 +++----------- schema/plugin-schema.json | 2 +- .../cases/multiple-entries-filename/expected/a.txt | 2 ++ .../cases/multiple-entries-filename/expected/b.txt | 2 ++ .../multiple-entries-filename/webpack.config.js | 9 +++------ 6 files changed, 16 insertions(+), 27 deletions(-) create mode 100755 test/cases/multiple-entries-filename/expected/a.txt create mode 100755 test/cases/multiple-entries-filename/expected/b.txt diff --git a/README.md b/README.md index 3af25cfb..d5a770ce 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|Object}`|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| @@ -157,9 +157,8 @@ module.exports = { ### Modify filename -`filename` parameter could be `Object`. It accepts `format` and `modify` callback as attributes. -In the following config, before `modify` callback is called, the css path would be `css/js/a.css`. -After `modify` callback, it is transformed to `css/a.css`. +`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: { @@ -167,11 +166,8 @@ entry: { }, plugins: [ new ExtractTextPlugin({ - filename: { - format: 'css/[name].css', - modify: (filename) => { - return filename.replace('css/js', 'css'); - } + filename: (getPath) => { + return getPath('css/[name].css').replace('css/js', 'css'); }, allChunks: true }) diff --git a/index.js b/index.js index c8e5d15d..e104c030 100644 --- a/index.js +++ b/index.js @@ -329,23 +329,15 @@ ExtractTextPlugin.prototype.apply = function(compiler) { }); var chunk = extractedChunk.originalChunk; var source = this.renderExtractedChunk(extractedChunk); - - var format = filename; - - if (isObject(filename)) { - format = filename.format; - } - var file = compilation.getPath(format, { + 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)); }); - - if (isFunction(filename.modify)) { - file = filename.modify(file); - } + 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 e74edc41..7b507d17 100644 --- a/schema/plugin-schema.json +++ b/schema/plugin-schema.json @@ -23,7 +23,7 @@ "description": "The filename and path that ExtractTextPlugin will extract to", "modes": { "type": "string", - "type": "object" + "type": "function" } }, "ignoreOrder": { 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/webpack.config.js b/test/cases/multiple-entries-filename/webpack.config.js index 4c7cf94e..2337be5a 100644 --- a/test/cases/multiple-entries-filename/webpack.config.js +++ b/test/cases/multiple-entries-filename/webpack.config.js @@ -6,13 +6,10 @@ module.exports = { }, plugins: [ new ExtractTextPlugin({ - filename: { - format: 'txt/[name].txt', - modify: (filename) => { - return filename.replace('txt/js', 'txt'); - } + filename: (getPath) => { + return getPath('txt/[name].txt').replace('txt/js', ''); }, allChunks: true }) ] -}; +}; \ No newline at end of file From 4356fe5a512e8b477e3783e95342d3e760de7b9f Mon Sep 17 00:00:00 2001 From: "lcxfs1991@gmail.com" Date: Wed, 22 Feb 2017 01:47:24 +0800 Subject: [PATCH 10/10] increate test coverage rate --- .gitignore | 2 ++ index.js | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) 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/index.js b/index.js index e104c030..e6e12b23 100644 --- a/index.js +++ b/index.js @@ -152,10 +152,6 @@ function isString(a) { return typeof a === "string"; } -function isObject(a) { - return isType('Object', a); -} - function isFunction(a) { return isType('Function', a); }