UNPKG

3.58 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 { UsageState } = require("../ExportsInfo");
10const propertyAccess = require("../util/propertyAccess");
11const { getEntryRuntime } = require("../util/runtime");
12const 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("../Compiler")} Compiler */
19/** @typedef {import("../Module")} Module */
20/** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
21/** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T> */
22
23/**
24 * @typedef {Object} ExportPropertyLibraryPluginParsed
25 * @property {string | string[]} export
26 */
27
28/**
29 * @typedef {Object} ExportPropertyLibraryPluginOptions
30 * @property {LibraryType} type
31 * @property {boolean} nsObjectUsed the namespace object is used
32 */
33/**
34 * @typedef {ExportPropertyLibraryPluginParsed} T
35 * @extends {AbstractLibraryPlugin<ExportPropertyLibraryPluginParsed>}
36 */
37class ExportPropertyLibraryPlugin extends AbstractLibraryPlugin {
38 /**
39 * @param {ExportPropertyLibraryPluginOptions} options options
40 */
41 constructor({ type, nsObjectUsed }) {
42 super({
43 pluginName: "ExportPropertyLibraryPlugin",
44 type
45 });
46 this.nsObjectUsed = nsObjectUsed;
47 }
48
49 /**
50 * @param {LibraryOptions} library normalized library option
51 * @returns {T | false} preprocess as needed by overriding
52 */
53 parseOptions(library) {
54 return {
55 export: library.export
56 };
57 }
58
59 /**
60 * @param {Module} module the exporting entry module
61 * @param {string} entryName the name of the entrypoint
62 * @param {LibraryContext<T>} libraryContext context
63 * @returns {void}
64 */
65 finishEntryModule(
66 module,
67 entryName,
68 { options, compilation, compilation: { moduleGraph } }
69 ) {
70 const runtime = getEntryRuntime(compilation, entryName);
71 if (options.export) {
72 const exportsInfo = moduleGraph.getExportInfo(
73 module,
74 Array.isArray(options.export) ? options.export[0] : options.export
75 );
76 exportsInfo.setUsed(UsageState.Used, runtime);
77 exportsInfo.canMangleUse = false;
78 } else {
79 const exportsInfo = moduleGraph.getExportsInfo(module);
80 if (this.nsObjectUsed) {
81 exportsInfo.setUsedInUnknownWay(runtime);
82 } else {
83 exportsInfo.setAllKnownExportsUsed(runtime);
84 }
85 }
86 moduleGraph.addExtraReason(module, "used as library export");
87 }
88
89 /**
90 * @param {Chunk} chunk the chunk
91 * @param {Set<string>} set runtime requirements
92 * @param {LibraryContext<T>} libraryContext context
93 * @returns {void}
94 */
95 runtimeRequirements(chunk, set, libraryContext) {}
96
97 /**
98 * @param {Source} source source
99 * @param {Module} module module
100 * @param {StartupRenderContext} renderContext render context
101 * @param {LibraryContext<T>} libraryContext context
102 * @returns {Source} source with library export
103 */
104 renderStartup(source, module, renderContext, { options }) {
105 if (!options.export) return source;
106 const postfix = `__webpack_exports__ = __webpack_exports__${propertyAccess(
107 Array.isArray(options.export) ? options.export : [options.export]
108 )};\n`;
109 return new ConcatSource(source, postfix);
110 }
111}
112
113module.exports = ExportPropertyLibraryPlugin;