Skip to content

Commit 5eee030

Browse files
committed
fix(tapable): refactored calls to iterator and add some destructuring
1 parent 25c135a commit 5eee030

File tree

6 files changed

+39
-25
lines changed

6 files changed

+39
-25
lines changed

lib/AmdMainTemplatePlugin.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ class AmdMainTemplatePlugin {
1414
}
1515

1616
apply(compilation) {
17-
const mainTemplate = compilation.mainTemplate;
17+
const { mainTemplate, chunkTemplate } = compilation;
1818

19-
compilation.tapMainAndChunkTemplates("AmdMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
19+
for(const template of[mainTemplate, chunkTemplate]) {
20+
template.hooks.renderWithEntry("AmdMainTemplatePlugin", onRenderWithEntry);
21+
}
22+
23+
const onRenderWithEntry = (source, chunk, hash) => {
2024
const externals = chunk.getModules().filter((m) => m.external);
2125
const externalsDepsArray = JSON.stringify(externals.map((m) =>
2226
typeof m.request === "object" ? m.request.amd : m.request
@@ -39,7 +43,7 @@ class AmdMainTemplatePlugin {
3943
} else {
4044
return new ConcatSource("define(function() { return ", source, "});");
4145
}
42-
});
46+
};
4347

4448
mainTemplate.hooks.globalHashPaths.tap("AmdMainTemplatePlugin", paths => {
4549
if(this.name) paths.push(this.name);

lib/Compilation.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,6 @@ class Compilation extends Tapable {
217217
return new Stats(this);
218218
}
219219

220-
templatesPlugin(name, fn) {
221-
this.mainTemplate.plugin(name, fn);
222-
this.chunkTemplate.plugin(name, fn);
223-
}
224-
225-
tapMainAndChunkTemplates(pluginName, hookName, fn) {
226-
this.mainTemplate.hooks[hookName].tap(pluginName, fn);
227-
this.chunkTemplate.hooks[hookName].tap(pluginName, fn);
228-
}
229-
230220
addModule(module, cacheGroup) {
231221
const identifier = module.identifier();
232222
if(this._modules.get(identifier)) {

lib/ExportPropertyMainTemplatePlugin.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ class ExportPropertyMainTemplatePlugin {
1616
}
1717

1818
apply(compilation) {
19-
const mainTemplate = compilation.mainTemplate;
20-
compilation.tapMainAndChunkTemplates("ExportPropertyMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
19+
const { mainTemplate, chunkTemplate } = compilation;
20+
21+
for(const template of[mainTemplate, chunkTemplate]) {
22+
template.hooks.renderWithEntry("AmdMainTemplatePlugin", onRenderWithEntry);
23+
}
24+
25+
const onRenderWithEntry = (source, chunk, hash) => {
2126
const postfix = `${accessorToObjectAccess([].concat(this.property))}`;
2227
return new ConcatSource(source, postfix);
23-
});
28+
};
29+
2430
mainTemplate.hooks.hash.tap("ExportPropertyMainTemplatePlugin", hash => {
2531
hash.update("export property");
2632
hash.update(`${this.property}`);

lib/SetVarMainTemplatePlugin.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ class SetVarMainTemplatePlugin {
1313
}
1414

1515
apply(compilation) {
16-
const mainTemplate = compilation.mainTemplate;
17-
compilation.tapMainAndChunkTemplates("SetVarMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
16+
const { mainTemplate, chunkTemplate } = compilation;
17+
18+
for(const template of[mainTemplate, chunkTemplate]) {
19+
template.hooks.renderWithEntry("AmdMainTemplatePlugin", onRenderWithEntry);
20+
}
21+
22+
const onRenderWithEntry = (source, chunk, hash) => {
1823
const varExpression = mainTemplate.getAssetPath(this.varExpression, {
1924
hash,
2025
chunk
@@ -25,7 +30,7 @@ class SetVarMainTemplatePlugin {
2530
const prefix = `${varExpression} =\n`;
2631
return new ConcatSource(prefix, source);
2732
}
28-
});
33+
};
2934
mainTemplate.hooks.globalHashPaths.tap("SetVarMainTemplatePlugin", paths => {
3035
if(this.varExpression) paths.push(this.varExpression);
3136
return paths;

lib/UmdMainTemplatePlugin.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ class UmdMainTemplatePlugin {
4040
}
4141

4242
apply(compilation) {
43-
const mainTemplate = compilation.mainTemplate;
44-
compilation.tapMainAndChunkTemplates("UmdMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
43+
const { mainTemplate, chunkTemplate } = compilation;
44+
45+
for(const template of[mainTemplate, chunkTemplate]) {
46+
template.hooks.renderWithEntry("AmdMainTemplatePlugin", onRenderWithEntry);
47+
}
48+
49+
const onRenderWithEntry = (source, chunk, hash) => {
4550
let externals = chunk.getModules().filter(m => m.external && (m.externalType === "umd" || m.externalType === "umd2"));
4651
const optionalExternals = [];
4752
let requiredExternals = [];
@@ -172,7 +177,7 @@ class UmdMainTemplatePlugin {
172177
" }\n"
173178
) +
174179
"})(typeof self !== 'undefined' ? self : this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
175-
});
180+
};
176181
mainTemplate.hooks.globalHasPaths.tap("UmdMainTemplatePlugin", (paths) => {
177182
if(this.names.root) paths = paths.concat(this.names.root);
178183
if(this.names.amd) paths = paths.concat(this.names.amd);

lib/web/JsonpExportMainTemplatePlugin.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ class JsonpExportMainTemplatePlugin {
1212
}
1313

1414
apply(compilation) {
15-
const mainTemplate = compilation.mainTemplate;
15+
const { mainTemplate, chunkTemplate } = compilation;
1616

17-
compilation.tapMainAndChunkTemplates("JsonpExportMainTemplatePlugin", "renderWithEntry", (source, chunk, hash) => {
17+
for(const template of[mainTemplate, chunkTemplate]) {
18+
template.hooks.renderWithEntry("AmdMainTemplatePlugin", onRenderWithEntry);
19+
}
20+
21+
const onRenderWithEntry = (source, chunk, hash) => {
1822
const name = mainTemplate.getAssetPath(this.name || "", {
1923
hash,
2024
chunk
2125
});
2226
return new ConcatSource(`${name}(`, source, ");");
23-
});
27+
};
2428

2529
mainTemplate.hooks.globalHashPaths.tap("JsonpExportMainTemplatePlugin", paths => {
2630
if(this.name) paths.push(this.name);

0 commit comments

Comments
 (0)