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 RuntimeGlobals = require("../RuntimeGlobals");
|
---|
10 | const Template = require("../Template");
|
---|
11 | const propertyAccess = require("../util/propertyAccess");
|
---|
12 | const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
|
---|
13 |
|
---|
14 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
15 | /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
|
---|
16 | /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
|
---|
17 | /** @typedef {import("../Chunk")} Chunk */
|
---|
18 | /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
|
---|
19 | /** @typedef {import("../Compiler")} Compiler */
|
---|
20 | /** @typedef {import("../Module")} Module */
|
---|
21 | /** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
|
---|
22 | /** @typedef {import("../util/Hash")} Hash */
|
---|
23 | /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @typedef {object} ModuleLibraryPluginOptions
|
---|
27 | * @property {LibraryType} type
|
---|
28 | */
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @typedef {object} ModuleLibraryPluginParsed
|
---|
32 | * @property {string} name
|
---|
33 | */
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @typedef {ModuleLibraryPluginParsed} T
|
---|
37 | * @extends {AbstractLibraryPlugin<ModuleLibraryPluginParsed>}
|
---|
38 | */
|
---|
39 | class ModuleLibraryPlugin extends AbstractLibraryPlugin {
|
---|
40 | /**
|
---|
41 | * @param {ModuleLibraryPluginOptions} options the plugin options
|
---|
42 | */
|
---|
43 | constructor(options) {
|
---|
44 | super({
|
---|
45 | pluginName: "ModuleLibraryPlugin",
|
---|
46 | type: options.type
|
---|
47 | });
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @param {LibraryOptions} library normalized library option
|
---|
52 | * @returns {T | false} preprocess as needed by overriding
|
---|
53 | */
|
---|
54 | parseOptions(library) {
|
---|
55 | const { name } = library;
|
---|
56 | if (name) {
|
---|
57 | throw new Error(
|
---|
58 | `Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
|
---|
59 | );
|
---|
60 | }
|
---|
61 | const _name = /** @type {string} */ (name);
|
---|
62 | return {
|
---|
63 | name: _name
|
---|
64 | };
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * @param {Source} source source
|
---|
69 | * @param {Module} module module
|
---|
70 | * @param {StartupRenderContext} renderContext render context
|
---|
71 | * @param {LibraryContext<T>} libraryContext context
|
---|
72 | * @returns {Source} source with library export
|
---|
73 | */
|
---|
74 | renderStartup(
|
---|
75 | source,
|
---|
76 | module,
|
---|
77 | { moduleGraph, chunk },
|
---|
78 | { options, compilation }
|
---|
79 | ) {
|
---|
80 | const result = new ConcatSource(source);
|
---|
81 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
---|
82 | const exports = [];
|
---|
83 | const isAsync = moduleGraph.isAsync(module);
|
---|
84 | if (isAsync) {
|
---|
85 | result.add(
|
---|
86 | `${RuntimeGlobals.exports} = await ${RuntimeGlobals.exports};\n`
|
---|
87 | );
|
---|
88 | }
|
---|
89 | for (const exportInfo of exportsInfo.orderedExports) {
|
---|
90 | if (!exportInfo.provided) continue;
|
---|
91 | const varName = `${RuntimeGlobals.exports}${Template.toIdentifier(
|
---|
92 | exportInfo.name
|
---|
93 | )}`;
|
---|
94 | result.add(
|
---|
95 | `var ${varName} = ${RuntimeGlobals.exports}${propertyAccess([
|
---|
96 | /** @type {string} */
|
---|
97 | (exportInfo.getUsedName(exportInfo.name, chunk.runtime))
|
---|
98 | ])};\n`
|
---|
99 | );
|
---|
100 | exports.push(`${varName} as ${exportInfo.name}`);
|
---|
101 | }
|
---|
102 | if (exports.length > 0) {
|
---|
103 | result.add(`export { ${exports.join(", ")} };\n`);
|
---|
104 | }
|
---|
105 | return result;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | module.exports = ModuleLibraryPlugin;
|
---|