1 | /*
|
---|
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 ExternalModule = require("../ExternalModule");
|
---|
10 | const Template = require("../Template");
|
---|
11 | const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
|
---|
12 |
|
---|
13 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
14 | /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
|
---|
15 | /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
|
---|
16 | /** @typedef {import("../Chunk")} Chunk */
|
---|
17 | /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
|
---|
18 | /** @typedef {import("../Compiler")} Compiler */
|
---|
19 | /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
|
---|
20 | /** @typedef {import("../util/Hash")} Hash */
|
---|
21 | /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @typedef {object} AmdLibraryPluginOptions
|
---|
25 | * @property {LibraryType} type
|
---|
26 | * @property {boolean=} requireAsWrapper
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @typedef {object} AmdLibraryPluginParsed
|
---|
31 | * @property {string} name
|
---|
32 | * @property {string} amdContainer
|
---|
33 | */
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @typedef {AmdLibraryPluginParsed} T
|
---|
37 | * @extends {AbstractLibraryPlugin<AmdLibraryPluginParsed>}
|
---|
38 | */
|
---|
39 | class AmdLibraryPlugin extends AbstractLibraryPlugin {
|
---|
40 | /**
|
---|
41 | * @param {AmdLibraryPluginOptions} options the plugin options
|
---|
42 | */
|
---|
43 | constructor(options) {
|
---|
44 | super({
|
---|
45 | pluginName: "AmdLibraryPlugin",
|
---|
46 | type: options.type
|
---|
47 | });
|
---|
48 | this.requireAsWrapper = options.requireAsWrapper;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * @param {LibraryOptions} library normalized library option
|
---|
53 | * @returns {T | false} preprocess as needed by overriding
|
---|
54 | */
|
---|
55 | parseOptions(library) {
|
---|
56 | const { name, amdContainer } = library;
|
---|
57 | if (this.requireAsWrapper) {
|
---|
58 | if (name) {
|
---|
59 | throw new Error(
|
---|
60 | `AMD library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
|
---|
61 | );
|
---|
62 | }
|
---|
63 | } else if (name && typeof name !== "string") {
|
---|
64 | throw new Error(
|
---|
65 | `AMD library name must be a simple string or unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
|
---|
66 | );
|
---|
67 | }
|
---|
68 | const _name = /** @type {string} */ (name);
|
---|
69 | const _amdContainer = /** @type {string} */ (amdContainer);
|
---|
70 | return { name: _name, amdContainer: _amdContainer };
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * @param {Source} source source
|
---|
75 | * @param {RenderContext} renderContext render context
|
---|
76 | * @param {LibraryContext<T>} libraryContext context
|
---|
77 | * @returns {Source} source with library export
|
---|
78 | */
|
---|
79 | render(
|
---|
80 | source,
|
---|
81 | { chunkGraph, chunk, runtimeTemplate },
|
---|
82 | { options, compilation }
|
---|
83 | ) {
|
---|
84 | const modern = runtimeTemplate.supportsArrowFunction();
|
---|
85 | const modules = chunkGraph
|
---|
86 | .getChunkModules(chunk)
|
---|
87 | .filter(
|
---|
88 | m =>
|
---|
89 | m instanceof ExternalModule &&
|
---|
90 | (m.externalType === "amd" || m.externalType === "amd-require")
|
---|
91 | );
|
---|
92 | const externals = /** @type {ExternalModule[]} */ (modules);
|
---|
93 | const externalsDepsArray = JSON.stringify(
|
---|
94 | externals.map(m =>
|
---|
95 | typeof m.request === "object" && !Array.isArray(m.request)
|
---|
96 | ? m.request.amd
|
---|
97 | : m.request
|
---|
98 | )
|
---|
99 | );
|
---|
100 | const externalsArguments = externals
|
---|
101 | .map(
|
---|
102 | m =>
|
---|
103 | `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
|
---|
104 | `${chunkGraph.getModuleId(m)}`
|
---|
105 | )}__`
|
---|
106 | )
|
---|
107 | .join(", ");
|
---|
108 |
|
---|
109 | const iife = runtimeTemplate.isIIFE();
|
---|
110 | const fnStart =
|
---|
111 | (modern
|
---|
112 | ? `(${externalsArguments}) => {`
|
---|
113 | : `function(${externalsArguments}) {`) +
|
---|
114 | (iife || !chunk.hasRuntime() ? " return " : "\n");
|
---|
115 | const fnEnd = iife ? ";\n}" : "\n}";
|
---|
116 |
|
---|
117 | let amdContainerPrefix = "";
|
---|
118 | if (options.amdContainer) {
|
---|
119 | amdContainerPrefix = `${options.amdContainer}.`;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (this.requireAsWrapper) {
|
---|
123 | return new ConcatSource(
|
---|
124 | `${amdContainerPrefix}require(${externalsDepsArray}, ${fnStart}`,
|
---|
125 | source,
|
---|
126 | `${fnEnd});`
|
---|
127 | );
|
---|
128 | } else if (options.name) {
|
---|
129 | const name = compilation.getPath(options.name, {
|
---|
130 | chunk
|
---|
131 | });
|
---|
132 |
|
---|
133 | return new ConcatSource(
|
---|
134 | `${amdContainerPrefix}define(${JSON.stringify(
|
---|
135 | name
|
---|
136 | )}, ${externalsDepsArray}, ${fnStart}`,
|
---|
137 | source,
|
---|
138 | `${fnEnd});`
|
---|
139 | );
|
---|
140 | } else if (externalsArguments) {
|
---|
141 | return new ConcatSource(
|
---|
142 | `${amdContainerPrefix}define(${externalsDepsArray}, ${fnStart}`,
|
---|
143 | source,
|
---|
144 | `${fnEnd});`
|
---|
145 | );
|
---|
146 | }
|
---|
147 | return new ConcatSource(
|
---|
148 | `${amdContainerPrefix}define(${fnStart}`,
|
---|
149 | source,
|
---|
150 | `${fnEnd});`
|
---|
151 | );
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * @param {Chunk} chunk the chunk
|
---|
156 | * @param {Hash} hash hash
|
---|
157 | * @param {ChunkHashContext} chunkHashContext chunk hash context
|
---|
158 | * @param {LibraryContext<T>} libraryContext context
|
---|
159 | * @returns {void}
|
---|
160 | */
|
---|
161 | chunkHash(chunk, hash, chunkHashContext, { options, compilation }) {
|
---|
162 | hash.update("AmdLibraryPlugin");
|
---|
163 | if (this.requireAsWrapper) {
|
---|
164 | hash.update("requireAsWrapper");
|
---|
165 | } else if (options.name) {
|
---|
166 | hash.update("named");
|
---|
167 | const name = compilation.getPath(options.name, {
|
---|
168 | chunk
|
---|
169 | });
|
---|
170 | hash.update(name);
|
---|
171 | } else if (options.amdContainer) {
|
---|
172 | hash.update("amdContainer");
|
---|
173 | hash.update(options.amdContainer);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | module.exports = AmdLibraryPlugin;
|
---|