@@ -31,6 +31,7 @@ const createHash = require("./util/createHash");
3131/** @typedef {import("./RuntimeTemplate") } RuntimeTemplate */
3232/** @typedef {import("./javascript/JavascriptParser") } JavascriptParser */
3333/** @typedef {import("./javascript/JavascriptParser").DestructuringAssignmentProperties } DestructuringAssignmentProperties */
34+ /** @typedef {import("./javascript/JavascriptParser").Members } Members */
3435/** @typedef {import("./javascript/JavascriptParser").Range } Range */
3536/** @typedef {import("./logging/Logger").Logger } Logger */
3637/** @typedef {import("./Compilation") } Compilation */
@@ -140,6 +141,17 @@ function getObjKeys(properties) {
140141 return new Set ( [ ...properties ] . map ( ( p ) => p . id ) ) ;
141142}
142143
144+ /**
145+ * Whether a value is a nested definition (plain object/array) to recurse into.
146+ * @param {CodeValue } code code value
147+ * @returns {code is Definitions } true for a plain object or array
148+ */
149+ const isObjectDefinition = ( code ) =>
150+ Boolean ( code ) &&
151+ typeof code === "object" &&
152+ ! ( code instanceof RuntimeValue ) &&
153+ ! ( code instanceof RegExp ) ;
154+
143155/** @typedef {Set<string> | null } ObjKeys */
144156/** @typedef {boolean | undefined | null } AsiSafe */
145157
@@ -487,12 +499,7 @@ class DefinePlugin {
487499 const walkDefinitions = ( definitions , prefix ) => {
488500 for ( const key of Object . keys ( definitions ) ) {
489501 const code = definitions [ key ] ;
490- if (
491- code &&
492- typeof code === "object" &&
493- ! ( code instanceof RuntimeValue ) &&
494- ! ( code instanceof RegExp )
495- ) {
502+ if ( isObjectDefinition ( code ) ) {
496503 walkDefinitions (
497504 /** @type {Definitions } */ ( code ) ,
498505 `${ prefix + key } .`
@@ -774,6 +781,77 @@ class DefinePlugin {
774781 }
775782 return toConstantDependency ( parser , strCode ) ( expr ) ;
776783 } ) ;
784+ // A property access not defined on the object resolves to `undefined`
785+ // and the whole object is never inlined (issue #15559). Keyed by the
786+ // chain root so dotted object keys (e.g. `a.b`) are also covered.
787+ const chainParts = key . split ( "." ) ;
788+ // `import.meta` is one chain root (a MetaProperty), not two members.
789+ const isMeta =
790+ chainParts [ 0 ] === "import" && chainParts [ 1 ] === "meta" ;
791+ const chainRoot = isMeta ? "import.meta" : chainParts [ 0 ] ;
792+ const chainPrefix = chainParts . slice ( isMeta ? 2 : 1 ) ;
793+ /**
794+ * Whether a member chain reads a property that is not defined.
795+ * Collects every define key consulted so the caller can record a
796+ * value dependency on each (a sibling key like `OBJECT.SUB2` affects
797+ * the result even though `OBJECT` registered the handler).
798+ * `member in value` keeps inherited members (e.g. `toString`) defined.
799+ * @param {Members } members chain members (after the root)
800+ * @param {string[] } deps consulted define keys (mutated)
801+ * @returns {boolean } true when the access resolves to `undefined`
802+ */
803+ const isUndefinedMemberAccess = ( members , deps ) => {
804+ if ( members . length <= chainPrefix . length ) return false ;
805+ for ( let i = 0 ; i < chainPrefix . length ; i ++ ) {
806+ if ( members [ i ] !== chainPrefix [ i ] ) return false ;
807+ }
808+ /** @type {CodeValue } */
809+ let value = /** @type {CodeValue } */ ( obj ) ;
810+ let path = key ;
811+ for ( let i = chainPrefix . length ; i < members . length ; i ++ ) {
812+ // a leaf with members left is a real property access on a value
813+ if ( ! isObjectDefinition ( value ) ) return false ;
814+ const member = members [ i ] ;
815+ const nextPath = `${ path } .${ member } ` ;
816+ if ( member in value ) {
817+ value = value [ member ] ;
818+ } else if (
819+ Object . prototype . hasOwnProperty . call ( definitions , nextPath )
820+ ) {
821+ // defined via a dotted sibling key, e.g. `OBJECT.SUB2`
822+ deps . push ( nextPath ) ;
823+ value = definitions [ nextPath ] ;
824+ } else {
825+ return true ;
826+ }
827+ path = nextPath ;
828+ }
829+ return false ;
830+ } ;
831+ parser . hooks . expressionMemberChain
832+ . for ( chainRoot )
833+ . tap ( PLUGIN_NAME , ( expr , members ) => {
834+ const deps = [ key ] ;
835+ if ( ! isUndefinedMemberAccess ( members , deps ) ) return ;
836+ for ( const dep of deps ) addValueDependency ( dep ) ;
837+ return toConstantDependency ( parser , "undefined" ) ( expr ) ;
838+ } ) ;
839+ // In a call, replace only the callee so the call itself is kept:
840+ // `x.MISSING()` stays a (throwing) call and `x.MISSING?.()`
841+ // short-circuits, with the object never inlined.
842+ parser . hooks . callMemberChain
843+ . for ( chainRoot )
844+ . tap ( PLUGIN_NAME , ( expr , members ) => {
845+ const deps = [ key ] ;
846+ if ( ! isUndefinedMemberAccess ( members , deps ) ) return ;
847+ for ( const dep of deps ) addValueDependency ( dep ) ;
848+ toConstantDependency (
849+ parser ,
850+ "undefined"
851+ ) ( /** @type {Expression } */ ( expr . callee ) ) ;
852+ parser . walkExpressions ( expr . arguments ) ;
853+ return true ;
854+ } ) ;
777855 parser . hooks . typeof
778856 . for ( key )
779857 . tap (
@@ -821,12 +899,7 @@ class DefinePlugin {
821899 warning . hideStack = true ;
822900 compilation . warnings . push ( warning ) ;
823901 }
824- if (
825- code &&
826- typeof code === "object" &&
827- ! ( code instanceof RuntimeValue ) &&
828- ! ( code instanceof RegExp )
829- ) {
902+ if ( isObjectDefinition ( code ) ) {
830903 walkDefinitionsForValues (
831904 /** @type {Definitions } */ ( code ) ,
832905 `${ prefix + key } .`
0 commit comments