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-11 17:51:58 +08:00
|
|
|
"use strict";
|
2018-04-04 15:17:10 +08:00
|
|
|
|
2018-07-23 20:53:50 +08:00
|
|
|
const InitFragment = require("../InitFragment");
|
2019-05-23 02:11:16 +08:00
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
2018-07-30 23:08:51 +08:00
|
|
|
const Template = require("../Template");
|
2019-05-23 02:11:16 +08:00
|
|
|
const AsyncModuleInitFragment = require("../async-modules/AsyncModuleInitFragment");
|
2018-04-04 15:17:10 +08:00
|
|
|
const DependencyReference = require("./DependencyReference");
|
2017-08-08 15:32:43 +08:00
|
|
|
const ModuleDependency = require("./ModuleDependency");
|
2015-01-13 00:45:30 +08:00
|
|
|
|
2018-07-23 23:33:29 +08:00
|
|
|
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
2018-07-24 23:35:36 +08:00
|
|
|
/** @typedef {import("webpack-sources").Source} Source */
|
2018-08-28 17:50:33 +08:00
|
|
|
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("../Dependency")} Dependency */
|
2018-07-18 01:38:42 +08:00
|
|
|
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
2018-07-31 03:39:17 +08:00
|
|
|
/** @typedef {import("../Module")} Module */
|
2018-07-17 22:42:05 +08:00
|
|
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
2018-07-24 23:35:36 +08:00
|
|
|
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
2018-07-25 15:33:48 +08:00
|
|
|
/** @typedef {import("../util/createHash").Hash} Hash */
|
2018-07-23 23:33:29 +08:00
|
|
|
|
2017-01-08 01:24:30 +08:00
|
|
|
class HarmonyImportDependency extends ModuleDependency {
|
2018-08-07 01:39:43 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} request request string
|
|
|
|
* @param {number} sourceOrder source order
|
|
|
|
*/
|
2018-09-27 19:40:58 +08:00
|
|
|
constructor(request, sourceOrder) {
|
2017-01-08 01:24:30 +08:00
|
|
|
super(request);
|
2017-08-08 15:32:43 +08:00
|
|
|
this.sourceOrder = sourceOrder;
|
2019-05-23 02:11:16 +08:00
|
|
|
this.await = false;
|
2017-01-08 01:24:30 +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
|
|
|
if (!moduleGraph.getModule(this)) return null;
|
2018-06-08 19:20:57 +08:00
|
|
|
return new DependencyReference(
|
2018-07-24 21:30:37 +08:00
|
|
|
() => moduleGraph.getModule(this),
|
2019-05-29 17:51:47 +08:00
|
|
|
DependencyReference.NO_IMPORTED_NAMES,
|
2018-06-08 19:20:57 +08:00
|
|
|
this.weak,
|
|
|
|
this.sourceOrder
|
|
|
|
);
|
2017-01-08 01:24:30 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 23:35:36 +08:00
|
|
|
/**
|
|
|
|
* @param {ModuleGraph} moduleGraph the module graph
|
|
|
|
* @returns {string} name of the variable for the import
|
|
|
|
*/
|
|
|
|
getImportVar(moduleGraph) {
|
2018-09-27 19:40:58 +08:00
|
|
|
const module = moduleGraph.getParentModule(this);
|
|
|
|
const meta = moduleGraph.getMeta(module);
|
|
|
|
let importVarMap = meta.importVarMap;
|
|
|
|
if (!importVarMap) meta.importVarMap = importVarMap = new Map();
|
2018-07-24 21:30:37 +08:00
|
|
|
let importVar = importVarMap.get(moduleGraph.getModule(this));
|
2018-02-25 09:00:20 +08:00
|
|
|
if (importVar) return importVar;
|
|
|
|
importVar = `${Template.toIdentifier(
|
|
|
|
`${this.userRequest}`
|
|
|
|
)}__WEBPACK_IMPORTED_MODULE_${importVarMap.size}__`;
|
2018-07-24 21:30:37 +08:00
|
|
|
importVarMap.set(moduleGraph.getModule(this), importVar);
|
2017-08-08 15:32:43 +08:00
|
|
|
return importVar;
|
|
|
|
}
|
|
|
|
|
2018-07-24 23:35:36 +08:00
|
|
|
/**
|
|
|
|
* @param {boolean} update create new variables or update existing one
|
|
|
|
* @param {DependencyTemplateContext} templateContext the template context
|
|
|
|
* @returns {string} name of the variable for the import
|
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
getImportStatement(
|
|
|
|
update,
|
2018-11-17 01:18:44 +08:00
|
|
|
{ runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
|
2018-08-28 17:50:33 +08:00
|
|
|
) {
|
2018-08-07 20:20:53 +08:00
|
|
|
return runtimeTemplate.importStatement({
|
2017-12-19 22:50:09 +08:00
|
|
|
update,
|
2018-07-24 21:30:37 +08:00
|
|
|
module: moduleGraph.getModule(this),
|
2018-08-28 17:50:33 +08:00
|
|
|
chunkGraph,
|
2018-07-24 23:35:36 +08:00
|
|
|
importVar: this.getImportVar(moduleGraph),
|
2017-12-19 22:50:09 +08:00
|
|
|
request: this.request,
|
2018-11-17 01:18:44 +08:00
|
|
|
originModule: module,
|
|
|
|
runtimeRequirements
|
2017-12-19 22:50:09 +08:00
|
|
|
});
|
2017-08-08 15:32:43 +08:00
|
|
|
}
|
|
|
|
|
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 importedModule = chunkGraph.moduleGraph.getModule(this);
|
2018-02-25 09:00:20 +08:00
|
|
|
hash.update(
|
|
|
|
(importedModule &&
|
|
|
|
(!importedModule.buildMeta || importedModule.buildMeta.exportsType)) +
|
|
|
|
""
|
|
|
|
);
|
2017-01-08 01:24:30 +08:00
|
|
|
}
|
2018-10-09 20:30:59 +08:00
|
|
|
|
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
|
|
|
write(this.sourceOrder);
|
2019-05-24 21:01:38 +08:00
|
|
|
write(this.await);
|
2018-10-09 20:30:59 +08:00
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
|
|
|
this.sourceOrder = read();
|
2019-05-24 21:01:38 +08:00
|
|
|
this.await = read();
|
2018-10-09 20:30:59 +08:00
|
|
|
super.deserialize(context);
|
|
|
|
}
|
2017-01-08 01:24:30 +08:00
|
|
|
}
|
|
|
|
|
2017-08-08 15:32:43 +08:00
|
|
|
module.exports = HarmonyImportDependency;
|
|
|
|
|
|
|
|
const importEmittedMap = new WeakMap();
|
|
|
|
|
2018-07-23 23:33:29 +08:00
|
|
|
HarmonyImportDependency.Template = class HarmonyImportDependencyTemplate extends ModuleDependency.Template {
|
|
|
|
/**
|
|
|
|
* @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) {
|
2018-07-27 17:45:12 +08:00
|
|
|
const dep = /** @type {HarmonyImportDependency} */ (dependency);
|
2018-07-24 21:30:37 +08:00
|
|
|
const { module, moduleGraph } = templateContext;
|
2018-07-27 17:45:12 +08:00
|
|
|
|
2018-07-24 21:30:37 +08:00
|
|
|
const referencedModule = moduleGraph.getModule(dep);
|
|
|
|
const moduleKey = referencedModule
|
|
|
|
? referencedModule.identifier()
|
|
|
|
: dep.request;
|
2018-07-30 17:51:27 +08:00
|
|
|
const key = `harmony import ${moduleKey}`;
|
2018-07-23 20:53:50 +08:00
|
|
|
|
2018-07-31 03:39:17 +08:00
|
|
|
if (module) {
|
|
|
|
let emittedModules = importEmittedMap.get(dep);
|
|
|
|
if (emittedModules === undefined) {
|
|
|
|
emittedModules = new WeakSet();
|
|
|
|
importEmittedMap.set(dep, emittedModules);
|
|
|
|
}
|
|
|
|
emittedModules.add(module);
|
|
|
|
}
|
|
|
|
|
2018-11-16 23:40:03 +08:00
|
|
|
templateContext.initFragments.push(
|
2018-07-23 20:53:50 +08:00
|
|
|
new InitFragment(
|
2018-07-24 23:35:36 +08:00
|
|
|
dep.getImportStatement(false, templateContext),
|
2018-07-30 16:15:18 +08:00
|
|
|
InitFragment.STAGE_HARMONY_IMPORTS,
|
2018-07-30 17:51:27 +08:00
|
|
|
dep.sourceOrder,
|
|
|
|
key
|
2018-07-23 20:53:50 +08:00
|
|
|
)
|
2018-11-16 23:40:03 +08:00
|
|
|
);
|
2019-05-23 02:11:16 +08:00
|
|
|
if (dep.await) {
|
|
|
|
templateContext.runtimeRequirements.add(RuntimeGlobals.module);
|
|
|
|
templateContext.initFragments.push(
|
2019-05-24 21:19:45 +08:00
|
|
|
new AsyncModuleInitFragment(
|
|
|
|
templateContext.module.moduleArgument,
|
|
|
|
false,
|
|
|
|
[dep.getImportVar(templateContext.moduleGraph)]
|
|
|
|
)
|
2019-05-23 02:11:16 +08:00
|
|
|
);
|
|
|
|
}
|
2018-11-16 23:40:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Dependency} dep the dependency
|
|
|
|
* @param {Module} module the module
|
|
|
|
* @returns {boolean} true, when for this dependency and module a import init fragment was created
|
|
|
|
*/
|
|
|
|
static isImportEmitted(dep, module) {
|
|
|
|
const emittedModules = importEmittedMap.get(dep);
|
|
|
|
return emittedModules !== undefined && emittedModules.has(module);
|
2015-01-13 00:45:30 +08:00
|
|
|
}
|
2017-08-08 15:32:43 +08:00
|
|
|
};
|