webpack/lib/dependencies/HarmonyImportSpecifierDepen...

229 lines
6.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";
2018-04-04 15:17:10 +08:00
2018-10-09 20:30:59 +08:00
const makeSerializable = require("../util/makeSerializable");
2018-04-04 15:17:10 +08:00
const DependencyReference = require("./DependencyReference");
const HarmonyImportDependency = require("./HarmonyImportDependency");
2015-01-13 00:45:30 +08:00
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
2018-07-30 23:08:51 +08:00
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2018-07-17 22:42:05 +08:00
/** @typedef {import("../ModuleGraph")} ModuleGraph */
2018-07-25 15:33:48 +08:00
/** @typedef {import("../WebpackError")} WebpackError */
2019-07-17 22:02:33 +08:00
/** @typedef {import("../util/Hash")} Hash */
const idsSymbol = Symbol("HarmonyImportSpecifierDependency.ids");
class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
constructor(request, sourceOrder, ids, name, range, strictExportPresence) {
super(request, sourceOrder);
this.ids = ids;
this.name = name;
this.range = range;
2017-02-23 05:31:46 +08:00
this.strictExportPresence = strictExportPresence;
this.namespaceObjectAsContext = false;
this.call = undefined;
this.directImport = undefined;
this.shorthand = undefined;
}
// TODO webpack 6 remove
get id() {
throw new Error("id was renamed to ids and type changed to string[]");
}
// TODO webpack 6 remove
getId() {
throw new Error("id was renamed to ids and type changed to string[]");
}
// TODO webpack 6 remove
setId() {
throw new Error("id was renamed to ids and type changed to string[]");
}
get type() {
return "harmony import specifier";
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {string[]} the imported ids
*/
getIds(moduleGraph) {
return moduleGraph.getMeta(this)[idsSymbol] || this.ids;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {string[]} ids the imported ids
* @returns {void}
*/
setIds(moduleGraph, ids) {
moduleGraph.getMeta(this)[idsSymbol] = ids;
}
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) return null;
const ids = this.getIds(moduleGraph);
2018-04-04 15:17:10 +08:00
return new DependencyReference(
() => moduleGraph.getModule(this),
2019-05-30 03:59:09 +08:00
[this.namespaceObjectAsContext ? ids.slice(0, -1) : ids],
false,
this.sourceOrder
2018-04-04 15:17:10 +08:00
);
}
2018-07-25 15:33:48 +08:00
/**
* Returns warnings
* @param {ModuleGraph} moduleGraph module graph
2018-07-25 15:33:48 +08:00
* @returns {WebpackError[]} warnings
*/
getWarnings(moduleGraph) {
2018-02-25 09:00:20 +08:00
if (
this.strictExportPresence ||
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
2018-02-25 09:00:20 +08:00
) {
2017-02-23 05:31:46 +08:00
return [];
}
return this._getErrors(moduleGraph);
2017-02-23 05:31:46 +08:00
}
2018-07-25 15:33:48 +08:00
/**
* Returns errors
* @param {ModuleGraph} moduleGraph module graph
2018-07-25 15:33:48 +08:00
* @returns {WebpackError[]} errors
*/
getErrors(moduleGraph) {
2018-02-25 09:00:20 +08:00
if (
this.strictExportPresence ||
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
2018-02-25 09:00:20 +08:00
) {
return this._getErrors(moduleGraph);
2017-02-23 05:31:46 +08:00
}
return [];
}
/**
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[] | undefined} errors
*/
_getErrors(moduleGraph) {
const ids = this.getIds(moduleGraph);
return this.getLinkingErrors(
moduleGraph,
ids,
`(imported as '${this.name}')`
);
}
/**
* implement this method to allow the occurrence order plugin to count correctly
* @returns {number} count how often the id is used in this dependency
*/
getNumberOfIdOccurrences() {
return 0;
}
2018-07-25 15:33:48 +08:00
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {ChunkGraph} chunkGraph chunk graph
2018-07-25 15:33:48 +08:00
* @returns {void}
*/
updateHash(hash, chunkGraph) {
super.updateHash(hash, chunkGraph);
const moduleGraph = chunkGraph.moduleGraph;
const importedModule = moduleGraph.getModule(this);
const ids = this.getIds(moduleGraph);
hash.update(ids.join());
if (importedModule) {
const exportsInfo = moduleGraph.getExportsInfo(importedModule);
hash.update(`${exportsInfo.getUsedName(ids)}`);
hash.update(
(!importedModule.buildMeta || importedModule.buildMeta.exportsType) + ""
);
}
}
2018-10-09 20:30:59 +08:00
serialize(context) {
const { write } = context;
write(this.ids);
2018-10-09 20:30:59 +08:00
write(this.name);
write(this.range);
write(this.strictExportPresence);
write(this.namespaceObjectAsContext);
write(this.call);
write(this.directImport);
write(this.shorthand);
super.serialize(context);
}
deserialize(context) {
const { read } = context;
this.ids = read();
2018-10-09 20:30:59 +08:00
this.name = read();
this.range = read();
this.strictExportPresence = read();
this.namespaceObjectAsContext = read();
this.call = read();
this.directImport = read();
this.shorthand = read();
super.deserialize(context);
}
}
2018-10-09 20:30:59 +08:00
makeSerializable(
HarmonyImportSpecifierDependency,
"webpack/lib/dependencies/HarmonyImportSpecifierDependency"
);
HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends HarmonyImportDependency.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) {
super.apply(dependency, source, templateContext);
const dep = /** @type {HarmonyImportSpecifierDependency} */ (dependency);
const content = this.getContent(dep, templateContext);
source.replace(dep.range[0], dep.range[1] - 1, content);
}
getContent(
dep,
{ runtimeTemplate, module, moduleGraph, runtimeRequirements }
) {
const ids = dep.getIds(moduleGraph);
const exportExpr = runtimeTemplate.exportFromImport({
moduleGraph,
module: moduleGraph.getModule(dep),
2018-02-10 00:35:28 +08:00
request: dep.request,
exportName: ids,
originModule: module,
asiSafe: dep.shorthand,
isCall: dep.call,
callContext: !dep.directImport,
importVar: dep.getImportVar(moduleGraph),
runtimeRequirements
});
return dep.shorthand ? `${dep.name}: ${exportExpr}` : exportExpr;
}
2017-01-11 17:51:58 +08:00
};
module.exports = HarmonyImportSpecifierDependency;