webpack/lib/dependencies/HarmonyImportSpecifierDepen...

154 lines
3.7 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";
const HarmonyImportDependency = require("./HarmonyImportDependency");
const HiddenStackError = require("../HiddenStackError");
2015-01-13 00:45:30 +08:00
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.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;
}
get type() {
return "harmony import specifier";
}
getReference() {
2018-02-25 09:00:20 +08:00
if (!this.module) return null;
return {
module: this.module,
2018-02-25 09:00:20 +08:00
importedNames:
this.id && !this.namespaceObjectAsContext ? [this.id] : true
};
}
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();
}
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"
) {
// In strict harmony modules we only support the default export
2018-02-25 09:00:20 +08:00
const exportName = this.id
? `the named export '${this.id}'`
: "the namespace object";
const err = new HiddenStackError(
2018-03-26 22:56:10 +08:00
`Can't import ${exportName} from non EcmaScript module (only default export is available)`
2018-02-25 09:00:20 +08:00
);
return [err];
}
return;
}
2018-02-25 09:00:20 +08:00
if (!this.id) {
return;
}
2018-02-25 09:00:20 +08:00
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
}'${idIsNotNameMessage} was not found in '${this.userRequest}'`;
const err = new HiddenStackError(errorMessage);
return [err];
}
2018-03-13 16:45:23 +08:00
// implement this method to allow the occurrence order plugin to count correctly
getNumberOfIdOccurrences() {
return 0;
}
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)) + ""
);
hash.update(
(importedModule &&
(!importedModule.buildMeta || importedModule.buildMeta.exportsType)) +
""
);
hash.update(
(importedModule &&
importedModule.used + JSON.stringify(importedModule.usedExports)) + ""
);
}
}
HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
apply(dep, source, runtime) {
super.apply(dep, source, runtime);
const content = this.getContent(dep, runtime);
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;