webpack/lib/dependencies/HarmonyImportSpecifierDepen...

227 lines
6.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";
2018-04-04 15:17:10 +08:00
2018-07-30 23:08:51 +08:00
const HarmonyLinkingError = require("../HarmonyLinkingError");
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 */
2018-07-30 23:08:51 +08:00
/** @typedef {import("../util/createHash").Hash} Hash */
const idSymbol = Symbol("HarmonyImportSpecifierDependency.id");
class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
2018-02-25 09:00:20 +08:00
constructor(
request,
sourceOrder,
parserScope,
id,
name,
range,
strictExportPresence
) {
super(request, sourceOrder, parserScope);
2017-12-07 03:37:58 +08:00
this.id = id === null ? null : `${id}`;
this.name = name;
this.range = range;
2017-02-23 05:31:46 +08:00
this.strictExportPresence = strictExportPresence;
this.namespaceObjectAsContext = false;
this.callArgs = undefined;
this.call = undefined;
this.directImport = undefined;
this.shorthand = undefined;
}
get type() {
return "harmony import specifier";
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {string} the imported id
*/
getId(moduleGraph) {
return moduleGraph.getMeta(this)[idSymbol] || this.id;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {string} id the imported id
* @returns {void}
*/
setId(moduleGraph, id) {
moduleGraph.getMeta(this)[idSymbol] = id;
}
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;
2018-04-04 15:17:10 +08:00
return new DependencyReference(
() => moduleGraph.getModule(this),
this.getId(moduleGraph) && !this.namespaceObjectAsContext
? [this.getId(moduleGraph)]
: true,
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 [];
}
_getErrors(moduleGraph) {
const importedModule = moduleGraph.getModule(this);
2018-02-25 09:00:20 +08:00
if (!importedModule) {
return;
}
2018-02-25 09:00:20 +08:00
if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) {
// It's not an harmony module
2018-02-25 09:00:20 +08:00
if (
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule &&
this.getId(moduleGraph) !== "default"
2018-02-25 09:00:20 +08:00
) {
// In strict harmony modules we only support the default export
const exportName = this.getId(moduleGraph)
? `the named export '${this.getId(moduleGraph)}'`
2018-02-25 09:00:20 +08:00
: "the namespace object";
2018-04-04 12:10:07 +08:00
return [
new HarmonyLinkingError(
`Can't import ${exportName} from non EcmaScript module (only default export is available)`
)
];
}
return;
}
if (!this.getId(moduleGraph)) {
return;
}
if (importedModule.isProvided(this.getId(moduleGraph)) !== false) {
// It's provided or we are not sure
return;
}
// We are sure that it's not provided
2018-02-25 09:00:20 +08:00
const idIsNotNameMessage =
this.getId(moduleGraph) !== this.name
? ` (imported as '${this.name}')`
: "";
const errorMessage = `"export '${this.getId(
moduleGraph
)}'${idIsNotNameMessage} was not found in '${this.userRequest}'`;
2018-04-04 12:10:07 +08:00
return [new HarmonyLinkingError(errorMessage)];
}
/**
* 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);
hash.update((importedModule && this.getId(moduleGraph)) + "");
2018-02-25 09:00:20 +08:00
hash.update(
(importedModule &&
this.getId(moduleGraph) &&
importedModule.getUsedName(moduleGraph, this.getId(moduleGraph))) + ""
2018-02-25 09:00:20 +08:00
);
hash.update(
(importedModule &&
(!importedModule.buildMeta || importedModule.buildMeta.exportsType)) +
""
);
if (importedModule) {
const usedExports = moduleGraph.getUsedExports(importedModule);
const stringifyUsedExports = JSON.stringify(usedExports);
hash.update(stringifyUsedExports);
}
}
}
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 }) {
const exportExpr = runtimeTemplate.exportFromImport({
moduleGraph,
module: moduleGraph.getModule(dep),
2018-02-10 00:35:28 +08:00
request: dep.request,
exportName: dep.getId(moduleGraph),
originModule: module,
asiSafe: dep.shorthand,
isCall: dep.call,
callContext: !dep.directImport,
importVar: dep.getImportVar(moduleGraph)
});
return dep.shorthand ? `${dep.name}: ${exportExpr}` : exportExpr;
}
2017-01-11 17:51:58 +08:00
};
module.exports = HarmonyImportSpecifierDependency;