13
13
'use strict' ;
14
14
15
15
const babelRegisterOnly = require ( 'metro-babel-register' ) ;
16
- const nullthrows = require ( 'nullthrows' ) ;
17
- const createCacheKeyFunction = require ( '@jest/create-cache-key-function' )
18
- . default ;
19
- const t = require ( '@babel/types' ) ;
20
- const { statements} = require ( '@babel/template' ) . default ;
16
+ const createCacheKeyFunction =
17
+ require ( '@jest/create-cache-key-function' ) . default ;
21
18
22
- const importDefault = '__importDefault__' ;
23
- const importAll = '__importAll__' ;
24
-
25
- // prelude
26
- const importPrelude = statements ( `
27
- function ${ importDefault } (moduleId) {
28
- const exports = require(moduleId);
29
-
30
- if (exports && exports.__esModule) {
31
- return exports.default;
32
- }
33
-
34
- return exports;
35
- };
36
-
37
- function ${ importAll } (moduleId) {
38
- const exports = require(moduleId);
39
-
40
- if (exports && exports.__esModule) {
41
- return exports;
42
- }
43
-
44
- return Object.assign({}, exports, {default: exports});
45
- };
46
- ` ) ;
47
-
48
- const {
49
- transformSync : babelTransformSync ,
50
- transformFromAstSync : babelTransformFromAstSync ,
51
- } = require ( '@babel/core' ) ;
19
+ const { transformSync : babelTransformSync } = require ( '@babel/core' ) ;
52
20
const generate = require ( '@babel/generator' ) . default ;
53
21
54
22
const nodeFiles = new RegExp (
@@ -73,13 +41,13 @@ module.exports = {
73
41
} ) . code ;
74
42
}
75
43
76
- let { ast} = transformer . transform ( {
44
+ const { ast} = transformer . transform ( {
77
45
filename : file ,
78
46
options : {
79
47
ast : true , // needed for open source (?) https://github.com/facebook/react-native/commit/f8d6b97140cffe8d18b2558f94570c8d1b410d5c#r28647044
80
48
dev : true ,
81
49
enableBabelRuntime : false ,
82
- experimentalImportSupport : true ,
50
+ experimentalImportSupport : false ,
83
51
globalPrefix : '' ,
84
52
hot : false ,
85
53
inlineRequires : true ,
@@ -111,6 +79,10 @@ module.exports = {
111
79
[ require ( '@babel/plugin-transform-regenerator' ) ] ,
112
80
[ require ( '@babel/plugin-transform-sticky-regex' ) ] ,
113
81
[ require ( '@babel/plugin-transform-unicode-regex' ) ] ,
82
+ [
83
+ require ( '@babel/plugin-transform-modules-commonjs' ) ,
84
+ { strict : false , allowTopLevelThis : true } ,
85
+ ] ,
114
86
[ require ( '@babel/plugin-transform-classes' ) ] ,
115
87
[ require ( '@babel/plugin-transform-arrow-functions' ) ] ,
116
88
[ require ( '@babel/plugin-transform-spread' ) ] ,
@@ -127,46 +99,6 @@ module.exports = {
127
99
] ,
128
100
} ) ;
129
101
130
- // We're not using @babel/plugin-transform-modules-commonjs so
131
- // we need to add 'use strict' manually
132
- const directives = ast . program . directives ;
133
-
134
- if (
135
- ast . program . sourceType === 'module' &&
136
- ( directives == null ||
137
- directives . findIndex ( d => d . value . value === 'use strict' ) === - 1 )
138
- ) {
139
- ast . program . directives = [
140
- ...( directives || [ ] ) ,
141
- t . directive ( t . directiveLiteral ( 'use strict' ) ) ,
142
- ] ;
143
- }
144
-
145
- // Postprocess the transformed module to handle ESM and inline requires.
146
- // We need to do this in a separate pass to avoid issues tracking references.
147
- const babelTransformResult = babelTransformFromAstSync ( ast , src , {
148
- ast : true ,
149
- retainLines : true ,
150
- plugins : [
151
- [
152
- require ( 'metro-transform-plugins' ) . importExportPlugin ,
153
- { importDefault, importAll} ,
154
- ] ,
155
- [
156
- require ( 'babel-preset-fbjs/plugins/inline-requires.js' ) ,
157
- { inlineableCalls : [ importDefault , importAll ] } ,
158
- ] ,
159
- ] ,
160
- sourceType : 'module' ,
161
- } ) ;
162
-
163
- ast = nullthrows ( babelTransformResult . ast ) ;
164
-
165
- // Inject import helpers *after* running the inline-requires transform,
166
- // because otherwise it will assume they are user code and bail out of
167
- // inlining calls to them.
168
- ast . program . body . unshift ( ...importPrelude ( ) ) ;
169
-
170
102
return generate (
171
103
ast ,
172
104
// $FlowFixMe[prop-missing] Error found when improving flow typing for libs
0 commit comments