From a0394a061b832789693909214e0b46feabf61d77 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 2 Jun 2014 23:40:50 +0200 Subject: [PATCH] refactored ChunkTemplate with plugin interface --- lib/ChunkTemplate.js | 17 +++------- lib/Compilation.js | 3 +- lib/Compiler.js | 3 +- lib/JsonpChunkTemplate.js | 32 ------------------- lib/JsonpChunkTemplatePlugin.js | 26 +++++++++++++++ lib/JsonpTemplatePlugin.js | 4 +-- lib/node/NodeChunkTemplate.js | 26 --------------- lib/node/NodeChunkTemplatePlugin.js | 23 +++++++++++++ lib/node/NodeTemplatePlugin.js | 4 +-- lib/webworker/WebWorkerChunkTemplate.js | 32 ------------------- lib/webworker/WebWorkerChunkTemplatePlugin.js | 26 +++++++++++++++ lib/webworker/WebWorkerTemplatePlugin.js | 8 ++--- 12 files changed, 89 insertions(+), 115 deletions(-) delete mode 100644 lib/JsonpChunkTemplate.js create mode 100644 lib/JsonpChunkTemplatePlugin.js delete mode 100644 lib/node/NodeChunkTemplate.js create mode 100644 lib/node/NodeChunkTemplatePlugin.js delete mode 100644 lib/webworker/WebWorkerChunkTemplate.js create mode 100644 lib/webworker/WebWorkerChunkTemplatePlugin.js diff --git a/lib/ChunkTemplate.js b/lib/ChunkTemplate.js index 4d7a19629aa..72e79ec7af6 100644 --- a/lib/ChunkTemplate.js +++ b/lib/ChunkTemplate.js @@ -2,7 +2,6 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -var ConcatSource = require("webpack-core/lib/ConcatSource"); var Template = require("./Template"); function ChunkTemplate(outputOptions) { @@ -13,23 +12,15 @@ module.exports = ChunkTemplate; ChunkTemplate.prototype = Object.create(Template.prototype); ChunkTemplate.prototype.render = function(chunk, moduleTemplate, dependencyTemplates) { - var source = new ConcatSource(); - source.add(this.asString(this.renderHeader(chunk))); - source.add(this.renderChunkModules(chunk, moduleTemplate, dependencyTemplates)); - source.add(this.asString(this.renderFooter(chunk))); + var modules = this.renderChunkModules(chunk, moduleTemplate, dependencyTemplates); + var core = this.applyPluginsWaterfall("modules", modules, chunk, moduleTemplate, dependencyTemplates); + var source = this.applyPluginsWaterfall("render", core, chunk, moduleTemplate, dependencyTemplates); chunk.rendered = true; return source; }; -ChunkTemplate.prototype.renderHeader = function(chunk) { - return []; -}; - -ChunkTemplate.prototype.renderFooter = function(chunk) { - return []; -}; - ChunkTemplate.prototype.updateHash = function(hash) { hash.update("ChunkTemplate"); hash.update("2"); + this.applyPlugins("hash", hash); }; \ No newline at end of file diff --git a/lib/Compilation.js b/lib/Compilation.js index ad34caaf191..55d65b9bc2b 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -16,6 +16,7 @@ var Chunk = require("./Chunk"); var Stats = require("./Stats"); var Template = require("./Template"); var MainTemplate = require("./MainTemplate"); +var ChunkTemplate = require("./ChunkTemplate"); function Compilation(compiler) { Tapable.call(this); @@ -29,7 +30,7 @@ function Compilation(compiler) { this.profile = options && options.profile; this.mainTemplate = new MainTemplate(this.outputOptions); - this.chunkTemplate = compiler.chunkTemplate; + this.chunkTemplate = new ChunkTemplate(this.outputOptions); this.hotUpdateChunkTemplate = compiler.hotUpdateChunkTemplate; this.moduleTemplate = compiler.moduleTemplate; diff --git a/lib/Compiler.js b/lib/Compiler.js index 9d85222042f..4880b11fa89 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -122,7 +122,7 @@ Watching.prototype.close = function(callback) { function Compiler() { Tapable.call(this); - this.chunkTemplate = this.moduleTemplate = null; + this.moduleTemplate = null; this.outputPath = ""; this.outputFileSystem = null; @@ -309,7 +309,6 @@ Compiler.prototype.createChildCompiler = function(compilation, compilerName, out childCompiler._plugins[name] = this._plugins[name].slice(); } childCompiler.name = compilerName; - childCompiler.chunkTemplate = this.chunkTemplate; childCompiler.moduleTemplate = this.moduleTemplate; childCompiler.outputPath = this.outputPath; childCompiler.inputFileSystem = this.inputFileSystem; diff --git a/lib/JsonpChunkTemplate.js b/lib/JsonpChunkTemplate.js deleted file mode 100644 index 762738b2b04..00000000000 --- a/lib/JsonpChunkTemplate.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - MIT License http://www.opensource.org/licenses/mit-license.php - Author Tobias Koppers @sokra -*/ -var ChunkTemplate = require("./ChunkTemplate"); - -function JsonpChunkTemplate(outputOptions) { - ChunkTemplate.call(this, outputOptions); -} -module.exports = JsonpChunkTemplate; - -JsonpChunkTemplate.prototype = Object.create(ChunkTemplate.prototype); -JsonpChunkTemplate.prototype.renderHeader = function(chunk) { - var buf = ChunkTemplate.prototype.renderHeader.call(this, chunk); - var jsonpFunction = this.outputOptions.jsonpFunction || ("webpackJsonp" + (this.outputOptions.library || "")); - buf.unshift(jsonpFunction + "(" + JSON.stringify(chunk.ids) + ","); - return buf; -}; - -JsonpChunkTemplate.prototype.renderFooter = function(chunk) { - var buf = ChunkTemplate.prototype.renderFooter.call(this, chunk); - buf.push(")"); - return buf; -}; - -JsonpChunkTemplate.prototype.updateHash = function(hash) { - ChunkTemplate.prototype.updateHash.call(this, hash); - hash.update("JsonpChunkTemplate"); - hash.update("3"); - hash.update(this.outputOptions.jsonpFunction + ""); - hash.update(this.outputOptions.library + ""); -}; \ No newline at end of file diff --git a/lib/JsonpChunkTemplatePlugin.js b/lib/JsonpChunkTemplatePlugin.js new file mode 100644 index 00000000000..cb81396dc59 --- /dev/null +++ b/lib/JsonpChunkTemplatePlugin.js @@ -0,0 +1,26 @@ +/* + MIT License http://www.opensource.org/licenses/mit-license.php + Author Tobias Koppers @sokra +*/ +var ConcatSource = require("webpack-core/lib/ConcatSource"); + +function JsonpChunkTemplatePlugin() { +} +module.exports = JsonpChunkTemplatePlugin; + +JsonpChunkTemplatePlugin.prototype.apply = function(chunkTemplate) { + chunkTemplate.plugin("render", function(modules, chunk) { + var jsonpFunction = this.outputOptions.jsonpFunction || ("webpackJsonp" + (this.outputOptions.library || "")); + var source = new ConcatSource(); + source.add(jsonpFunction + "(" + JSON.stringify(chunk.ids) + ","); + source.add(modules); + source.add(");"); + return source; + }); + chunkTemplate.plugin("hash", function(hash) { + hash.update("JsonpChunkTemplatePlugin"); + hash.update("3"); + hash.update(this.outputOptions.jsonpFunction + ""); + hash.update(this.outputOptions.library + ""); + }); +}; diff --git a/lib/JsonpTemplatePlugin.js b/lib/JsonpTemplatePlugin.js index 5498219db36..0550c4804d6 100644 --- a/lib/JsonpTemplatePlugin.js +++ b/lib/JsonpTemplatePlugin.js @@ -3,7 +3,7 @@ Author Tobias Koppers @sokra */ var JsonpMainTemplatePlugin = require("./JsonpMainTemplatePlugin"); -var JsonpChunkTemplate = require("./JsonpChunkTemplate"); +var JsonpChunkTemplatePlugin = require("./JsonpChunkTemplatePlugin"); var JsonpHotUpdateChunkTemplate = require("./JsonpHotUpdateChunkTemplate"); function JsonpTemplatePlugin(options) { @@ -14,7 +14,7 @@ JsonpTemplatePlugin.prototype.apply = function(compiler) { var options = this.options; compiler.plugin("this-compilation", function(compilation) { compilation.mainTemplate.apply(new JsonpMainTemplatePlugin()); + compilation.chunkTemplate.apply(new JsonpChunkTemplatePlugin()); }); - compiler.chunkTemplate = new JsonpChunkTemplate(options); compiler.hotUpdateChunkTemplate = new JsonpHotUpdateChunkTemplate(options); }; \ No newline at end of file diff --git a/lib/node/NodeChunkTemplate.js b/lib/node/NodeChunkTemplate.js deleted file mode 100644 index 812039f0282..00000000000 --- a/lib/node/NodeChunkTemplate.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - MIT License http://www.opensource.org/licenses/mit-license.php - Author Tobias Koppers @sokra -*/ -var ChunkTemplate = require("../ChunkTemplate"); - -function NodeChunkTemplate(outputOptions) { - ChunkTemplate.call(this, outputOptions); -} -module.exports = NodeChunkTemplate; - -NodeChunkTemplate.prototype = Object.create(ChunkTemplate.prototype); -NodeChunkTemplate.prototype.renderHeader = function(chunk) { - var buf = ChunkTemplate.prototype.renderHeader.call(this, chunk); - buf.unshift( - "exports.ids = " + JSON.stringify(chunk.ids) + ";\n", - "exports.modules = " - ); - return buf; -}; - -NodeChunkTemplate.prototype.updateHash = function(hash) { - ChunkTemplate.prototype.updateHash.call(this, hash); - hash.update("node"); - hash.update("3"); -}; \ No newline at end of file diff --git a/lib/node/NodeChunkTemplatePlugin.js b/lib/node/NodeChunkTemplatePlugin.js new file mode 100644 index 00000000000..b3bd7aadb4f --- /dev/null +++ b/lib/node/NodeChunkTemplatePlugin.js @@ -0,0 +1,23 @@ +/* + MIT License http://www.opensource.org/licenses/mit-license.php + Author Tobias Koppers @sokra +*/ +var ConcatSource = require("webpack-core/lib/ConcatSource"); + +function NodeChunkTemplatePlugin() { +} +module.exports = NodeChunkTemplatePlugin; + +NodeChunkTemplatePlugin.prototype.apply = function(chunkTemplate) { + chunkTemplate.plugin("render", function(modules, chunk) { + var source = new ConcatSource(); + source.add("exports.ids = " + JSON.stringify(chunk.ids) + ";\nexports.modules = "); + source.add(modules); + source.add(";"); + return source; + }); + chunkTemplate.plugin("hash", function(hash) { + hash.update("node"); + hash.update("3"); + }); +}; diff --git a/lib/node/NodeTemplatePlugin.js b/lib/node/NodeTemplatePlugin.js index 0f7179f358d..7a051e0168b 100644 --- a/lib/node/NodeTemplatePlugin.js +++ b/lib/node/NodeTemplatePlugin.js @@ -3,7 +3,7 @@ Author Tobias Koppers @sokra */ var NodeMainTemplatePlugin = require("./NodeMainTemplatePlugin"); -var NodeChunkTemplate = require("./NodeChunkTemplate"); +var NodeChunkTemplatePlugin = require("./NodeChunkTemplatePlugin"); var NodeHotUpdateChunkTemplate = require("./NodeHotUpdateChunkTemplate"); function NodeTemplatePlugin(options, asyncChunkLoading) { @@ -15,7 +15,7 @@ NodeTemplatePlugin.prototype.apply = function(compiler) { var options = this.options; compiler.plugin("this-compilation", function(compilation) { compilation.mainTemplate.apply(new NodeMainTemplatePlugin(this.asyncChunkLoading)); + compilation.chunkTemplate.apply(new NodeChunkTemplatePlugin()); }.bind(this)); - compiler.chunkTemplate = new NodeChunkTemplate(options); compiler.hotUpdateChunkTemplate = new NodeHotUpdateChunkTemplate(options); }; diff --git a/lib/webworker/WebWorkerChunkTemplate.js b/lib/webworker/WebWorkerChunkTemplate.js deleted file mode 100644 index 209ae6ae1be..00000000000 --- a/lib/webworker/WebWorkerChunkTemplate.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - MIT License http://www.opensource.org/licenses/mit-license.php - Author Tobias Koppers @sokra -*/ -var ChunkTemplate = require("../ChunkTemplate"); - -function WebWorkerChunkTemplate(outputOptions) { - ChunkTemplate.call(this, outputOptions); -} -module.exports = WebWorkerChunkTemplate; - -WebWorkerChunkTemplate.prototype = Object.create(ChunkTemplate.prototype); -WebWorkerChunkTemplate.prototype.renderHeader = function(chunk) { - var buf = ChunkTemplate.prototype.renderHeader.call(this, chunk); - var chunkCallbackName = this.outputOptions.chunkCallbackName || ("webpackChunk" + (this.outputOptions.library || "")); - buf.unshift(chunkCallbackName + "(" + JSON.stringify(chunk.ids) + ","); - return buf; -}; - -WebWorkerChunkTemplate.prototype.renderFooter = function(chunk) { - var buf = ChunkTemplate.prototype.renderFooter.call(this, chunk); - buf.push(")"); - return buf; -}; - -WebWorkerChunkTemplate.prototype.updateHash = function(hash) { - ChunkTemplate.prototype.updateHash.call(this, hash); - hash.update("webworker"); - hash.update("3"); - hash.update(this.outputOptions.chunkCallbackName + ""); - hash.update(this.outputOptions.library + ""); -}; \ No newline at end of file diff --git a/lib/webworker/WebWorkerChunkTemplatePlugin.js b/lib/webworker/WebWorkerChunkTemplatePlugin.js new file mode 100644 index 00000000000..d31752c83b6 --- /dev/null +++ b/lib/webworker/WebWorkerChunkTemplatePlugin.js @@ -0,0 +1,26 @@ +/* + MIT License http://www.opensource.org/licenses/mit-license.php + Author Tobias Koppers @sokra +*/ +var ConcatSource = require("webpack-core/lib/ConcatSource"); + +function WebWorkerChunkTemplatePlugin() { +} +module.exports = WebWorkerChunkTemplatePlugin; + +WebWorkerChunkTemplatePlugin.prototype.apply = function(chunkTemplate) { + chunkTemplate.plugin("render", function(modules, chunk) { + var chunkCallbackName = this.outputOptions.chunkCallbackName || ("webpackChunk" + (this.outputOptions.library || "")); + var source = new ConcatSource(); + source.add(chunkCallbackName + "(" + JSON.stringify(chunk.ids) + ","); + source.add(modules); + source.add(")"); + return source; + }); + chunkTemplate.plugin("hash", function(hash) { + hash.update("webworker"); + hash.update("3"); + hash.update(this.outputOptions.chunkCallbackName + ""); + hash.update(this.outputOptions.library + ""); + }); +}; diff --git a/lib/webworker/WebWorkerTemplatePlugin.js b/lib/webworker/WebWorkerTemplatePlugin.js index 23b619be889..ed40764f2d0 100644 --- a/lib/webworker/WebWorkerTemplatePlugin.js +++ b/lib/webworker/WebWorkerTemplatePlugin.js @@ -3,16 +3,14 @@ Author Tobias Koppers @sokra */ var WebWorkerMainTemplatePlugin = require("./WebWorkerMainTemplatePlugin"); -var WebWorkerChunkTemplate = require("./WebWorkerChunkTemplate"); +var WebWorkerChunkTemplatePlugin = require("./WebWorkerChunkTemplatePlugin"); -function WebWorkerTemplatePlugin(options) { - this.options = options; +function WebWorkerTemplatePlugin() { } module.exports = WebWorkerTemplatePlugin; WebWorkerTemplatePlugin.prototype.apply = function(compiler) { - var options = this.options; compiler.plugin("this-compilation", function(compilation) { compilation.mainTemplate.apply(new WebWorkerMainTemplatePlugin()); + compilation.chunkTemplate.apply(new WebWorkerChunkTemplatePlugin()); }); - compiler.chunkTemplate = new WebWorkerChunkTemplate(options); }; \ No newline at end of file