webpack/lib/dependencies/HarmonyExportSpecifierDepen...

82 lines
2.3 KiB
JavaScript
Raw 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";
const InitFragment = require("../InitFragment");
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 */
class HarmonyExportSpecifierDependency extends NullDependency {
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],
dependencies: undefined
};
}
2015-01-13 00:45:30 +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}
*/
apply(dependency, source, templateContext) {
// no-op
}
2018-07-27 17:45:12 +08:00
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {DependencyTemplateContext} templateContext the template context
2018-07-27 17:45:12 +08:00
* @returns {InitFragment[]|null} the init fragments
*/
getInitFragments(dependency, { module, moduleGraph }) {
2018-07-30 16:15:18 +08:00
return [
new InitFragment(
this.getContent(dependency, module, moduleGraph),
2018-07-30 16:15:18 +08:00
InitFragment.STAGE_HARMONY_EXPORTS,
1
)
];
}
getContent(dep, module, moduleGraph) {
const used = module.getUsedName(moduleGraph, dep.name);
2018-02-25 09:00:20 +08:00
if (!used) {
return `/* unused harmony export ${dep.name || "namespace"} */\n`;
}
const exportsName = module.exportsArgument;
2018-03-26 22:56:10 +08:00
return `/* harmony export (binding) */ __webpack_require__.d(${exportsName}, ${JSON.stringify(
used
)}, function() { return ${dep.id}; });\n`;
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;