@@ -13,6 +13,7 @@ const {
1313 JAVASCRIPT_MODULE_TYPE_AUTO ,
1414 JAVASCRIPT_MODULE_TYPE_ESM
1515} = require ( "../ModuleTypeConstants" ) ;
16+ const RuntimeGlobals = require ( "../RuntimeGlobals" ) ;
1617const Template = require ( "../Template" ) ;
1718const BasicEvaluatedExpression = require ( "../javascript/BasicEvaluatedExpression" ) ;
1819const {
@@ -34,6 +35,7 @@ const ConstDependency = require("./ConstDependency");
3435/** @typedef {import("../javascript/JavascriptParser").Range } Range */
3536/** @typedef {import("../javascript/JavascriptParser").Members } Members */
3637/** @typedef {import("../javascript/JavascriptParser").DestructuringAssignmentProperty } DestructuringAssignmentProperty */
38+ /** @typedef {import("./ConstDependency").RawRuntimeRequirements } RawRuntimeRequirements */
3739
3840const getCriticalDependencyWarning = memoize ( ( ) =>
3941 require ( "./CriticalDependencyWarning" )
@@ -168,6 +170,9 @@ class ImportMetaPlugin {
168170 return true ;
169171 }
170172
173+ /** @type {RawRuntimeRequirements } */
174+ const runtimeRequirements = [ ] ;
175+
171176 let str = "" ;
172177 for ( const prop of referencedPropertiesInDestructuring ) {
173178 const value = hooks . propertyInDestructuring . call ( prop ) ;
@@ -184,6 +189,14 @@ class ImportMetaPlugin {
184189 case "webpack" :
185190 str += `webpack: ${ importMetaWebpackVersion ( ) } ,` ;
186191 break ;
192+ case "main" :
193+ str += `main: ${ RuntimeGlobals . moduleCache } [${ RuntimeGlobals . entryModuleId } ] === ${ RuntimeGlobals . module } ,` ;
194+ runtimeRequirements . push (
195+ RuntimeGlobals . moduleCache ,
196+ RuntimeGlobals . entryModuleId ,
197+ RuntimeGlobals . module
198+ ) ;
199+ break ;
187200 default :
188201 str += `[${ JSON . stringify (
189202 prop . id
@@ -193,7 +206,8 @@ class ImportMetaPlugin {
193206 }
194207 const dep = new ConstDependency (
195208 `({${ str } })` ,
196- /** @type {Range } */ ( metaProperty . range )
209+ /** @type {Range } */ ( metaProperty . range ) ,
210+ runtimeRequirements
197211 ) ;
198212 dep . loc = /** @type {DependencyLocation } */ ( metaProperty . loc ) ;
199213 parser . state . module . addPresentationalDependency ( dep ) ;
@@ -237,17 +251,17 @@ class ImportMetaPlugin {
237251 ) ;
238252
239253 // import.meta.webpack
240- parser . hooks . typeof
254+ parser . hooks . expression
241255 . for ( "import.meta.webpack" )
242256 . tap (
243257 PLUGIN_NAME ,
244- toConstantDependency ( parser , JSON . stringify ( "number" ) )
258+ toConstantDependency ( parser , importMetaWebpackVersion ( ) )
245259 ) ;
246- parser . hooks . expression
260+ parser . hooks . typeof
247261 . for ( "import.meta.webpack" )
248262 . tap (
249263 PLUGIN_NAME ,
250- toConstantDependency ( parser , importMetaWebpackVersion ( ) )
264+ toConstantDependency ( parser , JSON . stringify ( "number" ) )
251265 ) ;
252266 parser . hooks . evaluateTypeof
253267 . for ( "import.meta.webpack" )
@@ -256,6 +270,30 @@ class ImportMetaPlugin {
256270 . for ( "import.meta.webpack" )
257271 . tap ( PLUGIN_NAME , evaluateToNumber ( webpackVersion ) ) ;
258272
273+ parser . hooks . expression
274+ . for ( "import.meta.main" )
275+ . tap (
276+ PLUGIN_NAME ,
277+ toConstantDependency (
278+ parser ,
279+ `${ RuntimeGlobals . moduleCache } [${ RuntimeGlobals . entryModuleId } ] === ${ RuntimeGlobals . module } ` ,
280+ [
281+ RuntimeGlobals . moduleCache ,
282+ RuntimeGlobals . entryModuleId ,
283+ RuntimeGlobals . module
284+ ]
285+ )
286+ ) ;
287+ parser . hooks . typeof
288+ . for ( "import.meta.main" )
289+ . tap (
290+ PLUGIN_NAME ,
291+ toConstantDependency ( parser , JSON . stringify ( "boolean" ) )
292+ ) ;
293+ parser . hooks . evaluateTypeof
294+ . for ( "import.meta.main" )
295+ . tap ( PLUGIN_NAME , evaluateToString ( "boolean" ) ) ;
296+
259297 // Unknown properties
260298 parser . hooks . unhandledExpressionMemberChain
261299 . for ( "import.meta" )
0 commit comments