UNPKG

3.08 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { ConcatSource } = require("webpack-sources");
9const Template = require("../Template");
10const propertyAccess = require("../util/propertyAccess");
11const 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("../Module")} Module */
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} ModuleLibraryPluginOptions
26 * @property {LibraryType} type
27 */
28
29/**
30 * @typedef {Object} ModuleLibraryPluginParsed
31 * @property {string} name
32 */
33
34/**
35 * @typedef {ModuleLibraryPluginParsed} T
36 * @extends {AbstractLibraryPlugin<ModuleLibraryPluginParsed>}
37 */
38class ModuleLibraryPlugin extends AbstractLibraryPlugin {
39 /**
40 * @param {ModuleLibraryPluginOptions} options the plugin options
41 */
42 constructor(options) {
43 super({
44 pluginName: "ModuleLibraryPlugin",
45 type: options.type
46 });
47 }
48
49 /**
50 * @param {LibraryOptions} library normalized library option
51 * @returns {T | false} preprocess as needed by overriding
52 */
53 parseOptions(library) {
54 const { name } = library;
55 if (name) {
56 throw new Error(
57 `Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
58 );
59 }
60 return {
61 name: /** @type {string} */ (name)
62 };
63 }
64
65 /**
66 * @param {Source} source source
67 * @param {Module} module module
68 * @param {StartupRenderContext} renderContext render context
69 * @param {LibraryContext<T>} libraryContext context
70 * @returns {Source} source with library export
71 */
72 renderStartup(
73 source,
74 module,
75 { moduleGraph, chunk },
76 { options, compilation }
77 ) {
78 const result = new ConcatSource(source);
79 const exportsInfo = moduleGraph.getExportsInfo(module);
80 const exports = [];
81 const isAsync = moduleGraph.isAsync(module);
82 if (isAsync) {
83 result.add(`__webpack_exports__ = await __webpack_exports__;\n`);
84 }
85 for (const exportInfo of exportsInfo.orderedExports) {
86 if (!exportInfo.provided) continue;
87 const varName = `__webpack_exports__${Template.toIdentifier(
88 exportInfo.name
89 )}`;
90 result.add(
91 `var ${varName} = __webpack_exports__${propertyAccess([
92 exportInfo.getUsedName(exportInfo.name, chunk.runtime)
93 ])};\n`
94 );
95 exports.push(`${varName} as ${exportInfo.name}`);
96 }
97 if (exports.length > 0) {
98 result.add(`export { ${exports.join(", ")} };\n`);
99 }
100 return result;
101 }
102}
103
104module.exports = ModuleLibraryPlugin;