@@ -172,6 +172,52 @@ class ReplaceRequire extends Plugin {
172
172
updateFileWithTransform ( this , 'ember/index.js' , function ( babel : typeof Babel ) {
173
173
const { types : t } = babel ;
174
174
175
+ function createLoader ( ) {
176
+ return t . objectExpression ( [
177
+ t . objectMethod (
178
+ 'get' ,
179
+ t . identifier ( 'require' ) ,
180
+ [ ] ,
181
+ t . blockStatement ( [
182
+ t . returnStatement ( t . memberExpression ( t . identifier ( 'globalThis' ) , t . identifier ( 'require' ) ) ) ,
183
+ ] )
184
+ ) ,
185
+ t . objectMethod (
186
+ 'get' ,
187
+ t . identifier ( 'define' ) ,
188
+ [ ] ,
189
+ t . blockStatement ( [
190
+ t . returnStatement ( t . memberExpression ( t . identifier ( 'globalThis' ) , t . identifier ( 'define' ) ) ) ,
191
+ ] )
192
+ ) ,
193
+ t . objectMethod (
194
+ 'get' ,
195
+ t . identifier ( 'registry' ) ,
196
+ [ ] ,
197
+
198
+ t . blockStatement ( [
199
+ t . returnStatement (
200
+ t . logicalExpression (
201
+ '??' ,
202
+ t . optionalMemberExpression (
203
+ t . memberExpression ( t . identifier ( 'globalThis' ) , t . identifier ( 'requirejs' ) ) ,
204
+ t . identifier ( 'entries' ) ,
205
+ false ,
206
+ true
207
+ ) ,
208
+ t . optionalMemberExpression (
209
+ t . memberExpression ( t . identifier ( 'globalThis' ) , t . identifier ( 'require' ) ) ,
210
+ t . identifier ( 'entries' ) ,
211
+ false ,
212
+ true
213
+ )
214
+ )
215
+ ) ,
216
+ ] )
217
+ ) ,
218
+ ] ) ;
219
+ }
220
+
175
221
return {
176
222
visitor : {
177
223
CallExpression ( path : NodePath < Babel . types . CallExpression > ) {
@@ -194,6 +240,19 @@ class ReplaceRequire extends Plugin {
194
240
) ;
195
241
}
196
242
} ,
243
+ VariableDeclaration ( path : NodePath < Babel . types . VariableDeclaration > ) {
244
+ if (
245
+ path . node . declarations [ 0 ] . id . type === 'Identifier' &&
246
+ path . node . declarations [ 0 ] . id . name === 'PartialEmber' &&
247
+ path . node . declarations [ 0 ] . init ! . type === 'ObjectExpression'
248
+ ) {
249
+ const declaration = path . node . declarations [ 0 ] ;
250
+ const loader = ( declaration . init ! as Babel . types . ObjectExpression ) . properties . find (
251
+ p => ( p . type === 'ObjectProperty' && p . key . type === 'Identifier' && p . key . name ) === '__loader'
252
+ ) ;
253
+ ( loader as Babel . types . ObjectProperty ) . value = createLoader ( ) ;
254
+ }
255
+ } ,
197
256
AssignmentExpression ( path : NodePath < Babel . types . AssignmentExpression > ) {
198
257
if (
199
258
path . node . left . type === 'MemberExpression' &&
@@ -202,49 +261,7 @@ class ReplaceRequire extends Plugin {
202
261
path . node . left . property . type === 'Identifier' &&
203
262
path . node . left . property . name === '__loader'
204
263
) {
205
- path . node . right = t . objectExpression ( [
206
- t . objectMethod (
207
- 'get' ,
208
- t . identifier ( 'require' ) ,
209
- [ ] ,
210
- t . blockStatement ( [
211
- t . returnStatement ( t . memberExpression ( t . identifier ( 'globalThis' ) , t . identifier ( 'require' ) ) ) ,
212
- ] )
213
- ) ,
214
- t . objectMethod (
215
- 'get' ,
216
- t . identifier ( 'define' ) ,
217
- [ ] ,
218
- t . blockStatement ( [
219
- t . returnStatement ( t . memberExpression ( t . identifier ( 'globalThis' ) , t . identifier ( 'define' ) ) ) ,
220
- ] )
221
- ) ,
222
- t . objectMethod (
223
- 'get' ,
224
- t . identifier ( 'registry' ) ,
225
- [ ] ,
226
-
227
- t . blockStatement ( [
228
- t . returnStatement (
229
- t . logicalExpression (
230
- '??' ,
231
- t . optionalMemberExpression (
232
- t . memberExpression ( t . identifier ( 'globalThis' ) , t . identifier ( 'requirejs' ) ) ,
233
- t . identifier ( 'entries' ) ,
234
- false ,
235
- true
236
- ) ,
237
- t . optionalMemberExpression (
238
- t . memberExpression ( t . identifier ( 'globalThis' ) , t . identifier ( 'require' ) ) ,
239
- t . identifier ( 'entries' ) ,
240
- false ,
241
- true
242
- )
243
- )
244
- ) ,
245
- ] )
246
- ) ,
247
- ] ) ;
264
+ path . node . right = createLoader ( ) ;
248
265
}
249
266
} ,
250
267
} ,
0 commit comments