webpack/lib/dependencies/HarmonyImportSideEffectDepe...

61 lines
1.9 KiB
JavaScript
Raw Normal View History

/*
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-07-30 23:08:51 +08:00
const HarmonyImportDependency = require("./HarmonyImportDependency");
2018-07-27 17:45:12 +08:00
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2018-07-27 17:45:12 +08:00
/** @typedef {import("../InitFragment")} InitFragment */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
2018-07-27 17:45:12 +08:00
/** @typedef {import("../util/createHash").Hash} Hash */
2018-07-25 15:33:48 +08:00
/** @typedef {import("./DependencyReference")} DependencyReference */
class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
constructor(request, sourceOrder) {
super(request, sourceOrder);
}
2018-07-25 15:33:48 +08:00
/**
* Returns the referenced module and export
* @param {ModuleGraph} moduleGraph module graph
2018-07-25 15:33:48 +08:00
* @returns {DependencyReference} reference
*/
getReference(moduleGraph) {
const module = moduleGraph.getModule(this);
if (module && module.factoryMeta.sideEffectFree) {
return null;
}
return super.getReference(moduleGraph);
}
get type() {
return "harmony side effect evaluation";
}
}
HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDependencyTemplate extends HarmonyImportDependency.Template {
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, templateContext) {
2018-07-27 17:45:12 +08:00
const dep = /** @type {HarmonyImportSideEffectDependency} */ (dependency);
const { moduleGraph } = templateContext;
const module = moduleGraph.getModule(dep);
if (module && module.factoryMeta.sideEffectFree) {
return null;
} else {
return super.getInitFragments(dep, templateContext);
}
}
};
module.exports = HarmonyImportSideEffectDependency;