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