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 ConcatenatedModule = require("../optimize/ConcatenatedModule");
|
---|
10 | const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
|
---|
11 |
|
---|
12 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
13 | /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
|
---|
14 | /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
|
---|
15 | /** @typedef {import("../Chunk")} Chunk */
|
---|
16 | /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
|
---|
17 | /** @typedef {import("../Compiler")} Compiler */
|
---|
18 | /** @typedef {import("../Module")} Module */
|
---|
19 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
---|
20 | /** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
|
---|
21 | /** @typedef {import("../util/Hash")} Hash */
|
---|
22 | /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * @typedef {object} ModernModuleLibraryPluginOptions
|
---|
26 | * @property {LibraryType} type
|
---|
27 | */
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * @typedef {object} ModernModuleLibraryPluginParsed
|
---|
31 | * @property {string} name
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @typedef {ModernModuleLibraryPluginParsed} T
|
---|
36 | * @extends {AbstractLibraryPlugin<ModernModuleLibraryPluginParsed>}
|
---|
37 | */
|
---|
38 | class ModernModuleLibraryPlugin extends AbstractLibraryPlugin {
|
---|
39 | /**
|
---|
40 | * Apply the plugin
|
---|
41 | * @param {Compiler} compiler the compiler instance
|
---|
42 | * @returns {void}
|
---|
43 | */
|
---|
44 | apply(compiler) {
|
---|
45 | super.apply(compiler);
|
---|
46 |
|
---|
47 | compiler.hooks.compilation.tap("ModernModuleLibraryPlugin", compilation => {
|
---|
48 | const { exportsDefinitions } =
|
---|
49 | ConcatenatedModule.getCompilationHooks(compilation);
|
---|
50 | exportsDefinitions.tap("ModernModuleLibraryPlugin", () => true);
|
---|
51 | });
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * @param {ModernModuleLibraryPluginOptions} options the plugin options
|
---|
56 | */
|
---|
57 | constructor(options) {
|
---|
58 | super({
|
---|
59 | pluginName: "ModernModuleLibraryPlugin",
|
---|
60 | type: options.type
|
---|
61 | });
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * @param {LibraryOptions} library normalized library option
|
---|
66 | * @returns {T | false} preprocess as needed by overriding
|
---|
67 | */
|
---|
68 | parseOptions(library) {
|
---|
69 | const { name } = library;
|
---|
70 | if (name) {
|
---|
71 | throw new Error(
|
---|
72 | `Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
|
---|
73 | );
|
---|
74 | }
|
---|
75 | const _name = /** @type {string} */ (name);
|
---|
76 | return {
|
---|
77 | name: _name
|
---|
78 | };
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * @param {Source} source source
|
---|
83 | * @param {Module} module module
|
---|
84 | * @param {StartupRenderContext} renderContext render context
|
---|
85 | * @param {LibraryContext<T>} libraryContext context
|
---|
86 | * @returns {Source} source with library export
|
---|
87 | */
|
---|
88 | renderStartup(
|
---|
89 | source,
|
---|
90 | module,
|
---|
91 | { moduleGraph, chunk },
|
---|
92 | { options, compilation }
|
---|
93 | ) {
|
---|
94 | const result = new ConcatSource(source);
|
---|
95 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
---|
96 | const definitions =
|
---|
97 | /** @type {BuildMeta} */
|
---|
98 | (module.buildMeta).exportsFinalName;
|
---|
99 | const exports = [];
|
---|
100 |
|
---|
101 | for (const exportInfo of exportsInfo.orderedExports) {
|
---|
102 | let shouldContinue = false;
|
---|
103 | const reexport = exportInfo.findTarget(moduleGraph, _m => true);
|
---|
104 |
|
---|
105 | if (reexport) {
|
---|
106 | const exp = moduleGraph.getExportsInfo(reexport.module);
|
---|
107 |
|
---|
108 | for (const reexportInfo of exp.orderedExports) {
|
---|
109 | if (
|
---|
110 | !reexportInfo.provided &&
|
---|
111 | reexportInfo.name === /** @type {string[]} */ (reexport.export)[0]
|
---|
112 | ) {
|
---|
113 | shouldContinue = true;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (shouldContinue) continue;
|
---|
119 |
|
---|
120 | const webpackExportsProperty = exportInfo.getUsedName(
|
---|
121 | exportInfo.name,
|
---|
122 | chunk.runtime
|
---|
123 | );
|
---|
124 | const finalName =
|
---|
125 | definitions[
|
---|
126 | /** @type {string} */
|
---|
127 | (webpackExportsProperty)
|
---|
128 | ];
|
---|
129 | exports.push(
|
---|
130 | finalName === exportInfo.name
|
---|
131 | ? finalName
|
---|
132 | : `${finalName} as ${exportInfo.name}`
|
---|
133 | );
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (exports.length > 0) {
|
---|
137 | result.add(`export { ${exports.join(", ")} };\n`);
|
---|
138 | }
|
---|
139 |
|
---|
140 | return result;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | module.exports = ModernModuleLibraryPlugin;
|
---|