@@ -102,7 +102,9 @@ export default class extends V1Addon {
102
102
// top-level `ember` package tries to import it until 4.10. A
103
103
// spec-compliant ES modules implementation will treat this as a parse
104
104
// error.
105
- trees . push ( new FixStringLoc ( [ packages ] ) ) ;
105
+ trees . push ( new ReplaceRequire ( [ new FixStringLoc ( [ packages ] ) ] ) ) ;
106
+ } else if ( satisfies ( this . packageJSON . version , '<5.7.0' ) ) {
107
+ trees . push ( new ReplaceRequire ( [ packages ] ) ) ;
106
108
}
107
109
108
110
if ( satisfies ( this . packageJSON . version , '<5.12.0' ) ) {
@@ -165,6 +167,149 @@ class FixStringLoc extends Plugin {
165
167
}
166
168
}
167
169
170
+ class ReplaceRequire extends Plugin {
171
+ build ( ) {
172
+ updateFileWithTransform ( this , 'ember/index.js' , function ( babel : typeof Babel ) {
173
+ const { types : t } = babel ;
174
+
175
+ return {
176
+ visitor : {
177
+ CallExpression ( path : NodePath < Babel . types . CallExpression > ) {
178
+ if (
179
+ path . node . callee . type === 'Identifier' &&
180
+ ( path . node . callee . name === 'has' || path . node . callee . name === 'require' ) &&
181
+ path . node . arguments [ 0 ] . type === 'StringLiteral' &&
182
+ path . node . arguments [ 0 ] . value === 'ember-testing'
183
+ ) {
184
+ path . replaceWith ( t . identifier ( 'EmberTestingImpl' ) ) ;
185
+ }
186
+ } ,
187
+ ImportDeclaration ( path : NodePath < Babel . types . ImportDeclaration > ) {
188
+ if ( path . node . source . value === 'require' ) {
189
+ path . replaceWith (
190
+ t . importDeclaration (
191
+ [ t . importSpecifier ( t . identifier ( 'EmberTestingImpl' ) , t . identifier ( '_impl' ) ) ] ,
192
+ t . stringLiteral ( '@ember/test' )
193
+ )
194
+ ) ;
195
+ }
196
+ } ,
197
+ AssignmentExpression ( path : NodePath < Babel . types . AssignmentExpression > ) {
198
+ if (
199
+ path . node . left . type === 'MemberExpression' &&
200
+ path . node . left . object . type === 'Identifier' &&
201
+ path . node . left . object . name === 'Ember' &&
202
+ path . node . left . property . type === 'Identifier' &&
203
+ path . node . left . property . name === '__loader'
204
+ ) {
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
+ ] ) ;
248
+ }
249
+ } ,
250
+ } ,
251
+ } ;
252
+ } ) ;
253
+
254
+ replaceFile (
255
+ this ,
256
+ '@ember/test/index.js' ,
257
+ `export let registerAsyncHelper;
258
+ export let registerHelper;
259
+ export let registerWaiter;
260
+ export let unregisterHelper;
261
+ export let unregisterWaiter;
262
+ export let _impl;
263
+
264
+ let testingNotAvailableMessage = () => {
265
+ throw new Error('Attempted to use test utilities, but \`ember-testing\` was not included');
266
+ };
267
+
268
+ registerAsyncHelper = testingNotAvailableMessage;
269
+ registerHelper = testingNotAvailableMessage;
270
+ registerWaiter = testingNotAvailableMessage;
271
+ unregisterHelper = testingNotAvailableMessage;
272
+ unregisterWaiter = testingNotAvailableMessage;
273
+
274
+ export function registerTestImplementaiton(impl) {
275
+ let { Test } = impl;
276
+ registerAsyncHelper = Test.registerAsyncHelper;
277
+ registerHelper = Test.registerHelper;
278
+ registerWaiter = Test.registerWaiter;
279
+ unregisterHelper = Test.unregisterHelper;
280
+ unregisterWaiter = Test.unregisterWaiter;
281
+ _impl = impl;
282
+ }`
283
+ ) ;
284
+
285
+ replaceFile (
286
+ this ,
287
+ 'ember-testing/index.js' ,
288
+ `export * from './lib/public-api';
289
+ import * as EmberTesting from './lib/public-api';
290
+ import { registerTestImplementaiton } from '@ember/test';
291
+
292
+
293
+ registerTestImplementaiton(EmberTesting);`
294
+ ) ;
295
+
296
+ replaceFile (
297
+ this ,
298
+ 'ember-testing/lib/public-api.js' ,
299
+ `
300
+ export { default as Test } from './test';
301
+ export { default as Adapter } from './adapters/adapter';
302
+ export { default as setupForTesting } from './setup_for_testing';
303
+ export { default as QUnitAdapter } from './adapters/qunit';
304
+
305
+ import './ext/application';
306
+ import './ext/rsvp'; // setup RSVP + run loop integration
307
+ import './helpers'; // adds helpers to helpers object in Test
308
+ import './initializers'; // to setup initializer`
309
+ ) ;
310
+ }
311
+ }
312
+
168
313
class FixDeprecateFunction extends Plugin {
169
314
build ( ) {
170
315
let inSource = readFileSync ( resolve ( this . inputPaths [ 0 ] , '@ember' , 'debug' , 'index.js' ) , 'utf8' ) ;
@@ -252,6 +397,10 @@ function updateFileWithTransform(
252
397
outputFileSync ( resolve ( context . outputPath , file ) , outSource , 'utf8' ) ;
253
398
}
254
399
400
+ function replaceFile ( context : Plugin , file : string , content : string ) {
401
+ outputFileSync ( resolve ( context . outputPath , file ) , content , 'utf8' ) ;
402
+ }
403
+
255
404
class FixCycleImports extends Plugin {
256
405
build ( ) {
257
406
for ( let file of [ '@ember/object/observable.js' , '@ember/utils/lib/is_empty.js' ] ) {
0 commit comments