Skip to content

Commit

Permalink
refactored ChunkTemplate with plugin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jun 2, 2014
1 parent a8c7dc2 commit a0394a0
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 115 deletions.
17 changes: 4 additions & 13 deletions lib/ChunkTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
};
3 changes: 2 additions & 1 deletion lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions lib/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 0 additions & 32 deletions lib/JsonpChunkTemplate.js

This file was deleted.

26 changes: 26 additions & 0 deletions lib/JsonpChunkTemplatePlugin.js
Original file line number Diff line number Diff line change
@@ -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 + "");
});
};
4 changes: 2 additions & 2 deletions lib/JsonpTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
};
26 changes: 0 additions & 26 deletions lib/node/NodeChunkTemplate.js

This file was deleted.

23 changes: 23 additions & 0 deletions lib/node/NodeChunkTemplatePlugin.js
Original file line number Diff line number Diff line change
@@ -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");
});
};
4 changes: 2 additions & 2 deletions lib/node/NodeTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
};
32 changes: 0 additions & 32 deletions lib/webworker/WebWorkerChunkTemplate.js

This file was deleted.

26 changes: 26 additions & 0 deletions lib/webworker/WebWorkerChunkTemplatePlugin.js
Original file line number Diff line number Diff line change
@@ -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 + "");
});
};
8 changes: 3 additions & 5 deletions lib/webworker/WebWorkerTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit a0394a0

Please sign in to comment.