Skip to content

Commit 30ac700

Browse files
authored
Merge pull request webpack#6101 from webpack/feature/tapable-upgrade-json-export-main-template-plugin
feat(tapable): upgrade other template plugins
2 parents 6300c5b + a297e87 commit 30ac700

8 files changed

+69
-101
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ module.exports = {
4747
"case": { "after": true },
4848
"return": { "after": true },
4949
"finally": { "after": true },
50-
"do": { "after": true }
50+
"do": { "after": true },
51+
"of": { "after": true }
5152
}
5253
}],
5354
"no-console": "off",

lib/AmdMainTemplatePlugin.js

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

1616
apply(compilation) {
17-
const mainTemplate = compilation.mainTemplate;
17+
const {
18+
mainTemplate,
19+
chunkTemplate
20+
} = compilation;
1821

19-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
22+
const onRenderWithEntry = (source, chunk, hash) => {
2023
const externals = chunk.getModules().filter((m) => m.external);
2124
const externalsDepsArray = JSON.stringify(externals.map((m) =>
2225
typeof m.request === "object" ? m.request.amd : m.request
@@ -39,14 +42,18 @@ class AmdMainTemplatePlugin {
3942
} else {
4043
return new ConcatSource("define(function() { return ", source, "});");
4144
}
42-
});
45+
};
46+
47+
for(const template of [mainTemplate, chunkTemplate]) {
48+
template.hooks.renderWithEntry.tap("AmdMainTemplatePlugin", onRenderWithEntry);
49+
}
4350

44-
mainTemplate.plugin("global-hash-paths", (paths) => {
51+
mainTemplate.hooks.globalHashPaths.tap("AmdMainTemplatePlugin", paths => {
4552
if(this.name) paths.push(this.name);
4653
return paths;
4754
});
4855

49-
mainTemplate.plugin("hash", (hash) => {
56+
mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
5057
hash.update("exports amd");
5158
hash.update(this.name);
5259
});

lib/Compilation.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,6 @@ class Compilation extends Tapable {
220220
return new Stats(this);
221221
}
222222

223-
templatesPlugin(name, fn) {
224-
this.mainTemplate.plugin(name, fn);
225-
this.chunkTemplate.plugin(name, fn);
226-
}
227-
228223
addModule(module, cacheGroup) {
229224
const identifier = module.identifier();
230225
const alreadyAddedModule = this._modules.get(identifier);

lib/ExportPropertyMainTemplatePlugin.js

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

1818
apply(compilation) {
19-
const mainTemplate = compilation.mainTemplate;
20-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
19+
const {
20+
mainTemplate,
21+
chunkTemplate
22+
} = compilation;
23+
24+
const onRenderWithEntry = (source, chunk, hash) => {
2125
const postfix = `${accessorToObjectAccess([].concat(this.property))}`;
2226
return new ConcatSource(source, postfix);
23-
});
27+
};
28+
29+
for(const template of [mainTemplate, chunkTemplate]) {
30+
template.hooks.renderWithEntry.tap("ExportPropertyMainTemplatePlugin", onRenderWithEntry);
31+
}
32+
2433
mainTemplate.hooks.hash.tap("ExportPropertyMainTemplatePlugin", hash => {
2534
hash.update("export property");
2635
hash.update(`${this.property}`);

lib/SetVarMainTemplatePlugin.js

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

1515
apply(compilation) {
16-
const mainTemplate = compilation.mainTemplate;
17-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
16+
const {
17+
mainTemplate,
18+
chunkTemplate
19+
} = compilation;
20+
21+
const onRenderWithEntry = (source, chunk, hash) => {
1822
const varExpression = mainTemplate.getAssetPath(this.varExpression, {
1923
hash,
2024
chunk
@@ -25,12 +29,17 @@ class SetVarMainTemplatePlugin {
2529
const prefix = `${varExpression} =\n`;
2630
return new ConcatSource(prefix, source);
2731
}
28-
});
29-
mainTemplate.plugin("global-hash-paths", (paths) => {
32+
};
33+
34+
for(const template of [mainTemplate, chunkTemplate]) {
35+
template.hooks.renderWithEntry.tap("SetVarMainTemplatePlugin", onRenderWithEntry);
36+
}
37+
38+
mainTemplate.hooks.globalHashPaths.tap("SetVarMainTemplatePlugin", paths => {
3039
if(this.varExpression) paths.push(this.varExpression);
3140
return paths;
3241
});
33-
mainTemplate.plugin("hash", hash => {
42+
mainTemplate.hooks.hash.tap("SetVarMainTemplatePlugin", hash => {
3443
hash.update("set var");
3544
hash.update(`${this.varExpression}`);
3645
hash.update(`${this.copyObject}`);

lib/UmdMainTemplatePlugin.js

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

4242
apply(compilation) {
43-
const mainTemplate = compilation.mainTemplate;
44-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
43+
const {
44+
mainTemplate,
45+
chunkTemplate
46+
} = compilation;
47+
48+
const onRenderWithEntry = (source, chunk, hash) => {
4549
let externals = chunk.getModules().filter(m => m.external && (m.externalType === "umd" || m.externalType === "umd2"));
4650
const optionalExternals = [];
4751
let requiredExternals = [];
@@ -172,14 +176,20 @@ class UmdMainTemplatePlugin {
172176
" }\n"
173177
) +
174178
"})(typeof self !== 'undefined' ? self : this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
175-
});
176-
mainTemplate.plugin("global-hash-paths", (paths) => {
179+
};
180+
181+
for(const template of [mainTemplate, chunkTemplate]) {
182+
template.hooks.renderWithEntry.tap("UmdMainTemplatePlugin", onRenderWithEntry);
183+
}
184+
185+
mainTemplate.hooks.globalHashPaths.tap("UmdMainTemplatePlugin", (paths) => {
177186
if(this.names.root) paths = paths.concat(this.names.root);
178187
if(this.names.amd) paths = paths.concat(this.names.amd);
179188
if(this.names.commonjs) paths = paths.concat(this.names.commonjs);
180189
return paths;
181190
});
182-
mainTemplate.plugin("hash", (hash) => {
191+
192+
mainTemplate.hooks.hash.tap("UmdMainTemplatePlugin", (hash) => {
183193
hash.update("umd");
184194
hash.update(`${this.names.root}`);
185195
hash.update(`${this.names.amd}`);

lib/web/JsonpExportMainTemplatePlugin.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,29 @@ class JsonpExportMainTemplatePlugin {
1212
}
1313

1414
apply(compilation) {
15-
const mainTemplate = compilation.mainTemplate;
15+
const {
16+
mainTemplate,
17+
chunkTemplate
18+
} = compilation;
1619

17-
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
20+
const onRenderWithEntry = (source, chunk, hash) => {
1821
const name = mainTemplate.getAssetPath(this.name || "", {
19-
hash: hash,
20-
chunk: chunk
22+
hash,
23+
chunk
2124
});
2225
return new ConcatSource(`${name}(`, source, ");");
23-
});
26+
};
27+
28+
for(const template of [mainTemplate, chunkTemplate]) {
29+
template.hooks.renderWithEntry.tap("JsonpExportMainTemplatePlugin", onRenderWithEntry);
30+
}
2431

25-
mainTemplate.plugin("global-hash-paths", paths => {
32+
mainTemplate.hooks.globalHashPaths.tap("JsonpExportMainTemplatePlugin", paths => {
2633
if(this.name) paths.push(this.name);
2734
return paths;
2835
});
2936

30-
mainTemplate.plugin("hash", hash => {
37+
mainTemplate.hooks.hash.tap("JsonpExportMainTemplatePlugin", hash => {
3138
hash.update("jsonp export");
3239
hash.update(`${this.name}`);
3340
});

test/UmdMainTemplatePlugin.unittest.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)