1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Ivan Kopeykin @vankop
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { pathToFileURL } = require("url");
|
---|
9 | const ModuleDependencyWarning = require("../ModuleDependencyWarning");
|
---|
10 | const Template = require("../Template");
|
---|
11 | const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
|
---|
12 | const {
|
---|
13 | evaluateToIdentifier,
|
---|
14 | toConstantDependency,
|
---|
15 | evaluateToString,
|
---|
16 | evaluateToNumber
|
---|
17 | } = require("../javascript/JavascriptParserHelpers");
|
---|
18 | const memoize = require("../util/memoize");
|
---|
19 | const propertyAccess = require("../util/propertyAccess");
|
---|
20 | const ConstDependency = require("./ConstDependency");
|
---|
21 |
|
---|
22 | /** @typedef {import("estree").MemberExpression} MemberExpression */
|
---|
23 | /** @typedef {import("../Compiler")} Compiler */
|
---|
24 | /** @typedef {import("../NormalModule")} NormalModule */
|
---|
25 | /** @typedef {import("../javascript/JavascriptParser")} Parser */
|
---|
26 |
|
---|
27 | const getCriticalDependencyWarning = memoize(() =>
|
---|
28 | require("./CriticalDependencyWarning")
|
---|
29 | );
|
---|
30 |
|
---|
31 | class ImportMetaPlugin {
|
---|
32 | /**
|
---|
33 | * @param {Compiler} compiler compiler
|
---|
34 | */
|
---|
35 | apply(compiler) {
|
---|
36 | compiler.hooks.compilation.tap(
|
---|
37 | "ImportMetaPlugin",
|
---|
38 | (compilation, { normalModuleFactory }) => {
|
---|
39 | /**
|
---|
40 | * @param {NormalModule} module module
|
---|
41 | * @returns {string} file url
|
---|
42 | */
|
---|
43 | const getUrl = module => {
|
---|
44 | return pathToFileURL(module.resource).toString();
|
---|
45 | };
|
---|
46 | /**
|
---|
47 | * @param {Parser} parser parser
|
---|
48 | * @param {Object} parserOptions parserOptions
|
---|
49 | * @returns {void}
|
---|
50 | */
|
---|
51 | const parserHandler = (parser, parserOptions) => {
|
---|
52 | /// import.meta direct ///
|
---|
53 | parser.hooks.typeof
|
---|
54 | .for("import.meta")
|
---|
55 | .tap(
|
---|
56 | "ImportMetaPlugin",
|
---|
57 | toConstantDependency(parser, JSON.stringify("object"))
|
---|
58 | );
|
---|
59 | parser.hooks.expression
|
---|
60 | .for("import.meta")
|
---|
61 | .tap("ImportMetaPlugin", metaProperty => {
|
---|
62 | const CriticalDependencyWarning = getCriticalDependencyWarning();
|
---|
63 | parser.state.module.addWarning(
|
---|
64 | new ModuleDependencyWarning(
|
---|
65 | parser.state.module,
|
---|
66 | new CriticalDependencyWarning(
|
---|
67 | "Accessing import.meta directly is unsupported (only property access is supported)"
|
---|
68 | ),
|
---|
69 | metaProperty.loc
|
---|
70 | )
|
---|
71 | );
|
---|
72 | const dep = new ConstDependency(
|
---|
73 | `${parser.isAsiPosition(metaProperty.range[0]) ? ";" : ""}({})`,
|
---|
74 | metaProperty.range
|
---|
75 | );
|
---|
76 | dep.loc = metaProperty.loc;
|
---|
77 | parser.state.module.addPresentationalDependency(dep);
|
---|
78 | return true;
|
---|
79 | });
|
---|
80 | parser.hooks.evaluateTypeof
|
---|
81 | .for("import.meta")
|
---|
82 | .tap("ImportMetaPlugin", evaluateToString("object"));
|
---|
83 | parser.hooks.evaluateIdentifier.for("import.meta").tap(
|
---|
84 | "ImportMetaPlugin",
|
---|
85 | evaluateToIdentifier("import.meta", "import.meta", () => [], true)
|
---|
86 | );
|
---|
87 |
|
---|
88 | /// import.meta.url ///
|
---|
89 | parser.hooks.typeof
|
---|
90 | .for("import.meta.url")
|
---|
91 | .tap(
|
---|
92 | "ImportMetaPlugin",
|
---|
93 | toConstantDependency(parser, JSON.stringify("string"))
|
---|
94 | );
|
---|
95 | parser.hooks.expression
|
---|
96 | .for("import.meta.url")
|
---|
97 | .tap("ImportMetaPlugin", expr => {
|
---|
98 | const dep = new ConstDependency(
|
---|
99 | JSON.stringify(getUrl(parser.state.module)),
|
---|
100 | expr.range
|
---|
101 | );
|
---|
102 | dep.loc = expr.loc;
|
---|
103 | parser.state.module.addPresentationalDependency(dep);
|
---|
104 | return true;
|
---|
105 | });
|
---|
106 | parser.hooks.evaluateTypeof
|
---|
107 | .for("import.meta.url")
|
---|
108 | .tap("ImportMetaPlugin", evaluateToString("string"));
|
---|
109 | parser.hooks.evaluateIdentifier
|
---|
110 | .for("import.meta.url")
|
---|
111 | .tap("ImportMetaPlugin", expr => {
|
---|
112 | return new BasicEvaluatedExpression()
|
---|
113 | .setString(getUrl(parser.state.module))
|
---|
114 | .setRange(expr.range);
|
---|
115 | });
|
---|
116 |
|
---|
117 | /// import.meta.webpack ///
|
---|
118 | const webpackVersion = parseInt(
|
---|
119 | require("../../package.json").version,
|
---|
120 | 10
|
---|
121 | );
|
---|
122 | parser.hooks.typeof
|
---|
123 | .for("import.meta.webpack")
|
---|
124 | .tap(
|
---|
125 | "ImportMetaPlugin",
|
---|
126 | toConstantDependency(parser, JSON.stringify("number"))
|
---|
127 | );
|
---|
128 | parser.hooks.expression
|
---|
129 | .for("import.meta.webpack")
|
---|
130 | .tap(
|
---|
131 | "ImportMetaPlugin",
|
---|
132 | toConstantDependency(parser, JSON.stringify(webpackVersion))
|
---|
133 | );
|
---|
134 | parser.hooks.evaluateTypeof
|
---|
135 | .for("import.meta.webpack")
|
---|
136 | .tap("ImportMetaPlugin", evaluateToString("number"));
|
---|
137 | parser.hooks.evaluateIdentifier
|
---|
138 | .for("import.meta.webpack")
|
---|
139 | .tap("ImportMetaPlugin", evaluateToNumber(webpackVersion));
|
---|
140 |
|
---|
141 | /// Unknown properties ///
|
---|
142 | parser.hooks.unhandledExpressionMemberChain
|
---|
143 | .for("import.meta")
|
---|
144 | .tap("ImportMetaPlugin", (expr, members) => {
|
---|
145 | const dep = new ConstDependency(
|
---|
146 | `${Template.toNormalComment(
|
---|
147 | "unsupported import.meta." + members.join(".")
|
---|
148 | )} undefined${propertyAccess(members, 1)}`,
|
---|
149 | expr.range
|
---|
150 | );
|
---|
151 | dep.loc = expr.loc;
|
---|
152 | parser.state.module.addPresentationalDependency(dep);
|
---|
153 | return true;
|
---|
154 | });
|
---|
155 | parser.hooks.evaluate
|
---|
156 | .for("MemberExpression")
|
---|
157 | .tap("ImportMetaPlugin", expression => {
|
---|
158 | const expr = /** @type {MemberExpression} */ (expression);
|
---|
159 | if (
|
---|
160 | expr.object.type === "MetaProperty" &&
|
---|
161 | expr.object.meta.name === "import" &&
|
---|
162 | expr.object.property.name === "meta" &&
|
---|
163 | expr.property.type ===
|
---|
164 | (expr.computed ? "Literal" : "Identifier")
|
---|
165 | ) {
|
---|
166 | return new BasicEvaluatedExpression()
|
---|
167 | .setUndefined()
|
---|
168 | .setRange(expr.range);
|
---|
169 | }
|
---|
170 | });
|
---|
171 | };
|
---|
172 |
|
---|
173 | normalModuleFactory.hooks.parser
|
---|
174 | .for("javascript/auto")
|
---|
175 | .tap("ImportMetaPlugin", parserHandler);
|
---|
176 | normalModuleFactory.hooks.parser
|
---|
177 | .for("javascript/esm")
|
---|
178 | .tap("ImportMetaPlugin", parserHandler);
|
---|
179 | }
|
---|
180 | );
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | module.exports = ImportMetaPlugin;
|
---|