* Materializes a namespace object that keeps the original export names while * the module's own exports are mangled. Returns undefined when no export was * mangled (then the raw namespace object can be used as-is). * @template GenerateContext * @param {object} options options * @param {Mod
({
moduleGraph,
module,
importVar,
initFragments,
runtime,
runtimeRequirements
})
| 1384 | * @returns {string | undefined} expression of the materialized namespace object, or undefined |
| 1385 | */ |
| 1386 | _materializedNamespaceObject({ |
| 1387 | moduleGraph, |
| 1388 | module, |
| 1389 | importVar, |
| 1390 | initFragments, |
| 1391 | runtime, |
| 1392 | runtimeRequirements |
| 1393 | }) { |
| 1394 | const exportsInfo = moduleGraph.getExportsInfo(module); |
| 1395 | /** @type {string[]} */ |
| 1396 | const definitions = []; |
| 1397 | let mangled = false; |
| 1398 | for (const exportInfo of exportsInfo.orderedExports) { |
| 1399 | if (exportInfo.provided === false) continue; |
| 1400 | const used = exportsInfo.getUsedName([exportInfo.name], runtime); |
| 1401 | if (!used) continue; |
| 1402 | if (used instanceof InlinedUsedName) { |
| 1403 | // An inlined export isn't reachable by name on the raw exports object, |
| 1404 | // so the decoupled object must expose the inlined value directly. |
| 1405 | mangled = true; |
| 1406 | definitions.push( |
| 1407 | `${propertyName(exportInfo.name)}: ${this.returningFunction( |
| 1408 | used.render( |
| 1409 | Template.toNormalComment( |
| 1410 | `inlined export ${propertyAccess([exportInfo.name])}` |
| 1411 | ) |
| 1412 | ) |
| 1413 | )}` |
| 1414 | ); |
| 1415 | continue; |
| 1416 | } |
| 1417 | if (used[used.length - 1] !== exportInfo.name) mangled = true; |
| 1418 | definitions.push( |
| 1419 | `${propertyName(exportInfo.name)}: ${this.returningFunction( |
| 1420 | `${importVar}${propertyAccess(/** @type {string[]} */ (used))}` |
| 1421 | )}` |
| 1422 | ); |
| 1423 | } |
| 1424 | if (!mangled) return; |
| 1425 | const name = `${importVar}_namespace_object`; |
| 1426 | runtimeRequirements.add(RuntimeGlobals.exports); |
| 1427 | runtimeRequirements.add(RuntimeGlobals.makeNamespaceObject); |
| 1428 | runtimeRequirements.add(RuntimeGlobals.definePropertyGetters); |
| 1429 | initFragments.push( |
| 1430 | new InitFragment( |
| 1431 | `var ${name} = {};\n${RuntimeGlobals.makeNamespaceObject}(${name});\n${ |
| 1432 | RuntimeGlobals.definePropertyGetters |
| 1433 | }(${name}, {\n\t${definitions.join(",\n\t")}\n});\n`, |
| 1434 | InitFragment.STAGE_PROVIDES, |
| 1435 | 0, |
| 1436 | name |
| 1437 | ) |
| 1438 | ); |
| 1439 | return name; |
| 1440 | } |
| 1441 | |
| 1442 | /** |
| 1443 | * Returns expression. |
no test coverage detected