[6a3a178] | 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 Template = require("../Template");
|
---|
| 10 | const propertyAccess = require("../util/propertyAccess");
|
---|
| 11 | const 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 | */
|
---|
| 38 | class 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 | for (const exportInfo of exportsInfo.orderedExports) {
|
---|
| 82 | if (!exportInfo.provided) continue;
|
---|
| 83 | const varName = `__webpack_exports__${Template.toIdentifier(
|
---|
| 84 | exportInfo.name
|
---|
| 85 | )}`;
|
---|
| 86 | result.add(
|
---|
| 87 | `var ${varName} = __webpack_exports__${propertyAccess([
|
---|
| 88 | exportInfo.getUsedName(exportInfo.name, chunk.runtime)
|
---|
| 89 | ])};\n`
|
---|
| 90 | );
|
---|
| 91 | exports.push(`${varName} as ${exportInfo.name}`);
|
---|
| 92 | }
|
---|
| 93 | if (exports.length > 0) {
|
---|
| 94 | result.add(`export { ${exports.join(", ")} };\n`);
|
---|
| 95 | }
|
---|
| 96 | return result;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | module.exports = ModuleLibraryPlugin;
|
---|