webpack/lib/dependencies/HarmonyExportExpressionDepe...

74 lines
2.2 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 NullDependency = require("./NullDependency");
2015-01-13 00:45:30 +08:00
/** @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 */
2018-07-30 23:08:51 +08:00
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
class HarmonyExportExpressionDependency extends NullDependency {
constructor(originModule, range, rangeStatement) {
super();
this.originModule = originModule;
this.range = range;
this.rangeStatement = rangeStatement;
}
get type() {
return "harmony export expression";
}
2018-07-25 15:33:48 +08:00
/**
* Returns the exported names
* @returns {ExportsSpec | undefined} export names
*/
getExports() {
return {
2018-04-04 19:42:37 +08:00
exports: ["default"],
dependencies: undefined
};
}
2015-01-13 00:45:30 +08:00
}
HarmonyExportExpressionDependency.Template = class HarmonyExportDependencyTemplate 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 {RuntimeTemplate} runtimeTemplate the runtime template
* @param {DependencyTemplates} dependencyTemplates the dependency templates
* @returns {void}
*/
apply(dependency, source, runtimeTemplate, dependencyTemplates) {
const dep = /** @type {HarmonyExportExpressionDependency} */ (dependency);
const used = dep.originModule.isUsed("default");
const content = this.getContent(dep.originModule, used);
2018-02-25 09:00:20 +08:00
if (dep.range) {
2017-01-18 05:26:38 +08:00
source.replace(dep.rangeStatement[0], dep.range[0] - 1, content + "(");
source.replace(dep.range[1], dep.rangeStatement[1] - 1, ");");
return;
}
2015-11-15 05:41:06 +08:00
source.replace(dep.rangeStatement[0], dep.rangeStatement[1] - 1, content);
}
getContent(module, used) {
const exportsName = module.exportsArgument;
2018-02-25 09:00:20 +08:00
if (used) {
return `/* harmony default export */ ${exportsName}[${JSON.stringify(
used
)}] = `;
}
return "/* unused harmony default export */ var _unused_webpack_default_export = ";
}
2017-01-11 17:51:58 +08:00
};
module.exports = HarmonyExportExpressionDependency;