1 |
3a515b92
|
cagy
|
/*
|
2 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3 |
|
|
Author Tobias Koppers @sokra
|
4 |
|
|
*/
|
5 |
|
|
|
6 |
|
|
"use strict";
|
7 |
|
|
|
8 |
|
|
const { ConcatSource } = require("webpack-sources");
|
9 |
|
|
const Template = require("./Template");
|
10 |
|
|
|
11 |
|
|
/** @typedef {import("./Compilation")} Compilation */
|
12 |
|
|
|
13 |
|
|
/**
|
14 |
|
|
* @typedef {Object} AmdMainTemplatePluginOptions
|
15 |
|
|
* @param {string=} name the library name
|
16 |
|
|
* @property {boolean=} requireAsWrapper
|
17 |
|
|
*/
|
18 |
|
|
|
19 |
|
|
class AmdMainTemplatePlugin {
|
20 |
|
|
/**
|
21 |
|
|
* @param {AmdMainTemplatePluginOptions} options the plugin options
|
22 |
|
|
*/
|
23 |
|
|
constructor(options) {
|
24 |
|
|
if (!options || typeof options === "string") {
|
25 |
|
|
this.name = options;
|
26 |
|
|
this.requireAsWrapper = false;
|
27 |
|
|
} else {
|
28 |
|
|
this.name = options.name;
|
29 |
|
|
this.requireAsWrapper = options.requireAsWrapper;
|
30 |
|
|
}
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
/**
|
34 |
|
|
* @param {Compilation} compilation the compilation instance
|
35 |
|
|
* @returns {void}
|
36 |
|
|
*/
|
37 |
|
|
apply(compilation) {
|
38 |
|
|
const { mainTemplate, chunkTemplate } = compilation;
|
39 |
|
|
|
40 |
|
|
const onRenderWithEntry = (source, chunk, hash) => {
|
41 |
|
|
const externals = chunk.getModules().filter(m => m.external);
|
42 |
|
|
const externalsDepsArray = JSON.stringify(
|
43 |
|
|
externals.map(m =>
|
44 |
|
|
typeof m.request === "object" ? m.request.amd : m.request
|
45 |
|
|
)
|
46 |
|
|
);
|
47 |
|
|
const externalsArguments = externals
|
48 |
|
|
.map(
|
49 |
|
|
m => `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__`
|
50 |
|
|
)
|
51 |
|
|
.join(", ");
|
52 |
|
|
|
53 |
|
|
if (this.requireAsWrapper) {
|
54 |
|
|
return new ConcatSource(
|
55 |
|
|
`require(${externalsDepsArray}, function(${externalsArguments}) { return `,
|
56 |
|
|
source,
|
57 |
|
|
"});"
|
58 |
|
|
);
|
59 |
|
|
} else if (this.name) {
|
60 |
|
|
const name = mainTemplate.getAssetPath(this.name, {
|
61 |
|
|
hash,
|
62 |
|
|
chunk
|
63 |
|
|
});
|
64 |
|
|
|
65 |
|
|
return new ConcatSource(
|
66 |
|
|
`define(${JSON.stringify(
|
67 |
|
|
name
|
68 |
|
|
)}, ${externalsDepsArray}, function(${externalsArguments}) { return `,
|
69 |
|
|
source,
|
70 |
|
|
"});"
|
71 |
|
|
);
|
72 |
|
|
} else if (externalsArguments) {
|
73 |
|
|
return new ConcatSource(
|
74 |
|
|
`define(${externalsDepsArray}, function(${externalsArguments}) { return `,
|
75 |
|
|
source,
|
76 |
|
|
"});"
|
77 |
|
|
);
|
78 |
|
|
} else {
|
79 |
|
|
return new ConcatSource("define(function() { return ", source, "});");
|
80 |
|
|
}
|
81 |
|
|
};
|
82 |
|
|
|
83 |
|
|
for (const template of [mainTemplate, chunkTemplate]) {
|
84 |
|
|
template.hooks.renderWithEntry.tap(
|
85 |
|
|
"AmdMainTemplatePlugin",
|
86 |
|
|
onRenderWithEntry
|
87 |
|
|
);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
mainTemplate.hooks.globalHashPaths.tap("AmdMainTemplatePlugin", paths => {
|
91 |
|
|
if (this.name) {
|
92 |
|
|
paths.push(this.name);
|
93 |
|
|
}
|
94 |
|
|
return paths;
|
95 |
|
|
});
|
96 |
|
|
|
97 |
|
|
mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
|
98 |
|
|
hash.update("exports amd");
|
99 |
|
|
if (this.name) {
|
100 |
|
|
hash.update(this.name);
|
101 |
|
|
}
|
102 |
|
|
});
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
module.exports = AmdMainTemplatePlugin;
|