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