Skip to content

Commit 25c135a

Browse files
committed
feat(tapable): upgrade other template plugins
1 parent dc7460f commit 25c135a

6 files changed

Lines changed: 21 additions & 16 deletions

‎lib/AmdMainTemplatePlugin.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AmdMainTemplatePlugin {
1616
apply(compilation) {
1717
const mainTemplate = compilation.mainTemplate;
1818

19-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
19+
compilation.tapMainAndChunkTemplates("AmdMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
2020
const externals = chunk.getModules().filter((m) => m.external);
2121
const externalsDepsArray = JSON.stringify(externals.map((m) =>
2222
typeof m.request === "object" ? m.request.amd : m.request
@@ -41,12 +41,12 @@ class AmdMainTemplatePlugin {
4141
}
4242
});
4343

44-
mainTemplate.plugin("global-hash-paths", (paths) => {
44+
mainTemplate.hooks.globalHashPaths.tap("AmdMainTemplatePlugin", paths => {
4545
if(this.name) paths.push(this.name);
4646
return paths;
4747
});
4848

49-
mainTemplate.plugin("hash", (hash) => {
49+
mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
5050
hash.update("exports amd");
5151
hash.update(this.name);
5252
});

‎lib/Compilation.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ class Compilation extends Tapable {
222222
this.chunkTemplate.plugin(name, fn);
223223
}
224224

225+
tapMainAndChunkTemplates(pluginName, hookName, fn) {
226+
this.mainTemplate.hooks[hookName].tap(pluginName, fn);
227+
this.chunkTemplate.hooks[hookName].tap(pluginName, fn);
228+
}
229+
225230
addModule(module, cacheGroup) {
226231
const identifier = module.identifier();
227232
if(this._modules.get(identifier)) {

‎lib/ExportPropertyMainTemplatePlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class ExportPropertyMainTemplatePlugin {
1717

1818
apply(compilation) {
1919
const mainTemplate = compilation.mainTemplate;
20-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
20+
compilation.tapMainAndChunkTemplates("ExportPropertyMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
2121
const postfix = `${accessorToObjectAccess([].concat(this.property))}`;
2222
return new ConcatSource(source, postfix);
2323
});
24-
mainTemplate.plugin("hash", hash => {
24+
mainTemplate.hooks.hash.tap("ExportPropertyMainTemplatePlugin", hash => {
2525
hash.update("export property");
2626
hash.update(`${this.property}`);
2727
});

‎lib/SetVarMainTemplatePlugin.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class SetVarMainTemplatePlugin {
1414

1515
apply(compilation) {
1616
const mainTemplate = compilation.mainTemplate;
17-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
17+
compilation.tapMainAndChunkTemplates("SetVarMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
1818
const varExpression = mainTemplate.getAssetPath(this.varExpression, {
1919
hash,
2020
chunk
@@ -26,11 +26,11 @@ class SetVarMainTemplatePlugin {
2626
return new ConcatSource(prefix, source);
2727
}
2828
});
29-
mainTemplate.plugin("global-hash-paths", (paths) => {
29+
mainTemplate.hooks.globalHashPaths.tap("SetVarMainTemplatePlugin", paths => {
3030
if(this.varExpression) paths.push(this.varExpression);
3131
return paths;
3232
});
33-
mainTemplate.plugin("hash", hash => {
33+
mainTemplate.hooks.hash.tap("SetVarMainTemplatePlugin", hash => {
3434
hash.update("set var");
3535
hash.update(`${this.varExpression}`);
3636
hash.update(`${this.copyObject}`);

‎lib/UmdMainTemplatePlugin.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class UmdMainTemplatePlugin {
4141

4242
apply(compilation) {
4343
const mainTemplate = compilation.mainTemplate;
44-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
44+
compilation.tapMainAndChunkTemplates("UmdMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
4545
let externals = chunk.getModules().filter(m => m.external && (m.externalType === "umd" || m.externalType === "umd2"));
4646
const optionalExternals = [];
4747
let requiredExternals = [];
@@ -173,13 +173,13 @@ class UmdMainTemplatePlugin {
173173
) +
174174
"})(typeof self !== 'undefined' ? self : this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
175175
});
176-
mainTemplate.plugin("global-hash-paths", (paths) => {
176+
mainTemplate.hooks.globalHasPaths.tap("UmdMainTemplatePlugin", (paths) => {
177177
if(this.names.root) paths = paths.concat(this.names.root);
178178
if(this.names.amd) paths = paths.concat(this.names.amd);
179179
if(this.names.commonjs) paths = paths.concat(this.names.commonjs);
180180
return paths;
181181
});
182-
mainTemplate.plugin("hash", (hash) => {
182+
mainTemplate.hooks.hash.tap("UmdMainTemplatePlugin", (hash) => {
183183
hash.update("umd");
184184
hash.update(`${this.names.root}`);
185185
hash.update(`${this.names.amd}`);

‎lib/web/JsonpExportMainTemplatePlugin.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ class JsonpExportMainTemplatePlugin {
1414
apply(compilation) {
1515
const mainTemplate = compilation.mainTemplate;
1616

17-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
17+
compilation.tapMainAndChunkTemplates("JsonpExportMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
1818
const name = mainTemplate.getAssetPath(this.name || "", {
19-
hash: hash,
20-
chunk: chunk
19+
hash,
20+
chunk
2121
});
2222
return new ConcatSource(`${name}(`, source, ");");
2323
});
2424

25-
mainTemplate.plugin("global-hash-paths", paths => {
25+
mainTemplate.hooks.globalHashPaths.tap("JsonpExportMainTemplatePlugin", paths => {
2626
if(this.name) paths.push(this.name);
2727
return paths;
2828
});
2929

30-
mainTemplate.plugin("hash", hash => {
30+
mainTemplate.hooks.hash.tap("JsonpExportMainTemplatePlugin", hash => {
3131
hash.update("jsonp export");
3232
hash.update(`${this.name}`);
3333
});

0 commit comments

Comments
 (0)