[79a0317] | 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 {
|
---|
| 11 | JAVASCRIPT_MODULE_TYPE_AUTO,
|
---|
| 12 | JAVASCRIPT_MODULE_TYPE_ESM
|
---|
| 13 | } = require("../ModuleTypeConstants");
|
---|
| 14 | const Template = require("../Template");
|
---|
| 15 | const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
|
---|
| 16 | const {
|
---|
| 17 | evaluateToIdentifier,
|
---|
| 18 | toConstantDependency,
|
---|
| 19 | evaluateToString,
|
---|
| 20 | evaluateToNumber
|
---|
| 21 | } = require("../javascript/JavascriptParserHelpers");
|
---|
| 22 | const memoize = require("../util/memoize");
|
---|
| 23 | const propertyAccess = require("../util/propertyAccess");
|
---|
| 24 | const ConstDependency = require("./ConstDependency");
|
---|
| 25 |
|
---|
| 26 | /** @typedef {import("estree").MemberExpression} MemberExpression */
|
---|
| 27 | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
|
---|
| 28 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 29 | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
|
---|
| 30 | /** @typedef {import("../NormalModule")} NormalModule */
|
---|
| 31 | /** @typedef {import("../javascript/JavascriptParser")} Parser */
|
---|
| 32 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
| 33 |
|
---|
| 34 | const getCriticalDependencyWarning = memoize(() =>
|
---|
| 35 | require("./CriticalDependencyWarning")
|
---|
| 36 | );
|
---|
| 37 |
|
---|
| 38 | const PLUGIN_NAME = "ImportMetaPlugin";
|
---|
| 39 |
|
---|
| 40 | class ImportMetaPlugin {
|
---|
| 41 | /**
|
---|
| 42 | * @param {Compiler} compiler compiler
|
---|
| 43 | */
|
---|
| 44 | apply(compiler) {
|
---|
| 45 | compiler.hooks.compilation.tap(
|
---|
| 46 | PLUGIN_NAME,
|
---|
| 47 | (compilation, { normalModuleFactory }) => {
|
---|
| 48 | /**
|
---|
| 49 | * @param {NormalModule} module module
|
---|
| 50 | * @returns {string} file url
|
---|
| 51 | */
|
---|
| 52 | const getUrl = module => pathToFileURL(module.resource).toString();
|
---|
| 53 | /**
|
---|
| 54 | * @param {Parser} parser parser parser
|
---|
| 55 | * @param {JavascriptParserOptions} parserOptions parserOptions
|
---|
| 56 | * @returns {void}
|
---|
| 57 | */
|
---|
| 58 | const parserHandler = (parser, { importMeta }) => {
|
---|
| 59 | if (importMeta === false) {
|
---|
| 60 | const { importMetaName } = compilation.outputOptions;
|
---|
| 61 | if (importMetaName === "import.meta") return;
|
---|
| 62 |
|
---|
| 63 | parser.hooks.expression
|
---|
| 64 | .for("import.meta")
|
---|
| 65 | .tap(PLUGIN_NAME, metaProperty => {
|
---|
| 66 | const dep = new ConstDependency(
|
---|
| 67 | /** @type {string} */ (importMetaName),
|
---|
| 68 | /** @type {Range} */ (metaProperty.range)
|
---|
| 69 | );
|
---|
| 70 | dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
|
---|
| 71 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 72 | return true;
|
---|
| 73 | });
|
---|
| 74 | return;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | // import.meta direct
|
---|
| 78 | const webpackVersion = Number.parseInt(
|
---|
| 79 | require("../../package.json").version,
|
---|
| 80 | 10
|
---|
| 81 | );
|
---|
| 82 | const importMetaUrl = () =>
|
---|
| 83 | JSON.stringify(getUrl(parser.state.module));
|
---|
| 84 | const importMetaWebpackVersion = () => JSON.stringify(webpackVersion);
|
---|
| 85 | /**
|
---|
| 86 | * @param {string[]} members members
|
---|
| 87 | * @returns {string} error message
|
---|
| 88 | */
|
---|
| 89 | const importMetaUnknownProperty = members =>
|
---|
| 90 | `${Template.toNormalComment(
|
---|
| 91 | `unsupported import.meta.${members.join(".")}`
|
---|
| 92 | )} undefined${propertyAccess(members, 1)}`;
|
---|
| 93 | parser.hooks.typeof
|
---|
| 94 | .for("import.meta")
|
---|
| 95 | .tap(
|
---|
| 96 | PLUGIN_NAME,
|
---|
| 97 | toConstantDependency(parser, JSON.stringify("object"))
|
---|
| 98 | );
|
---|
| 99 | parser.hooks.expression
|
---|
| 100 | .for("import.meta")
|
---|
| 101 | .tap(PLUGIN_NAME, metaProperty => {
|
---|
| 102 | const referencedPropertiesInDestructuring =
|
---|
| 103 | parser.destructuringAssignmentPropertiesFor(metaProperty);
|
---|
| 104 | if (!referencedPropertiesInDestructuring) {
|
---|
| 105 | const CriticalDependencyWarning =
|
---|
| 106 | getCriticalDependencyWarning();
|
---|
| 107 | parser.state.module.addWarning(
|
---|
| 108 | new ModuleDependencyWarning(
|
---|
| 109 | parser.state.module,
|
---|
| 110 | new CriticalDependencyWarning(
|
---|
| 111 | "Accessing import.meta directly is unsupported (only property access or destructuring is supported)"
|
---|
| 112 | ),
|
---|
| 113 | /** @type {DependencyLocation} */ (metaProperty.loc)
|
---|
| 114 | )
|
---|
| 115 | );
|
---|
| 116 | const dep = new ConstDependency(
|
---|
| 117 | `${
|
---|
| 118 | parser.isAsiPosition(
|
---|
| 119 | /** @type {Range} */ (metaProperty.range)[0]
|
---|
| 120 | )
|
---|
| 121 | ? ";"
|
---|
| 122 | : ""
|
---|
| 123 | }({})`,
|
---|
| 124 | /** @type {Range} */ (metaProperty.range)
|
---|
| 125 | );
|
---|
| 126 | dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
|
---|
| 127 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 128 | return true;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | let str = "";
|
---|
| 132 | for (const { id: prop } of referencedPropertiesInDestructuring) {
|
---|
| 133 | switch (prop) {
|
---|
| 134 | case "url":
|
---|
| 135 | str += `url: ${importMetaUrl()},`;
|
---|
| 136 | break;
|
---|
| 137 | case "webpack":
|
---|
| 138 | str += `webpack: ${importMetaWebpackVersion()},`;
|
---|
| 139 | break;
|
---|
| 140 | default:
|
---|
| 141 | str += `[${JSON.stringify(
|
---|
| 142 | prop
|
---|
| 143 | )}]: ${importMetaUnknownProperty([prop])},`;
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | const dep = new ConstDependency(
|
---|
| 148 | `({${str}})`,
|
---|
| 149 | /** @type {Range} */ (metaProperty.range)
|
---|
| 150 | );
|
---|
| 151 | dep.loc = /** @type {DependencyLocation} */ (metaProperty.loc);
|
---|
| 152 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 153 | return true;
|
---|
| 154 | });
|
---|
| 155 | parser.hooks.evaluateTypeof
|
---|
| 156 | .for("import.meta")
|
---|
| 157 | .tap(PLUGIN_NAME, evaluateToString("object"));
|
---|
| 158 | parser.hooks.evaluateIdentifier.for("import.meta").tap(
|
---|
| 159 | PLUGIN_NAME,
|
---|
| 160 | evaluateToIdentifier("import.meta", "import.meta", () => [], true)
|
---|
| 161 | );
|
---|
| 162 |
|
---|
| 163 | // import.meta.url
|
---|
| 164 | parser.hooks.typeof
|
---|
| 165 | .for("import.meta.url")
|
---|
| 166 | .tap(
|
---|
| 167 | PLUGIN_NAME,
|
---|
| 168 | toConstantDependency(parser, JSON.stringify("string"))
|
---|
| 169 | );
|
---|
| 170 | parser.hooks.expression
|
---|
| 171 | .for("import.meta.url")
|
---|
| 172 | .tap(PLUGIN_NAME, expr => {
|
---|
| 173 | const dep = new ConstDependency(
|
---|
| 174 | importMetaUrl(),
|
---|
| 175 | /** @type {Range} */ (expr.range)
|
---|
| 176 | );
|
---|
| 177 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
| 178 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 179 | return true;
|
---|
| 180 | });
|
---|
| 181 | parser.hooks.evaluateTypeof
|
---|
| 182 | .for("import.meta.url")
|
---|
| 183 | .tap(PLUGIN_NAME, evaluateToString("string"));
|
---|
| 184 | parser.hooks.evaluateIdentifier
|
---|
| 185 | .for("import.meta.url")
|
---|
| 186 | .tap(PLUGIN_NAME, expr =>
|
---|
| 187 | new BasicEvaluatedExpression()
|
---|
| 188 | .setString(getUrl(parser.state.module))
|
---|
| 189 | .setRange(/** @type {Range} */ (expr.range))
|
---|
| 190 | );
|
---|
| 191 |
|
---|
| 192 | // import.meta.webpack
|
---|
| 193 | parser.hooks.typeof
|
---|
| 194 | .for("import.meta.webpack")
|
---|
| 195 | .tap(
|
---|
| 196 | PLUGIN_NAME,
|
---|
| 197 | toConstantDependency(parser, JSON.stringify("number"))
|
---|
| 198 | );
|
---|
| 199 | parser.hooks.expression
|
---|
| 200 | .for("import.meta.webpack")
|
---|
| 201 | .tap(
|
---|
| 202 | PLUGIN_NAME,
|
---|
| 203 | toConstantDependency(parser, importMetaWebpackVersion())
|
---|
| 204 | );
|
---|
| 205 | parser.hooks.evaluateTypeof
|
---|
| 206 | .for("import.meta.webpack")
|
---|
| 207 | .tap(PLUGIN_NAME, evaluateToString("number"));
|
---|
| 208 | parser.hooks.evaluateIdentifier
|
---|
| 209 | .for("import.meta.webpack")
|
---|
| 210 | .tap(PLUGIN_NAME, evaluateToNumber(webpackVersion));
|
---|
| 211 |
|
---|
| 212 | // Unknown properties
|
---|
| 213 | parser.hooks.unhandledExpressionMemberChain
|
---|
| 214 | .for("import.meta")
|
---|
| 215 | .tap(PLUGIN_NAME, (expr, members) => {
|
---|
| 216 | const dep = new ConstDependency(
|
---|
| 217 | importMetaUnknownProperty(members),
|
---|
| 218 | /** @type {Range} */ (expr.range)
|
---|
| 219 | );
|
---|
| 220 | dep.loc = /** @type {DependencyLocation} */ (expr.loc);
|
---|
| 221 | parser.state.module.addPresentationalDependency(dep);
|
---|
| 222 | return true;
|
---|
| 223 | });
|
---|
| 224 | parser.hooks.evaluate
|
---|
| 225 | .for("MemberExpression")
|
---|
| 226 | .tap(PLUGIN_NAME, expression => {
|
---|
| 227 | const expr = /** @type {MemberExpression} */ (expression);
|
---|
| 228 | if (
|
---|
| 229 | expr.object.type === "MetaProperty" &&
|
---|
| 230 | expr.object.meta.name === "import" &&
|
---|
| 231 | expr.object.property.name === "meta" &&
|
---|
| 232 | expr.property.type ===
|
---|
| 233 | (expr.computed ? "Literal" : "Identifier")
|
---|
| 234 | ) {
|
---|
| 235 | return new BasicEvaluatedExpression()
|
---|
| 236 | .setUndefined()
|
---|
| 237 | .setRange(/** @type {Range} */ (expr.range));
|
---|
| 238 | }
|
---|
| 239 | });
|
---|
| 240 | };
|
---|
| 241 |
|
---|
| 242 | normalModuleFactory.hooks.parser
|
---|
| 243 | .for(JAVASCRIPT_MODULE_TYPE_AUTO)
|
---|
| 244 | .tap(PLUGIN_NAME, parserHandler);
|
---|
| 245 | normalModuleFactory.hooks.parser
|
---|
| 246 | .for(JAVASCRIPT_MODULE_TYPE_ESM)
|
---|
| 247 | .tap(PLUGIN_NAME, parserHandler);
|
---|
| 248 | }
|
---|
| 249 | );
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | module.exports = ImportMetaPlugin;
|
---|