11// the ESM import gives this entry a concatenation; each require() below decides
22// whether its target is absorbed into it
33import { tag } from "./member" ;
4+ // also a branch of the computed requests below, so it sits in the scope
5+ import importedBranch from "./imported-branch" ;
46
57function getLazy ( ) {
68 return require ( "./lazy" ) . v ;
1517 tryV = "failed" ;
1618}
1719const argM = require ( global . __notSet ? "./arg-a" : "./arg-b" ) ;
20+ const importedBranchM = require (
21+ global . __notSet ? "./imported-branch" : "./arg-b"
22+ ) ;
23+ const importedBranchTakenM = require (
24+ global . __notSet ? "./arg-b" : "./imported-branch"
25+ ) ;
1826
1927require ( "./written-direct" ) . p = "set" ;
2028const writtenDirect = require ( "./written-direct" ) ;
@@ -26,7 +34,12 @@ const resolveId = require.resolve("./resolve-target");
2634const ORDER_AT_LOAD = global . __bailoutOrder . slice ( ) ;
2735
2836// no single target for a computed request; require.resolve() cannot concatenate
29- const BAILED_OUT = [ "./arg-a.js" , "./arg-b.js" , "./resolve-target.js" ] ;
37+ const BAILED_OUT = [
38+ "./arg-a.js" ,
39+ "./arg-b.js" ,
40+ "./imported-branch.js" ,
41+ "./resolve-target.js"
42+ ] ;
3043// absorbed targets render as wrapped members, so they evaluate at their
3144// require() instead of at their position in the concatenation
3245const ABSORBED = [
@@ -62,6 +75,12 @@ it("should resolve a require() with a computed request at runtime", () => {
6275 expect ( ORDER_AT_LOAD ) . not . toContain ( "arg-a" ) ;
6376} ) ;
6477
78+ it ( "should keep a computed request working when a branch is also imported" , ( ) => {
79+ expect ( importedBranch . v ) . toBe ( "imported-branch" ) ;
80+ expect ( importedBranchM . v ) . toBe ( "arg-b" ) ;
81+ expect ( importedBranchTakenM . v ) . toBe ( "imported-branch" ) ;
82+ } ) ;
83+
6584it ( "should keep a member-assigned require() target writable" , ( ) => {
6685 expect ( writtenDirect . p ) . toBe ( "set" ) ;
6786 expect ( writtenDirect . v ) . toBe ( "written-direct" ) ;
@@ -91,6 +110,34 @@ it("should absorb every require() target that can be wrapped", () => {
91110 expect ( concatModules [ 0 ] . modules . map ( ( m ) => m . name ) . sort ( ) ) . toEqual ( ABSORBED ) ;
92111} ) ;
93112
113+ // an async callback moves the require() into a child block, which renders
114+ // after the module's own `require` header
115+ it ( "should read a whole module object required in a require.ensure callback" , ( ) => {
116+ expect ( ORDER_AT_LOAD ) . not . toContain ( "ensure-target" ) ;
117+ return new Promise ( ( resolve ) => {
118+ require . ensure (
119+ [ ] ,
120+ ( require ) => {
121+ resolve ( require ( "./ensure-target" ) ) ;
122+ } ,
123+ "ensure"
124+ ) ;
125+ } ) . then ( ( whole ) => {
126+ expect ( whole . NAME ) . toBe ( "ensure-target" ) ;
127+ expect ( whole . default ) . toEqual ( { isDefault : true } ) ;
128+ expect ( global . __bailoutOrder ) . toContain ( "ensure-target" ) ;
129+ } ) ;
130+ } ) ;
131+
132+ it ( "should read a whole module object required in an AMD require callback" , ( ) =>
133+ new Promise ( ( resolve ) => {
134+ require ( [ "./amd-target" ] , function ( ) {
135+ resolve ( require ( "./amd-target" ) ) ;
136+ } ) ;
137+ } ) . then ( ( whole ) => {
138+ expect ( whole . v ) . toBe ( "amd-target" ) ;
139+ } ) ) ;
140+
94141it ( "should evaluate the absorbed targets at their require(), in source order" , ( ) => {
95142 // a bailed-out target is reached through a lazy accessor too, so it keeps its
96143 // source position instead of being hoisted above the concatenation
0 commit comments