webpack/lib/dependencies/HarmonyExportSpecifierDepen...

124 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2015-01-13 00:45:30 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2018-10-09 20:30:59 +08:00
const makeSerializable = require("../util/makeSerializable");
const HarmonyExportInitFragment = require("./HarmonyExportInitFragment");
const NullDependency = require("./NullDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
2018-07-30 23:08:51 +08:00
/** @typedef {import("../Dependency")} Dependency */
2018-07-25 15:33:48 +08:00
/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2018-07-17 22:34:36 +08:00
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
2023-04-12 03:22:51 +08:00
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
class HarmonyExportSpecifierDependency extends NullDependency {
2023-05-22 04:31:30 +08:00
/**
2025-04-07 21:09:05 +08:00
* @param {string} id the id
* @param {string} name the name
2023-05-22 04:31:30 +08:00
*/
constructor(id, name) {
super();
this.id = id;
this.name = name;
}
get type() {
return "harmony export specifier";
}
2018-07-25 15:33:48 +08:00
/**
* Returns the exported names
2018-07-17 22:34:36 +08:00
* @param {ModuleGraph} moduleGraph module graph
2018-07-25 15:33:48 +08:00
* @returns {ExportsSpec | undefined} export names
*/
2018-07-17 22:34:36 +08:00
getExports(moduleGraph) {
return {
2018-04-04 19:42:37 +08:00
exports: [this.name],
priority: 1,
terminalBinding: true,
2018-04-04 19:42:37 +08:00
dependencies: undefined
};
}
2018-10-09 20:30:59 +08:00
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {ConnectionState} how this dependency connects the module to referencing modules
*/
getModuleEvaluationSideEffectsState(moduleGraph) {
return false;
}
2023-04-12 03:22:51 +08:00
/**
* @param {ObjectSerializerContext} context context
*/
2018-10-09 20:30:59 +08:00
serialize(context) {
const { write } = context;
write(this.id);
write(this.name);
super.serialize(context);
}
2023-04-12 03:22:51 +08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2018-10-09 20:30:59 +08:00
deserialize(context) {
const { read } = context;
this.id = read();
this.name = read();
super.deserialize(context);
}
2015-01-13 00:45:30 +08:00
}
2018-10-09 20:30:59 +08:00
makeSerializable(
HarmonyExportSpecifierDependency,
"webpack/lib/dependencies/HarmonyExportSpecifierDependency"
);
2020-11-26 17:52:55 +08:00
HarmonyExportSpecifierDependency.Template = class HarmonyExportSpecifierDependencyTemplate extends (
NullDependency.Template
) {
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
2018-11-17 01:18:44 +08:00
apply(
dependency,
source,
{ module, moduleGraph, initFragments, runtime, concatenationScope }
2018-11-17 01:18:44 +08:00
) {
const dep = /** @type {HarmonyExportSpecifierDependency} */ (dependency);
if (concatenationScope) {
concatenationScope.registerExport(dep.name, dep.id);
return;
}
const used = moduleGraph
.getExportsInfo(module)
.getUsedName(dep.name, runtime);
2018-02-25 09:00:20 +08:00
if (!used) {
const set = new Set();
set.add(dep.name || "namespace");
initFragments.push(
new HarmonyExportInitFragment(module.exportsArgument, undefined, set)
);
return;
}
const map = new Map();
map.set(used, `/* binding */ ${dep.id}`);
initFragments.push(
new HarmonyExportInitFragment(module.exportsArgument, map, undefined)
);
2015-01-13 00:45:30 +08:00
}
2017-01-11 17:51:58 +08:00
};
2015-01-13 00:45:30 +08:00
module.exports = HarmonyExportSpecifierDependency;