1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const Dependency = require("../Dependency");
|
---|
9 | const makeSerializable = require("../util/makeSerializable");
|
---|
10 | const ModuleDependency = require("./ModuleDependency");
|
---|
11 |
|
---|
12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
13 | /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
---|
14 | /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
---|
15 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
16 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
17 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
18 |
|
---|
19 | class ImportDependency extends ModuleDependency {
|
---|
20 | /**
|
---|
21 | * @param {string} request the request
|
---|
22 | * @param {[number, number]} range expression range
|
---|
23 | * @param {string[][]=} referencedExports list of referenced exports
|
---|
24 | */
|
---|
25 | constructor(request, range, referencedExports) {
|
---|
26 | super(request);
|
---|
27 | this.range = range;
|
---|
28 | this.referencedExports = referencedExports;
|
---|
29 | }
|
---|
30 |
|
---|
31 | get type() {
|
---|
32 | return "import()";
|
---|
33 | }
|
---|
34 |
|
---|
35 | get category() {
|
---|
36 | return "esm";
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Returns list of exports referenced by this dependency
|
---|
41 | * @param {ModuleGraph} moduleGraph module graph
|
---|
42 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
43 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
44 | */
|
---|
45 | getReferencedExports(moduleGraph, runtime) {
|
---|
46 | return this.referencedExports
|
---|
47 | ? this.referencedExports.map(e => ({
|
---|
48 | name: e,
|
---|
49 | canMangle: false
|
---|
50 | }))
|
---|
51 | : Dependency.EXPORTS_OBJECT_REFERENCED;
|
---|
52 | }
|
---|
53 |
|
---|
54 | serialize(context) {
|
---|
55 | context.write(this.range);
|
---|
56 | context.write(this.referencedExports);
|
---|
57 | super.serialize(context);
|
---|
58 | }
|
---|
59 |
|
---|
60 | deserialize(context) {
|
---|
61 | this.range = context.read();
|
---|
62 | this.referencedExports = context.read();
|
---|
63 | super.deserialize(context);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | makeSerializable(ImportDependency, "webpack/lib/dependencies/ImportDependency");
|
---|
68 |
|
---|
69 | ImportDependency.Template = class ImportDependencyTemplate extends (
|
---|
70 | ModuleDependency.Template
|
---|
71 | ) {
|
---|
72 | /**
|
---|
73 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
74 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
75 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
76 | * @returns {void}
|
---|
77 | */
|
---|
78 | apply(
|
---|
79 | dependency,
|
---|
80 | source,
|
---|
81 | { runtimeTemplate, module, moduleGraph, chunkGraph, runtimeRequirements }
|
---|
82 | ) {
|
---|
83 | const dep = /** @type {ImportDependency} */ (dependency);
|
---|
84 | const block = /** @type {AsyncDependenciesBlock} */ (
|
---|
85 | moduleGraph.getParentBlock(dep)
|
---|
86 | );
|
---|
87 | const content = runtimeTemplate.moduleNamespacePromise({
|
---|
88 | chunkGraph,
|
---|
89 | block: block,
|
---|
90 | module: moduleGraph.getModule(dep),
|
---|
91 | request: dep.request,
|
---|
92 | strict: module.buildMeta.strictHarmonyModule,
|
---|
93 | message: "import()",
|
---|
94 | runtimeRequirements
|
---|
95 | });
|
---|
96 |
|
---|
97 | source.replace(dep.range[0], dep.range[1] - 1, content);
|
---|
98 | }
|
---|
99 | };
|
---|
100 |
|
---|
101 | module.exports = ImportDependency;
|
---|