[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 InitFragment = require("../InitFragment");
|
---|
| 9 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 10 | const { first } = require("../util/SetHelpers");
|
---|
| 11 | const { propertyName } = require("../util/propertyName");
|
---|
| 12 |
|
---|
| 13 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 14 | /** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * @param {Iterable<string>} iterable iterable strings
|
---|
| 18 | * @returns {string} result
|
---|
| 19 | */
|
---|
| 20 | const joinIterableWithComma = iterable => {
|
---|
| 21 | // This is more performant than Array.from().join(", ")
|
---|
| 22 | // as it doesn't create an array
|
---|
| 23 | let str = "";
|
---|
| 24 | let first = true;
|
---|
| 25 | for (const item of iterable) {
|
---|
| 26 | if (first) {
|
---|
| 27 | first = false;
|
---|
| 28 | } else {
|
---|
| 29 | str += ", ";
|
---|
| 30 | }
|
---|
| 31 | str += item;
|
---|
| 32 | }
|
---|
| 33 | return str;
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | const EMPTY_MAP = new Map();
|
---|
| 37 | const EMPTY_SET = new Set();
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * @extends {InitFragment<GenerateContext>} Context
|
---|
| 41 | */
|
---|
| 42 | class HarmonyExportInitFragment extends InitFragment {
|
---|
| 43 | /**
|
---|
| 44 | * @param {string} exportsArgument the exports identifier
|
---|
| 45 | * @param {Map<string, string>} exportMap mapping from used name to exposed variable name
|
---|
| 46 | * @param {Set<string>} unusedExports list of unused export names
|
---|
| 47 | */
|
---|
| 48 | constructor(
|
---|
| 49 | exportsArgument,
|
---|
| 50 | exportMap = EMPTY_MAP,
|
---|
| 51 | unusedExports = EMPTY_SET
|
---|
| 52 | ) {
|
---|
| 53 | super(undefined, InitFragment.STAGE_HARMONY_EXPORTS, 1, "harmony-exports");
|
---|
| 54 | this.exportsArgument = exportsArgument;
|
---|
| 55 | this.exportMap = exportMap;
|
---|
| 56 | this.unusedExports = unusedExports;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * @param {HarmonyExportInitFragment[]} fragments all fragments to merge
|
---|
| 61 | * @returns {HarmonyExportInitFragment} merged fragment
|
---|
| 62 | */
|
---|
| 63 | mergeAll(fragments) {
|
---|
| 64 | let exportMap;
|
---|
| 65 | let exportMapOwned = false;
|
---|
| 66 | let unusedExports;
|
---|
| 67 | let unusedExportsOwned = false;
|
---|
| 68 |
|
---|
| 69 | for (const fragment of fragments) {
|
---|
| 70 | if (fragment.exportMap.size !== 0) {
|
---|
| 71 | if (exportMap === undefined) {
|
---|
| 72 | exportMap = fragment.exportMap;
|
---|
| 73 | exportMapOwned = false;
|
---|
| 74 | } else {
|
---|
| 75 | if (!exportMapOwned) {
|
---|
| 76 | exportMap = new Map(exportMap);
|
---|
| 77 | exportMapOwned = true;
|
---|
| 78 | }
|
---|
| 79 | for (const [key, value] of fragment.exportMap) {
|
---|
| 80 | if (!exportMap.has(key)) exportMap.set(key, value);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | if (fragment.unusedExports.size !== 0) {
|
---|
| 85 | if (unusedExports === undefined) {
|
---|
| 86 | unusedExports = fragment.unusedExports;
|
---|
| 87 | unusedExportsOwned = false;
|
---|
| 88 | } else {
|
---|
| 89 | if (!unusedExportsOwned) {
|
---|
| 90 | unusedExports = new Set(unusedExports);
|
---|
| 91 | unusedExportsOwned = true;
|
---|
| 92 | }
|
---|
| 93 | for (const value of fragment.unusedExports) {
|
---|
| 94 | unusedExports.add(value);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | return new HarmonyExportInitFragment(
|
---|
| 100 | this.exportsArgument,
|
---|
| 101 | exportMap,
|
---|
| 102 | unusedExports
|
---|
| 103 | );
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * @param {HarmonyExportInitFragment} other other
|
---|
| 108 | * @returns {HarmonyExportInitFragment} merged result
|
---|
| 109 | */
|
---|
| 110 | merge(other) {
|
---|
| 111 | let exportMap;
|
---|
| 112 | if (this.exportMap.size === 0) {
|
---|
| 113 | exportMap = other.exportMap;
|
---|
| 114 | } else if (other.exportMap.size === 0) {
|
---|
| 115 | exportMap = this.exportMap;
|
---|
| 116 | } else {
|
---|
| 117 | exportMap = new Map(other.exportMap);
|
---|
| 118 | for (const [key, value] of this.exportMap) {
|
---|
| 119 | if (!exportMap.has(key)) exportMap.set(key, value);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | let unusedExports;
|
---|
| 123 | if (this.unusedExports.size === 0) {
|
---|
| 124 | unusedExports = other.unusedExports;
|
---|
| 125 | } else if (other.unusedExports.size === 0) {
|
---|
| 126 | unusedExports = this.unusedExports;
|
---|
| 127 | } else {
|
---|
| 128 | unusedExports = new Set(other.unusedExports);
|
---|
| 129 | for (const value of this.unusedExports) {
|
---|
| 130 | unusedExports.add(value);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | return new HarmonyExportInitFragment(
|
---|
| 134 | this.exportsArgument,
|
---|
| 135 | exportMap,
|
---|
| 136 | unusedExports
|
---|
| 137 | );
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /**
|
---|
| 141 | * @param {GenerateContext} context context
|
---|
| 142 | * @returns {string | Source | undefined} the source code that will be included as initialization code
|
---|
| 143 | */
|
---|
| 144 | getContent({ runtimeTemplate, runtimeRequirements }) {
|
---|
| 145 | runtimeRequirements.add(RuntimeGlobals.exports);
|
---|
| 146 | runtimeRequirements.add(RuntimeGlobals.definePropertyGetters);
|
---|
| 147 |
|
---|
| 148 | const unusedPart =
|
---|
| 149 | this.unusedExports.size > 1
|
---|
| 150 | ? `/* unused harmony exports ${joinIterableWithComma(
|
---|
| 151 | this.unusedExports
|
---|
| 152 | )} */\n`
|
---|
| 153 | : this.unusedExports.size > 0
|
---|
| 154 | ? `/* unused harmony export ${first(this.unusedExports)} */\n`
|
---|
| 155 | : "";
|
---|
| 156 | const definitions = [];
|
---|
| 157 | const orderedExportMap = Array.from(this.exportMap).sort(([a], [b]) =>
|
---|
| 158 | a < b ? -1 : 1
|
---|
| 159 | );
|
---|
| 160 | for (const [key, value] of orderedExportMap) {
|
---|
| 161 | definitions.push(
|
---|
| 162 | `\n/* harmony export */ ${propertyName(
|
---|
| 163 | key
|
---|
| 164 | )}: ${runtimeTemplate.returningFunction(value)}`
|
---|
| 165 | );
|
---|
| 166 | }
|
---|
| 167 | const definePart =
|
---|
| 168 | this.exportMap.size > 0
|
---|
| 169 | ? `/* harmony export */ ${RuntimeGlobals.definePropertyGetters}(${
|
---|
| 170 | this.exportsArgument
|
---|
| 171 | }, {${definitions.join(",")}\n/* harmony export */ });\n`
|
---|
| 172 | : "";
|
---|
| 173 | return `${definePart}${unusedPart}`;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | module.exports = HarmonyExportInitFragment;
|
---|