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
|
|
|
|
2017-01-05 02:50:35 +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");
|
2017-08-08 15:32:43 +08:00
|
|
|
const HarmonyImportDependency = require("./HarmonyImportDependency");
|
2015-01-13 00:45:30 +08:00
|
|
|
|
2018-07-23 23:33:29 +08:00
|
|
|
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
2018-08-28 17:50:33 +08:00
|
|
|
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("../Dependency")} Dependency */
|
|
|
|
/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
2018-07-18 01:38:42 +08:00
|
|
|
/** @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 */
|
2018-07-23 23:33:29 +08:00
|
|
|
|
2019-03-14 19:06:59 +08:00
|
|
|
const idsSymbol = Symbol("HarmonyImportSpecifierDependency.ids");
|
2018-08-16 20:07:34 +08:00
|
|
|
|
2017-08-08 15:32:43 +08:00
|
|
|
class HarmonyImportSpecifierDependency extends HarmonyImportDependency {
|
2019-03-14 19:06:59 +08:00
|
|
|
constructor(request, sourceOrder, ids, name, range, strictExportPresence) {
|
2018-09-27 19:40:58 +08:00
|
|
|
super(request, sourceOrder);
|
2019-03-14 19:06:59 +08:00
|
|
|
this.ids = ids;
|
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.call = undefined;
|
|
|
|
this.directImport = undefined;
|
2018-03-30 08:33:19 +08:00
|
|
|
this.shorthand = undefined;
|
2017-01-05 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:06:59 +08:00
|
|
|
// 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[]");
|
|
|
|
}
|
|
|
|
|
2017-01-05 02:50:35 +08:00
|
|
|
get type() {
|
|
|
|
return "harmony import specifier";
|
|
|
|
}
|
|
|
|
|
2018-07-24 23:35:36 +08:00
|
|
|
/**
|
|
|
|
* @param {ModuleGraph} moduleGraph the module graph
|
2019-03-14 19:06:59 +08:00
|
|
|
* @returns {string[]} the imported ids
|
2018-07-24 23:35:36 +08:00
|
|
|
*/
|
2019-03-14 19:06:59 +08:00
|
|
|
getIds(moduleGraph) {
|
|
|
|
return moduleGraph.getMeta(this)[idsSymbol] || this.ids;
|
2018-08-16 20:07:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ModuleGraph} moduleGraph the module graph
|
2019-03-14 19:06:59 +08:00
|
|
|
* @param {string[]} ids the imported ids
|
2018-08-16 20:07:34 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-03-14 19:06:59 +08:00
|
|
|
setIds(moduleGraph, ids) {
|
|
|
|
moduleGraph.getMeta(this)[idsSymbol] = ids;
|
2018-05-27 05:07:02 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* Returns the referenced module and export
|
2018-07-24 23:35:36 +08:00
|
|
|
* @param {ModuleGraph} moduleGraph module graph
|
2018-07-25 15:33:48 +08:00
|
|
|
* @returns {DependencyReference} reference
|
|
|
|
*/
|
2018-07-24 23:35:36 +08:00
|
|
|
getReference(moduleGraph) {
|
2018-07-24 21:30:37 +08:00
|
|
|
const module = moduleGraph.getModule(this);
|
|
|
|
if (!module) return null;
|
2019-03-14 19:06:59 +08:00
|
|
|
const ids = this.getIds(moduleGraph);
|
2018-04-04 15:17:10 +08:00
|
|
|
return new DependencyReference(
|
2018-07-24 21:30:37 +08:00
|
|
|
() => moduleGraph.getModule(this),
|
2019-05-30 03:59:09 +08:00
|
|
|
[this.namespaceObjectAsContext ? ids.slice(0, -1) : ids],
|
2018-06-08 19:20:57 +08:00
|
|
|
false,
|
|
|
|
this.sourceOrder
|
2018-04-04 15:17:10 +08:00
|
|
|
);
|
2017-01-05 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* Returns warnings
|
2018-07-24 23:35:36 +08:00
|
|
|
* @param {ModuleGraph} moduleGraph module graph
|
2018-07-25 15:33:48 +08:00
|
|
|
* @returns {WebpackError[]} warnings
|
|
|
|
*/
|
2018-07-24 23:35:36 +08:00
|
|
|
getWarnings(moduleGraph) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
this.strictExportPresence ||
|
2018-08-07 20:20:53 +08:00
|
|
|
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
|
2018-02-25 09:00:20 +08:00
|
|
|
) {
|
2017-02-23 05:31:46 +08:00
|
|
|
return [];
|
|
|
|
}
|
2018-07-24 23:35:36 +08:00
|
|
|
return this._getErrors(moduleGraph);
|
2017-02-23 05:31:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* Returns errors
|
2018-07-24 23:35:36 +08:00
|
|
|
* @param {ModuleGraph} moduleGraph module graph
|
2018-07-25 15:33:48 +08:00
|
|
|
* @returns {WebpackError[]} errors
|
|
|
|
*/
|
2018-07-24 23:35:36 +08:00
|
|
|
getErrors(moduleGraph) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
this.strictExportPresence ||
|
2018-08-07 20:20:53 +08:00
|
|
|
moduleGraph.getParentModule(this).buildMeta.strictHarmonyModule
|
2018-02-25 09:00:20 +08:00
|
|
|
) {
|
2018-07-24 23:35:36 +08:00
|
|
|
return this._getErrors(moduleGraph);
|
2017-02-23 05:31:46 +08:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-12-30 16:03:42 +08:00
|
|
|
/**
|
|
|
|
* @param {ModuleGraph} moduleGraph module graph
|
|
|
|
* @returns {WebpackError[] | undefined} errors
|
|
|
|
*/
|
2018-07-24 23:35:36 +08:00
|
|
|
_getErrors(moduleGraph) {
|
2019-03-14 19:06:59 +08:00
|
|
|
const ids = this.getIds(moduleGraph);
|
2019-06-12 20:31:36 +08:00
|
|
|
return this.getLinkingErrors(
|
|
|
|
moduleGraph,
|
|
|
|
ids,
|
|
|
|
`(imported as '${this.name}')`
|
|
|
|
);
|
2017-01-05 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2018-07-20 22:13:22 +08:00
|
|
|
/**
|
|
|
|
* implement this method to allow the occurrence order plugin to count correctly
|
|
|
|
* @returns {number} count how often the id is used in this dependency
|
|
|
|
*/
|
2017-08-08 15:32:43 +08:00
|
|
|
getNumberOfIdOccurrences() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* Update the hash
|
|
|
|
* @param {Hash} hash hash to be updated
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph chunk graph
|
2018-07-25 15:33:48 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
updateHash(hash, chunkGraph) {
|
|
|
|
super.updateHash(hash, chunkGraph);
|
|
|
|
const moduleGraph = chunkGraph.moduleGraph;
|
2018-07-24 21:30:37 +08:00
|
|
|
const importedModule = moduleGraph.getModule(this);
|
2019-03-14 19:06:59 +08:00
|
|
|
const ids = this.getIds(moduleGraph);
|
|
|
|
hash.update(ids.join());
|
2018-08-07 01:39:43 +08:00
|
|
|
if (importedModule) {
|
2019-03-14 19:06:59 +08:00
|
|
|
const exportsInfo = moduleGraph.getExportsInfo(importedModule);
|
2019-07-25 21:43:15 +08:00
|
|
|
hash.update(`${exportsInfo.getUsedName(ids)}`);
|
2019-02-20 18:32:12 +08:00
|
|
|
hash.update(
|
|
|
|
(!importedModule.buildMeta || importedModule.buildMeta.exportsType) + ""
|
|
|
|
);
|
2018-08-07 01:39:43 +08:00
|
|
|
}
|
2017-01-05 02:50:35 +08:00
|
|
|
}
|
2018-10-09 20:30:59 +08:00
|
|
|
|
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
2019-03-14 19:06:59 +08:00
|
|
|
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;
|
2019-03-14 19:06:59 +08:00
|
|
|
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);
|
|
|
|
}
|
2017-01-05 02:50:35 +08:00
|
|
|
}
|
|
|
|
|
2018-10-09 20:30:59 +08:00
|
|
|
makeSerializable(
|
|
|
|
HarmonyImportSpecifierDependency,
|
|
|
|
"webpack/lib/dependencies/HarmonyImportSpecifierDependency"
|
|
|
|
);
|
|
|
|
|
2017-08-08 15:32:43 +08:00
|
|
|
HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
|
2018-07-23 23:33:29 +08:00
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency for which the template should be applied
|
|
|
|
* @param {ReplaceSource} source the current replace source which can be modified
|
2018-07-18 01:38:42 +08:00
|
|
|
* @param {DependencyTemplateContext} templateContext the context object
|
2018-07-23 23:33:29 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-07-18 01:38:42 +08:00
|
|
|
apply(dependency, source, templateContext) {
|
|
|
|
super.apply(dependency, source, templateContext);
|
2018-07-23 23:33:29 +08:00
|
|
|
const dep = /** @type {HarmonyImportSpecifierDependency} */ (dependency);
|
2018-07-24 23:35:36 +08:00
|
|
|
const content = this.getContent(dep, templateContext);
|
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
|
|
|
|
2018-11-20 16:31:36 +08:00
|
|
|
getContent(
|
|
|
|
dep,
|
|
|
|
{ runtimeTemplate, module, moduleGraph, runtimeRequirements }
|
|
|
|
) {
|
2019-03-14 19:06:59 +08:00
|
|
|
const ids = dep.getIds(moduleGraph);
|
2018-08-07 20:20:53 +08:00
|
|
|
const exportExpr = runtimeTemplate.exportFromImport({
|
2018-08-07 01:39:43 +08:00
|
|
|
moduleGraph,
|
2018-07-24 21:30:37 +08:00
|
|
|
module: moduleGraph.getModule(dep),
|
2018-02-10 00:35:28 +08:00
|
|
|
request: dep.request,
|
2019-03-14 19:06:59 +08:00
|
|
|
exportName: ids,
|
2018-08-07 20:20:53 +08:00
|
|
|
originModule: module,
|
2017-12-19 22:50:09 +08:00
|
|
|
asiSafe: dep.shorthand,
|
|
|
|
isCall: dep.call,
|
|
|
|
callContext: !dep.directImport,
|
2018-11-20 16:31:36 +08:00
|
|
|
importVar: dep.getImportVar(moduleGraph),
|
|
|
|
runtimeRequirements
|
2017-12-19 22:50:09 +08:00
|
|
|
});
|
|
|
|
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;
|