webpack/lib/dependencies/HarmonyImportSpecifierDepen...

205 lines
5.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
*/
"use strict";
2018-04-04 15:17:10 +08:00
const DependencyReference = require("./DependencyReference");
const HarmonyImportDependency = require("./HarmonyImportDependency");
2018-04-04 12:10:07 +08:00
const HarmonyLinkingError = require("../HarmonyLinkingError");
2015-01-13 00:45:30 +08:00
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
2018-07-25 15:33:48 +08:00
/** @typedef {import("../util/createHash").Hash} Hash */
/** @typedef {import("../WebpackError")} WebpackError */
/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
2018-02-25 09:00:20 +08:00
constructor(
request,
originModule,
sourceOrder,
parserScope,
id,
name,
range,
strictExportPresence
) {
super(request, originModule, sourceOrder, parserScope);
2017-12-07 03:37:58 +08:00
this.id = id === null ? null : `${id}`;
this.redirectedId = undefined;
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";
}
get _id() {
return this.redirectedId || this.id;
}
2018-07-25 15:33:48 +08:00
/**
* Returns the referenced module and export
* @returns {DependencyReference} reference
*/
getReference() {
if (!this._module) return null;
2018-04-04 15:17:10 +08:00
return new DependencyReference(
2018-07-23 03:01:05 +08:00
() => this._module,
this._id && !this.namespaceObjectAsContext ? [this._id] : true,
false,
this.sourceOrder
2018-04-04 15:17:10 +08:00
);
}
2018-07-25 15:33:48 +08:00
/**
* Returns warnings
* @returns {WebpackError[]} warnings
*/
getWarnings() {
2018-02-25 09:00:20 +08:00
if (
this.strictExportPresence ||
this.originModule.buildMeta.strictHarmonyModule
) {
2017-02-23 05:31:46 +08:00
return [];
}
return this._getErrors();
}
2018-07-25 15:33:48 +08:00
/**
* Returns errors
* @returns {WebpackError[]} errors
*/
2017-02-23 05:31:46 +08:00
getErrors() {
2018-02-25 09:00:20 +08:00
if (
this.strictExportPresence ||
this.originModule.buildMeta.strictHarmonyModule
) {
2017-02-23 05:31:46 +08:00
return this._getErrors();
}
return [];
}
_getErrors() {
const importedModule = this._module;
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 (
this.originModule.buildMeta.strictHarmonyModule &&
this._id !== "default"
2018-02-25 09:00:20 +08:00
) {
// In strict harmony modules we only support the default export
const exportName = this._id
? `the named export '${this._id}'`
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._id) {
return;
}
if (importedModule.isProvided(this._id) !== 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._id !== this.name ? ` (imported as '${this.name}')` : "";
2018-03-26 22:56:10 +08:00
const errorMessage = `"export '${
this._id
2018-03-26 22:56:10 +08:00
}'${idIsNotNameMessage} was not found in '${this.userRequest}'`;
2018-04-04 12:10:07 +08:00
return [new HarmonyLinkingError(errorMessage)];
}
2018-03-13 16:45:23 +08:00
// implement this method to allow the occurrence order plugin to count correctly
getNumberOfIdOccurrences() {
return 0;
}
2018-07-25 15:33:48 +08:00
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @returns {void}
*/
updateHash(hash) {
super.updateHash(hash);
const importedModule = this._module;
hash.update((importedModule && this._id) + "");
2018-02-25 09:00:20 +08:00
hash.update(
(importedModule && this._id && importedModule.isUsed(this._id)) + ""
2018-02-25 09:00:20 +08:00
);
hash.update(
(importedModule &&
(!importedModule.buildMeta || importedModule.buildMeta.exportsType)) +
""
);
hash.update(
(importedModule &&
importedModule.used + JSON.stringify(importedModule.usedExports)) + ""
);
}
2018-07-25 15:33:48 +08:00
/**
* Disconnect the dependency from the graph
* @returns {void}
*/
disconnect() {
super.disconnect();
this.redirectedId = undefined;
}
}
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 {RuntimeTemplate} runtimeTemplate the runtime template
* @param {DependencyTemplates} dependencyTemplates the dependency templates
* @returns {void}
*/
apply(dependency, source, runtimeTemplate, dependencyTemplates) {
const dep = /** @type {HarmonyImportSpecifierDependency} */ (dependency);
super.apply(dep, source, runtimeTemplate, dependencyTemplates);
const content = this.getContent(dep, runtimeTemplate);
source.replace(dep.range[0], dep.range[1] - 1, content);
}
getContent(dep, runtime) {
const exportExpr = runtime.exportFromImport({
module: dep._module,
2018-02-10 00:35:28 +08:00
request: dep.request,
exportName: dep._id,
originModule: dep.originModule,
asiSafe: dep.shorthand,
isCall: dep.call,
callContext: !dep.directImport,
importVar: dep.getImportVar()
});
return dep.shorthand ? `${dep.name}: ${exportExpr}` : exportExpr;
}
2017-01-11 17:51:58 +08:00
};
module.exports = HarmonyImportSpecifierDependency;