Repository: webpack/webpack
File: lib/dependencies/ImportMetaPlugin.js (JavaScript)

Code before:
							const dep = new ConstDependency(
								stringify,
								/** @type {Range} */ (expr.range)
							);
							dep.loc = /** @type {DependencyLocation} */ (expr.loc);
							parser.state.module.addPresentationalDependency(dep);
							return true;
						});
					parser.hooks.evaluateTypeof
						.for("import.meta.env")
						.tap(PLUGIN_NAME, evaluateToString("object"));
					parser.hooks.evaluateIdentifier
						.for("import.meta.env")
						.tap(PLUGIN_NAME, (expr) =>
							new BasicEvaluatedExpression()
								.setTruthy()
								.setSideEffects(false)
								.setRange(/** @type {Range} */ (expr.range))
						);

					// Unknown properties
					parser.hooks.unhandledExpressionMemberChain.for("import.meta").tap(
						PLUGIN_NAME,
						// unknown import.meta properties should be determined at runtime
						() => true
					);
					parser.hooks.evaluate
						.for("MemberExpression")
						.tap(PLUGIN_NAME, (expression) => {
							const expr = /** @type {MemberExpression} */ (expression);
							if (
								expr.object.type === "MetaProperty" &&
								expr.object.meta.name === "import" &&
								expr.object.property.name === "meta" &&
								expr.property.type ===
									(expr.computed ? "Literal" : "Identifier")
							) {
								return new BasicEvaluatedExpression()
									.setUndefined()
									.setRange(/** @type {Range} */ (expr.range));
							}
						});
				};

				normalModuleFactory.hooks.parser
					.for(JAVASCRIPT_MODULE_TYPE_AUTO)
					.tap(PLUGIN_NAME, parserHandler);
				normalModuleFactory.hooks.parser
					.for(JAVASCRIPT_MODULE_TYPE_ESM)
					.tap(PLUGIN_NAME, parserHandler);
			}

Review comment (suggestion):
Looks good, but let's do it under the option, we have `D(parserOptions, "importMeta", true);` right now, but we need `importMeta: boolean | "preserve-unknown"`, let's set `preserve-unknown` when `output.module` is `true`, we still should replace it when `output.module` is `false`, otherwise code will not work

Code after:
									return true;
								}
							}
						});
					parser.hooks.expression
						.for("import.meta.env")
						.tap(PLUGIN_NAME, (expr) => {
							const { stringify } =
								collectImportMetaEnvDefinitions(compilation);

							const dep = new ConstDependency(
								stringify,
								/** @type {Range} */ (expr.range)
							);
							dep.loc = /** @type {DependencyLocation} */ (expr.loc);
							parser.state.module.addPresentationalDependency(dep);
							return true;
						});
					parser.hooks.evaluateTypeof
						.for("import.meta.env")
						.tap(PLUGIN_NAME, evaluateToString("object"));
					parser.hooks.evaluateIdentifier
						.for("import.meta.env")
						.tap(PLUGIN_NAME, (expr) =>
							new BasicEvaluatedExpression()
								.setTruthy()
								.setSideEffects(false)
								.setRange(/** @type {Range} */ (expr.range))
						);

					// Unknown properties
					parser.hooks.unhandledExpressionMemberChain
						.for("import.meta")
						.tap(PLUGIN_NAME, (expr, members) => {
							// unknown import.meta properties should be determined at runtime
							if (importMeta === "preserve-unknown") {
								return true;
							}

							// keep import.meta.env unknown property
							// don't evaluate import.meta.env.UNKNOWN_PROPERTY -> undefined.UNKNOWN_PROPERTY
							// `dirname` and `filename` logic in NodeStuffPlugin
							if (
								members[0] === "env" ||
								members[0] === "dirname" ||
								members[0] === "filename"
							) {
								return true;
							}
							const dep = new ConstDependency(
								importMetaUnknownProperty(members),