Skip to content

Commit

Permalink
Merge pull request webpack#3615 from abouthiroppy/feature/refactor-Am…
Browse files Browse the repository at this point in the history
…dMainTemplatePlugin

refactor(AmdMainTemplatePlugin): update AmdMainTemplatePlugin to es2015
  • Loading branch information
sokra authored Dec 30, 2016
2 parents 6292940 + 750a542 commit dc0f4e5
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions lib/AmdMainTemplatePlugin.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var ConcatSource = require("webpack-sources").ConcatSource;
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/

function AmdMainTemplatePlugin(name) {
this.name = name;
"use strict";

const ConcatSource = require("webpack-sources").ConcatSource;

class AmdMainTemplatePlugin {
constructor(name) {
this.name = name;
}

apply(compilation) {
let mainTemplate = compilation.mainTemplate;

compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
let externals = chunk.modules.filter((m) => m.external);
let externalsDepsArray = JSON.stringify(externals.map((m) =>
typeof m.request === "object" ? m.request.amd : m.request
));
let externalsArguments = externals.map((m) =>
`__WEBPACK_EXTERNAL_MODULE_${m.id}__`
).join(", ");

if(this.name) {
let name = mainTemplate.applyPluginsWaterfall("asset-path", this.name, {
hash,
chunk
});

return new ConcatSource(
`define(${JSON.stringify(name)}, ${externalsDepsArray}, function(${externalsArguments}) { return `, source, "});"
);
} else if(externalsArguments) {
return new ConcatSource(`define(${externalsDepsArray}, function(${externalsArguments}) { return `, source, "});");
} else {
return new ConcatSource("define(function() { return ", source, "});");
}
});

mainTemplate.plugin("global-hash-paths", (paths) => {
if(this.name) paths.push(this.name);
return paths;
});

mainTemplate.plugin("hash", (hash) => {
hash.update("exports amd");
hash.update(`${this.name}`);
});
}
}

module.exports = AmdMainTemplatePlugin;
AmdMainTemplatePlugin.prototype.apply = function(compilation) {
var mainTemplate = compilation.mainTemplate;
compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
var externals = chunk.modules.filter(function(m) {
return m.external;
});
var externalsDepsArray = JSON.stringify(externals.map(function(m) {
return typeof m.request === "object" ? m.request.amd : m.request;
}));
var externalsArguments = externals.map(function(m) {
return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__";
}).join(", ");
if(this.name) {
var name = mainTemplate.applyPluginsWaterfall("asset-path", this.name, {
hash: hash,
chunk: chunk
});
return new ConcatSource("define(" + JSON.stringify(name) + ", " + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});");
} else if(externalsArguments) {
return new ConcatSource("define(" + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});");
} else {
return new ConcatSource("define(function() { return ", source, "});");
}
}.bind(this));
mainTemplate.plugin("global-hash-paths", function(paths) {
if(this.name) paths.push(this.name);
return paths;
}.bind(this));
mainTemplate.plugin("hash", function(hash) {
hash.update("exports amd");
hash.update(this.name + "");
}.bind(this));
};

0 comments on commit dc0f4e5

Please sign in to comment.