2015-01-13 00:45:30 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-05 02:50:35 +08:00
|
|
|
"use strict";
|
2017-08-08 15:32:43 +08:00
|
|
|
const HarmonyImportDependency = require("./HarmonyImportDependency");
|
2015-01-13 00:45:30 +08:00
|
|
|
|
2017-08-08 15:32:43 +08:00
|
|
|
class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
2018-02-25 09:00:20 +08:00
|
|
|
constructor(
|
|
|
|
request,
|
|
|
|
originModule,
|
|
|
|
sourceOrder,
|
|
|
|
parserScope,
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
range,
|
|
|
|
strictExportPresence
|
|
|
|
) {
|
2017-08-08 15:32:43 +08:00
|
|
|
super(request, originModule, sourceOrder, parserScope);
|
2017-12-07 03:37:58 +08:00
|
|
|
this.id = id === null ? null : `${id}`;
|
2017-01-05 02:50:35 +08:00
|
|
|
this.name = name;
|
|
|
|
this.range = range;
|
2017-02-23 05:31:46 +08:00
|
|
|
this.strictExportPresence = strictExportPresence;
|
2017-05-21 15:13:33 +08:00
|
|
|
this.namespaceObjectAsContext = false;
|
|
|
|
this.callArgs = undefined;
|
|
|
|
this.call = undefined;
|
|
|
|
this.directImport = undefined;
|
2017-01-05 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
return "harmony import specifier";
|
|
|
|
}
|
|
|
|
|
|
|
|
getReference() {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!this.module) return null;
|
2017-01-05 02:50:35 +08:00
|
|
|
return {
|
2017-08-08 15:32:43 +08:00
|
|
|
module: this.module,
|
2018-02-25 09:00:20 +08:00
|
|
|
importedNames:
|
|
|
|
this.id && !this.namespaceObjectAsContext ? [this.id] : true
|
2017-01-05 02:50:35 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2017-08-08 15:32:43 +08:00
|
|
|
const importedModule = this.module;
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!importedModule) {
|
2017-11-23 21:10:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) {
|
2017-11-23 21:10:52 +08:00
|
|
|
// It's not an harmony module
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
this.originModule.buildMeta.strictHarmonyModule &&
|
|
|
|
this.id !== "default"
|
|
|
|
) {
|
2017-11-23 21:10:52 +08:00
|
|
|
// 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 Error(
|
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
|
|
|
);
|
2017-11-23 21:10:52 +08:00
|
|
|
err.hideStack = true;
|
|
|
|
return [err];
|
|
|
|
}
|
2017-01-05 02:50:35 +08:00
|
|
|
return;
|
2016-06-24 07:52:28 +08:00
|
|
|
}
|
2017-01-05 02:50:35 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (!this.id) {
|
2017-01-05 02:50:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (importedModule.isProvided(this.id) !== false) {
|
2017-11-23 21:10:52 +08:00
|
|
|
// It's provided or we are not sure
|
2017-01-05 02:50:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 21:10:52 +08:00
|
|
|
// 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}'`;
|
2017-01-05 02:50:35 +08:00
|
|
|
const err = new Error(errorMessage);
|
|
|
|
err.hideStack = true;
|
|
|
|
return [err];
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:45:23 +08:00
|
|
|
// implement this method to allow the occurrence order plugin to count correctly
|
2017-08-08 15:32:43 +08:00
|
|
|
getNumberOfIdOccurrences() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-05 02:50:35 +08:00
|
|
|
updateHash(hash) {
|
|
|
|
super.updateHash(hash);
|
2017-08-08 15:32:43 +08:00
|
|
|
const importedModule = this.module;
|
2017-01-05 02:50:35 +08:00
|
|
|
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)) + ""
|
|
|
|
);
|
2017-01-05 02:50:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:32:43 +08:00
|
|
|
HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
|
2017-12-07 17:31:00 +08:00
|
|
|
apply(dep, source, runtime) {
|
|
|
|
super.apply(dep, source, runtime);
|
2017-12-19 22:50:09 +08:00
|
|
|
const content = this.getContent(dep, runtime);
|
2017-01-05 02:50:35 +08:00
|
|
|
source.replace(dep.range[0], dep.range[1] - 1, content);
|
2016-06-24 07:52:28 +08:00
|
|
|
}
|
2017-01-05 02:50:35 +08:00
|
|
|
|
2017-12-19 22:50:09 +08:00
|
|
|
getContent(dep, runtime) {
|
|
|
|
const exportExpr = runtime.exportFromImport({
|
|
|
|
module: dep.module,
|
2018-02-10 00:35:28 +08:00
|
|
|
request: dep.request,
|
2017-12-19 22:50:09 +08:00
|
|
|
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;
|
2016-06-04 21:22:47 +08:00
|
|
|
}
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2017-01-05 02:50:35 +08:00
|
|
|
|
|
|
|
module.exports = HarmonyImportSpecifierDependency;
|