metro-transform-worker 0.66.2 → 0.67.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -11
- package/src/__mocks__/index.js +2 -2
- package/src/index.js +66 -64
- package/src/index.js.flow +40 -40
- package/src/utils/assetTransformer.js +6 -6
- package/src/utils/assetTransformer.js.flow +4 -5
- package/src/utils/getMinifier.js +1 -1
- package/src/utils/getMinifier.js.flow +1 -1
- package/src.real/__mocks__/index.js +2 -2
- package/src.real/__tests__/index-test.js +28 -5
- package/src.real/index.js +40 -40
- package/src.real/utils/assetTransformer.js +4 -5
- package/src.real/utils/getMinifier.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metro-transform-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.67.0",
|
|
4
4
|
"description": "🚇 Transform worker for Metro.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -18,19 +18,19 @@
|
|
|
18
18
|
"@babel/parser": "^7.14.0",
|
|
19
19
|
"@babel/types": "^7.0.0",
|
|
20
20
|
"babel-preset-fbjs": "^3.4.0",
|
|
21
|
-
"metro": "0.
|
|
22
|
-
"metro-babel-transformer": "0.
|
|
23
|
-
"metro-cache": "0.
|
|
24
|
-
"metro-cache-key": "0.
|
|
25
|
-
"metro-hermes-compiler": "0.
|
|
26
|
-
"metro-source-map": "0.
|
|
27
|
-
"metro-transform-plugins": "0.
|
|
21
|
+
"metro": "0.67.0",
|
|
22
|
+
"metro-babel-transformer": "0.67.0",
|
|
23
|
+
"metro-cache": "0.67.0",
|
|
24
|
+
"metro-cache-key": "0.67.0",
|
|
25
|
+
"metro-hermes-compiler": "0.67.0",
|
|
26
|
+
"metro-source-map": "0.67.0",
|
|
27
|
+
"metro-transform-plugins": "0.67.0",
|
|
28
28
|
"nullthrows": "^1.1.1"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"metro-memory-fs": "0.
|
|
32
|
-
"metro-minify-uglify": "0.
|
|
33
|
-
"metro-react-native-babel-transformer": "0.
|
|
31
|
+
"metro-memory-fs": "0.67.0",
|
|
32
|
+
"metro-minify-uglify": "0.67.0",
|
|
33
|
+
"metro-react-native-babel-transformer": "0.67.0",
|
|
34
34
|
"mkdirp": "^0.5.1"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/__mocks__/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -8,6 +8,6 @@
|
|
|
8
8
|
*/
|
|
9
9
|
"use strict";
|
|
10
10
|
|
|
11
|
-
module.exports = function(data, callback) {
|
|
11
|
+
module.exports = function (data, callback) {
|
|
12
12
|
callback(null, {});
|
|
13
13
|
};
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -9,42 +9,42 @@
|
|
|
9
9
|
*/
|
|
10
10
|
"use strict";
|
|
11
11
|
|
|
12
|
-
const
|
|
12
|
+
const getMinifier = require("./utils/getMinifier");
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const { transformFromAstSync } = require("@babel/core");
|
|
15
15
|
|
|
16
|
-
const
|
|
16
|
+
const generate = require("@babel/generator").default;
|
|
17
17
|
|
|
18
|
-
const
|
|
18
|
+
const babylon = require("@babel/parser");
|
|
19
19
|
|
|
20
|
-
const
|
|
21
|
-
InvalidRequireCallError: InternalInvalidRequireCallError
|
|
22
|
-
} = require("metro/src/ModuleGraph/worker/collectDependencies");
|
|
20
|
+
const types = require("@babel/types");
|
|
23
21
|
|
|
24
|
-
const
|
|
22
|
+
const { stableHash } = require("metro-cache");
|
|
25
23
|
|
|
26
24
|
const getCacheKey = require("metro-cache-key");
|
|
27
25
|
|
|
28
|
-
const
|
|
26
|
+
const HermesCompiler = require("metro-hermes-compiler");
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
fromRawMappings,
|
|
30
|
+
toBabelSegments,
|
|
31
|
+
toSegmentTuple,
|
|
32
|
+
} = require("metro-source-map");
|
|
29
33
|
|
|
30
34
|
const metroTransformPlugins = require("metro-transform-plugins");
|
|
31
35
|
|
|
32
|
-
const
|
|
36
|
+
const countLines = require("metro/src/lib/countLines");
|
|
33
37
|
|
|
34
|
-
const {
|
|
38
|
+
const {
|
|
39
|
+
InvalidRequireCallError: InternalInvalidRequireCallError,
|
|
40
|
+
} = require("metro/src/ModuleGraph/worker/collectDependencies");
|
|
35
41
|
|
|
36
|
-
const
|
|
42
|
+
const generateImportNames = require("metro/src/ModuleGraph/worker/generateImportNames");
|
|
37
43
|
|
|
38
|
-
const
|
|
44
|
+
const JsFileWrapping = require("metro/src/ModuleGraph/worker/JsFileWrapping");
|
|
39
45
|
|
|
40
46
|
const nullthrows = require("nullthrows");
|
|
41
47
|
|
|
42
|
-
const {
|
|
43
|
-
fromRawMappings,
|
|
44
|
-
toBabelSegments,
|
|
45
|
-
toSegmentTuple
|
|
46
|
-
} = require("metro-source-map");
|
|
47
|
-
|
|
48
48
|
function getDynamicDepsBehavior(inPackages, filename) {
|
|
49
49
|
switch (inPackages) {
|
|
50
50
|
case "reject":
|
|
@@ -77,22 +77,24 @@ const minifyCode = async (
|
|
|
77
77
|
source,
|
|
78
78
|
map,
|
|
79
79
|
functionMap: null,
|
|
80
|
-
path: filename
|
|
81
|
-
}
|
|
80
|
+
path: filename,
|
|
81
|
+
},
|
|
82
82
|
]).toMap(undefined, {});
|
|
83
83
|
const minify = getMinifier(config.minifierPath);
|
|
84
84
|
|
|
85
85
|
try {
|
|
86
|
-
const minified = minify({
|
|
86
|
+
const minified = await minify({
|
|
87
87
|
code,
|
|
88
88
|
map: sourceMap,
|
|
89
89
|
filename,
|
|
90
90
|
reserved,
|
|
91
|
-
config: config.minifierConfig
|
|
91
|
+
config: config.minifierConfig,
|
|
92
92
|
});
|
|
93
93
|
return {
|
|
94
94
|
code: minified.code,
|
|
95
|
-
map: minified.map
|
|
95
|
+
map: minified.map
|
|
96
|
+
? toBabelSegments(minified.map).map(toSegmentTuple)
|
|
97
|
+
: [],
|
|
96
98
|
};
|
|
97
99
|
} catch (error) {
|
|
98
100
|
if (error.constructor.name === "JS_Parse_Error") {
|
|
@@ -124,7 +126,7 @@ const disabledDependencyTransformer = {
|
|
|
124
126
|
transformImportCall: () => void 0,
|
|
125
127
|
transformJSResource: () => void 0,
|
|
126
128
|
transformPrefetch: () => void 0,
|
|
127
|
-
transformIllegalDynamicRequire: () => void 0
|
|
129
|
+
transformIllegalDynamicRequire: () => void 0,
|
|
128
130
|
};
|
|
129
131
|
|
|
130
132
|
class InvalidRequireCallError extends Error {
|
|
@@ -144,7 +146,7 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
144
146
|
(_file$ast = file.ast) !== null && _file$ast !== void 0
|
|
145
147
|
? _file$ast
|
|
146
148
|
: babylon.parse(file.code, {
|
|
147
|
-
sourceType: "unambiguous"
|
|
149
|
+
sourceType: "unambiguous",
|
|
148
150
|
});
|
|
149
151
|
const { importDefault, importAll } = generateImportNames(ast); // Add "use strict" if the file was parsed as a module, and the directive did
|
|
150
152
|
// not exist yet.
|
|
@@ -154,7 +156,7 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
154
156
|
if (
|
|
155
157
|
ast.program.sourceType === "module" &&
|
|
156
158
|
directives != null &&
|
|
157
|
-
directives.findIndex(d => d.value.value === "use strict") === -1
|
|
159
|
+
directives.findIndex((d) => d.value.value === "use strict") === -1
|
|
158
160
|
) {
|
|
159
161
|
directives.push(types.directive(types.directiveLiteral("use strict")));
|
|
160
162
|
} // Perform the import-export transform (in case it's still needed), then
|
|
@@ -165,7 +167,7 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
165
167
|
...options,
|
|
166
168
|
inlineableCalls: [importDefault, importAll],
|
|
167
169
|
importDefault,
|
|
168
|
-
importAll
|
|
170
|
+
importAll,
|
|
169
171
|
};
|
|
170
172
|
|
|
171
173
|
if (options.experimentalImportSupport === true) {
|
|
@@ -176,7 +178,7 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
176
178
|
plugins.push([
|
|
177
179
|
// $FlowFixMe[untyped-import] untyped module
|
|
178
180
|
require("babel-preset-fbjs/plugins/inline-requires"),
|
|
179
|
-
{ ...babelPluginOpts, ignoredRequires: options.nonInlinedRequires }
|
|
181
|
+
{ ...babelPluginOpts, ignoredRequires: options.nonInlinedRequires },
|
|
180
182
|
]);
|
|
181
183
|
}
|
|
182
184
|
|
|
@@ -196,7 +198,7 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
196
198
|
// However, switching the flag to false caused issues with ES Modules if `experimentalImportSupport` isn't used https://github.com/facebook/metro/issues/641
|
|
197
199
|
// either because one of the plugins is doing something funky or Babel messes up some caches.
|
|
198
200
|
// Make sure to test the above mentioned case before flipping the flag back to false.
|
|
199
|
-
cloneInputAst: true
|
|
201
|
+
cloneInputAst: true,
|
|
200
202
|
}).ast
|
|
201
203
|
);
|
|
202
204
|
|
|
@@ -213,10 +215,10 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
213
215
|
comments: false,
|
|
214
216
|
filename: file.filename,
|
|
215
217
|
plugins: [
|
|
216
|
-
[metroTransformPlugins.constantFoldingPlugin, babelPluginOpts]
|
|
218
|
+
[metroTransformPlugins.constantFoldingPlugin, babelPluginOpts],
|
|
217
219
|
],
|
|
218
220
|
sourceMaps: false,
|
|
219
|
-
cloneInputAst: false
|
|
221
|
+
cloneInputAst: false,
|
|
220
222
|
}).ast
|
|
221
223
|
);
|
|
222
224
|
}
|
|
@@ -246,7 +248,7 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
246
248
|
inlineableCalls: [importDefault, importAll],
|
|
247
249
|
keepRequireNames: options.dev,
|
|
248
250
|
allowOptionalDependencies: config.allowOptionalDependencies,
|
|
249
|
-
dependencyMapName: config.unstable_dependencyMapReservedName
|
|
251
|
+
dependencyMapName: config.unstable_dependencyMapReservedName,
|
|
250
252
|
}; // $FlowFixMe[unsupported-syntax] dynamic require
|
|
251
253
|
|
|
252
254
|
const collectDependencies = require(config.unstable_collectDependenciesPath);
|
|
@@ -293,7 +295,7 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
293
295
|
) {
|
|
294
296
|
reserved.push(
|
|
295
297
|
...metroTransformPlugins.normalizePseudoGlobals(wrappedAst, {
|
|
296
|
-
reservedNames: reserved
|
|
298
|
+
reservedNames: reserved,
|
|
297
299
|
})
|
|
298
300
|
);
|
|
299
301
|
}
|
|
@@ -306,7 +308,7 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
306
308
|
filename: file.filename,
|
|
307
309
|
retainLines: false,
|
|
308
310
|
sourceFileName: file.filename,
|
|
309
|
-
sourceMaps: true
|
|
311
|
+
sourceMaps: true,
|
|
310
312
|
},
|
|
311
313
|
file.code
|
|
312
314
|
);
|
|
@@ -331,10 +333,10 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
331
333
|
code,
|
|
332
334
|
lineCount: countLines(code),
|
|
333
335
|
map,
|
|
334
|
-
functionMap: file.functionMap
|
|
336
|
+
functionMap: file.functionMap,
|
|
335
337
|
},
|
|
336
|
-
type: file.type
|
|
337
|
-
}
|
|
338
|
+
type: file.type,
|
|
339
|
+
},
|
|
338
340
|
];
|
|
339
341
|
|
|
340
342
|
if (options.runtimeBytecodeVersion != null) {
|
|
@@ -347,17 +349,17 @@ async function transformJS(file, { config, options, projectRoot }) {
|
|
|
347
349
|
source: file.code,
|
|
348
350
|
map,
|
|
349
351
|
functionMap: null,
|
|
350
|
-
path: file.filename
|
|
351
|
-
}
|
|
352
|
-
]).toString()
|
|
352
|
+
path: file.filename,
|
|
353
|
+
},
|
|
354
|
+
]).toString(),
|
|
353
355
|
}),
|
|
354
|
-
type: getBytecodeFileType(file.type)
|
|
356
|
+
type: getBytecodeFileType(file.type),
|
|
355
357
|
});
|
|
356
358
|
}
|
|
357
359
|
|
|
358
360
|
return {
|
|
359
361
|
dependencies,
|
|
360
|
-
output
|
|
362
|
+
output,
|
|
361
363
|
};
|
|
362
364
|
}
|
|
363
365
|
/**
|
|
@@ -377,7 +379,7 @@ async function transformAsset(file, context) {
|
|
|
377
379
|
...file,
|
|
378
380
|
type: "js/module/asset",
|
|
379
381
|
ast: result.ast,
|
|
380
|
-
functionMap: null
|
|
382
|
+
functionMap: null,
|
|
381
383
|
};
|
|
382
384
|
return transformJS(jsFile, context);
|
|
383
385
|
}
|
|
@@ -403,7 +405,7 @@ async function transformJSWithBabel(file, context) {
|
|
|
403
405
|
(_transformResult$func = transformResult.functionMap) !== null &&
|
|
404
406
|
_transformResult$func !== void 0
|
|
405
407
|
? _transformResult$func
|
|
406
|
-
: null
|
|
408
|
+
: null,
|
|
407
409
|
};
|
|
408
410
|
return await transformJS(jsFile, context);
|
|
409
411
|
}
|
|
@@ -447,10 +449,10 @@ async function transformJSON(file, { options, config, projectRoot }) {
|
|
|
447
449
|
code,
|
|
448
450
|
lineCount: countLines(code),
|
|
449
451
|
map,
|
|
450
|
-
functionMap: null
|
|
452
|
+
functionMap: null,
|
|
451
453
|
},
|
|
452
|
-
type: jsType
|
|
453
|
-
}
|
|
454
|
+
type: jsType,
|
|
455
|
+
},
|
|
454
456
|
];
|
|
455
457
|
|
|
456
458
|
if (options.runtimeBytecodeVersion != null) {
|
|
@@ -463,17 +465,17 @@ async function transformJSON(file, { options, config, projectRoot }) {
|
|
|
463
465
|
source: file.code,
|
|
464
466
|
map,
|
|
465
467
|
functionMap: null,
|
|
466
|
-
path: file.filename
|
|
467
|
-
}
|
|
468
|
-
]).toString()
|
|
468
|
+
path: file.filename,
|
|
469
|
+
},
|
|
470
|
+
]).toString(),
|
|
469
471
|
}),
|
|
470
|
-
type: getBytecodeFileType(jsType)
|
|
472
|
+
type: getBytecodeFileType(jsType),
|
|
471
473
|
});
|
|
472
474
|
}
|
|
473
475
|
|
|
474
476
|
return {
|
|
475
477
|
dependencies: [],
|
|
476
|
-
output
|
|
478
|
+
output,
|
|
477
479
|
};
|
|
478
480
|
}
|
|
479
481
|
/**
|
|
@@ -509,10 +511,10 @@ function getBabelTransformArgs(file, { options, config, projectRoot }) {
|
|
|
509
511
|
inlineRequires: false,
|
|
510
512
|
nonInlinedRequires: [],
|
|
511
513
|
projectRoot,
|
|
512
|
-
publicPath: config.publicPath
|
|
514
|
+
publicPath: config.publicPath,
|
|
513
515
|
},
|
|
514
516
|
plugins: [],
|
|
515
|
-
src: file.code
|
|
517
|
+
src: file.code,
|
|
516
518
|
};
|
|
517
519
|
}
|
|
518
520
|
|
|
@@ -521,7 +523,7 @@ module.exports = {
|
|
|
521
523
|
const context = {
|
|
522
524
|
config,
|
|
523
525
|
projectRoot,
|
|
524
|
-
options
|
|
526
|
+
options,
|
|
525
527
|
};
|
|
526
528
|
const sourceCode = data.toString("utf8");
|
|
527
529
|
const { unstable_dependencyMapReservedName } = config;
|
|
@@ -544,7 +546,7 @@ module.exports = {
|
|
|
544
546
|
filename,
|
|
545
547
|
inputFileSize: data.length,
|
|
546
548
|
code: sourceCode,
|
|
547
|
-
type: options.type
|
|
549
|
+
type: options.type,
|
|
548
550
|
};
|
|
549
551
|
return await transformJSON(jsonFile, context);
|
|
550
552
|
}
|
|
@@ -554,7 +556,7 @@ module.exports = {
|
|
|
554
556
|
filename,
|
|
555
557
|
inputFileSize: data.length,
|
|
556
558
|
code: sourceCode,
|
|
557
|
-
type: options.type
|
|
559
|
+
type: options.type,
|
|
558
560
|
};
|
|
559
561
|
return await transformAsset(file, context);
|
|
560
562
|
}
|
|
@@ -564,11 +566,11 @@ module.exports = {
|
|
|
564
566
|
inputFileSize: data.length,
|
|
565
567
|
code: sourceCode,
|
|
566
568
|
type: options.type === "script" ? "js/script" : "js/module",
|
|
567
|
-
functionMap: null
|
|
569
|
+
functionMap: null,
|
|
568
570
|
};
|
|
569
571
|
return await transformJSWithBabel(file, context);
|
|
570
572
|
},
|
|
571
|
-
getCacheKey: config => {
|
|
573
|
+
getCacheKey: (config) => {
|
|
572
574
|
const {
|
|
573
575
|
babelTransformerPath,
|
|
574
576
|
minifierPath,
|
|
@@ -583,7 +585,7 @@ module.exports = {
|
|
|
583
585
|
require.resolve(unstable_collectDependenciesPath),
|
|
584
586
|
require.resolve("metro/src/ModuleGraph/worker/generateImportNames"),
|
|
585
587
|
require.resolve("metro/src/ModuleGraph/worker/JsFileWrapping"),
|
|
586
|
-
...metroTransformPlugins.getTransformPluginCacheKeyFiles()
|
|
588
|
+
...metroTransformPlugins.getTransformPluginCacheKeyFiles(),
|
|
587
589
|
]);
|
|
588
590
|
|
|
589
591
|
const babelTransformer = require(babelTransformerPath);
|
|
@@ -591,7 +593,7 @@ module.exports = {
|
|
|
591
593
|
return [
|
|
592
594
|
filesKey,
|
|
593
595
|
stableHash(remainingConfig).toString("hex"),
|
|
594
|
-
babelTransformer.getCacheKey ? babelTransformer.getCacheKey() : ""
|
|
596
|
+
babelTransformer.getCacheKey ? babelTransformer.getCacheKey() : "",
|
|
595
597
|
].join("$");
|
|
596
|
-
}
|
|
598
|
+
},
|
|
597
599
|
};
|
package/src/index.js.flow
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -10,52 +10,50 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
import type {
|
|
14
|
+
BabelTransformer,
|
|
15
|
+
BabelTransformerArgs,
|
|
16
|
+
CustomTransformOptions,
|
|
17
|
+
TransformProfile,
|
|
18
|
+
} from 'metro-babel-transformer';
|
|
19
|
+
import type {
|
|
20
|
+
HermesCompilerResult,
|
|
21
|
+
Options as HermesCompilerOptions,
|
|
22
|
+
} from 'metro-hermes-compiler';
|
|
23
|
+
import type {
|
|
24
|
+
BasicSourceMap,
|
|
25
|
+
FBSourceFunctionMap,
|
|
26
|
+
MetroSourceMapSegmentTuple,
|
|
27
|
+
} from 'metro-source-map';
|
|
28
|
+
import type {TransformResultDependency} from 'metro/src/DeltaBundler';
|
|
29
|
+
import type {AllowOptionalDependencies} from 'metro/src/DeltaBundler/types.flow.js';
|
|
30
|
+
import type {
|
|
31
|
+
DependencyTransformer,
|
|
32
|
+
DynamicRequiresBehavior,
|
|
33
|
+
} from 'metro/src/ModuleGraph/worker/collectDependencies';
|
|
34
|
+
import typeof CollectDependenciesFn from 'metro/src/ModuleGraph/worker/collectDependencies';
|
|
18
35
|
|
|
19
|
-
const {
|
|
20
|
-
InvalidRequireCallError: InternalInvalidRequireCallError,
|
|
21
|
-
} = require('metro/src/ModuleGraph/worker/collectDependencies');
|
|
22
|
-
const generate = require('@babel/generator').default;
|
|
23
|
-
const getCacheKey = require('metro-cache-key');
|
|
24
36
|
const getMinifier = require('./utils/getMinifier');
|
|
25
|
-
const metroTransformPlugins = require('metro-transform-plugins');
|
|
26
37
|
const {transformFromAstSync} = require('@babel/core');
|
|
27
|
-
const
|
|
38
|
+
const generate = require('@babel/generator').default;
|
|
39
|
+
const babylon = require('@babel/parser');
|
|
28
40
|
const types = require('@babel/types');
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
41
|
+
const {stableHash} = require('metro-cache');
|
|
42
|
+
const getCacheKey = require('metro-cache-key');
|
|
43
|
+
const HermesCompiler = require('metro-hermes-compiler');
|
|
32
44
|
const {
|
|
33
45
|
fromRawMappings,
|
|
34
46
|
toBabelSegments,
|
|
35
47
|
toSegmentTuple,
|
|
36
48
|
} = require('metro-source-map');
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
FBSourceFunctionMap,
|
|
46
|
-
MetroSourceMapSegmentTuple,
|
|
47
|
-
} from 'metro-source-map';
|
|
48
|
-
import type {
|
|
49
|
-
HermesCompilerResult,
|
|
50
|
-
Options as HermesCompilerOptions,
|
|
51
|
-
} from 'metro-hermes-compiler';
|
|
52
|
-
import type {
|
|
53
|
-
BabelTransformer,
|
|
54
|
-
BabelTransformerArgs,
|
|
55
|
-
CustomTransformOptions,
|
|
56
|
-
TransformProfile,
|
|
57
|
-
} from 'metro-babel-transformer';
|
|
58
|
-
import typeof CollectDependenciesFn from 'metro/src/ModuleGraph/worker/collectDependencies';
|
|
49
|
+
const metroTransformPlugins = require('metro-transform-plugins');
|
|
50
|
+
const countLines = require('metro/src/lib/countLines');
|
|
51
|
+
const {
|
|
52
|
+
InvalidRequireCallError: InternalInvalidRequireCallError,
|
|
53
|
+
} = require('metro/src/ModuleGraph/worker/collectDependencies');
|
|
54
|
+
const generateImportNames = require('metro/src/ModuleGraph/worker/generateImportNames');
|
|
55
|
+
const JsFileWrapping = require('metro/src/ModuleGraph/worker/JsFileWrapping');
|
|
56
|
+
const nullthrows = require('nullthrows');
|
|
59
57
|
|
|
60
58
|
type MinifierConfig = $ReadOnly<{[string]: mixed, ...}>;
|
|
61
59
|
|
|
@@ -74,7 +72,9 @@ export type MinifierResult = {
|
|
|
74
72
|
...
|
|
75
73
|
};
|
|
76
74
|
|
|
77
|
-
export type Minifier = MinifierOptions =>
|
|
75
|
+
export type Minifier = MinifierOptions =>
|
|
76
|
+
| MinifierResult
|
|
77
|
+
| Promise<MinifierResult>;
|
|
78
78
|
|
|
79
79
|
export type Type = 'script' | 'module' | 'asset';
|
|
80
80
|
|
|
@@ -220,7 +220,7 @@ const minifyCode = async (
|
|
|
220
220
|
const minify = getMinifier(config.minifierPath);
|
|
221
221
|
|
|
222
222
|
try {
|
|
223
|
-
const minified = minify({
|
|
223
|
+
const minified = await minify({
|
|
224
224
|
code,
|
|
225
225
|
map: sourceMap,
|
|
226
226
|
filename,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -9,12 +9,12 @@
|
|
|
9
9
|
*/
|
|
10
10
|
"use strict";
|
|
11
11
|
|
|
12
|
-
const path = require("path");
|
|
13
|
-
|
|
14
12
|
const { getAssetData } = require("metro/src/Assets");
|
|
15
13
|
|
|
16
14
|
const { generateAssetCodeFileAst } = require("metro/src/Bundler/util");
|
|
17
15
|
|
|
16
|
+
const path = require("path");
|
|
17
|
+
|
|
18
18
|
async function transform(
|
|
19
19
|
{ filename, options, src },
|
|
20
20
|
assetRegistryPath,
|
|
@@ -24,7 +24,7 @@ async function transform(
|
|
|
24
24
|
platform: "",
|
|
25
25
|
projectRoot: "",
|
|
26
26
|
inlineRequires: false,
|
|
27
|
-
minify: false
|
|
27
|
+
minify: false,
|
|
28
28
|
};
|
|
29
29
|
const absolutePath = path.resolve(options.projectRoot, filename);
|
|
30
30
|
const data = await getAssetData(
|
|
@@ -35,10 +35,10 @@ async function transform(
|
|
|
35
35
|
options.publicPath
|
|
36
36
|
);
|
|
37
37
|
return {
|
|
38
|
-
ast: generateAssetCodeFileAst(assetRegistryPath, data)
|
|
38
|
+
ast: generateAssetCodeFileAst(assetRegistryPath, data),
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
module.exports = {
|
|
43
|
-
transform
|
|
43
|
+
transform,
|
|
44
44
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -10,13 +10,12 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
import type {File} from '@babel/types';
|
|
14
|
+
import type {BabelTransformerArgs} from 'metro-babel-transformer';
|
|
14
15
|
|
|
15
16
|
const {getAssetData} = require('metro/src/Assets');
|
|
16
17
|
const {generateAssetCodeFileAst} = require('metro/src/Bundler/util');
|
|
17
|
-
|
|
18
|
-
import type {File} from '@babel/types';
|
|
19
|
-
import type {BabelTransformerArgs} from 'metro-babel-transformer';
|
|
18
|
+
const path = require('path');
|
|
20
19
|
|
|
21
20
|
async function transform(
|
|
22
21
|
{filename, options, src}: BabelTransformerArgs,
|
package/src/utils/getMinifier.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -9,6 +9,6 @@
|
|
|
9
9
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
|
-
module.exports = function(data, callback) {
|
|
12
|
+
module.exports = function (data, callback) {
|
|
13
13
|
callback(null, {});
|
|
14
14
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -11,8 +11,6 @@
|
|
|
11
11
|
|
|
12
12
|
'use strict';
|
|
13
13
|
|
|
14
|
-
import type {JsTransformerConfig} from '../index';
|
|
15
|
-
|
|
16
14
|
jest
|
|
17
15
|
.mock('../utils/getMinifier', () => () => ({code, map}) => ({
|
|
18
16
|
code: code.replace('arbitrary(code)', 'minified(code)'),
|
|
@@ -25,14 +23,16 @@ jest
|
|
|
25
23
|
}))
|
|
26
24
|
.mock('metro-minify-uglify');
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
import type {JsTransformerConfig} from '../index';
|
|
29
27
|
|
|
28
|
+
const HermesCompiler = require('metro-hermes-compiler');
|
|
30
29
|
const path = require('path');
|
|
31
30
|
|
|
32
31
|
const babelTransformerPath = require.resolve(
|
|
33
32
|
'metro-react-native-babel-transformer',
|
|
34
33
|
);
|
|
35
|
-
const transformerContents =
|
|
34
|
+
const transformerContents = (() =>
|
|
35
|
+
require('fs').readFileSync(babelTransformerPath))();
|
|
36
36
|
|
|
37
37
|
const HEADER_DEV =
|
|
38
38
|
'__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {';
|
|
@@ -600,3 +600,26 @@ it('skips minification in Hermes canary transform profile', async () => {
|
|
|
600
600
|
});"
|
|
601
601
|
`);
|
|
602
602
|
});
|
|
603
|
+
|
|
604
|
+
it('counts all line endings correctly', async () => {
|
|
605
|
+
const transformStr = (
|
|
606
|
+
str: $TEMPORARY$string<'one\ntwo\nthree\nfour\nfive\nsix'> | string,
|
|
607
|
+
) =>
|
|
608
|
+
Transformer.transform(baseConfig, '/root', 'local/file.js', str, {
|
|
609
|
+
dev: false,
|
|
610
|
+
minify: false,
|
|
611
|
+
type: 'module',
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
const differentEndingsResult = await transformStr(
|
|
615
|
+
'one\rtwo\r\nthree\nfour\u2028five\u2029six',
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
const standardEndingsResult = await transformStr(
|
|
619
|
+
'one\ntwo\nthree\nfour\nfive\nsix',
|
|
620
|
+
);
|
|
621
|
+
|
|
622
|
+
expect(differentEndingsResult.output[0].data.lineCount).toEqual(
|
|
623
|
+
standardEndingsResult.output[0].data.lineCount,
|
|
624
|
+
);
|
|
625
|
+
});
|
package/src.real/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -10,52 +10,50 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
import type {
|
|
14
|
+
BabelTransformer,
|
|
15
|
+
BabelTransformerArgs,
|
|
16
|
+
CustomTransformOptions,
|
|
17
|
+
TransformProfile,
|
|
18
|
+
} from 'metro-babel-transformer';
|
|
19
|
+
import type {
|
|
20
|
+
HermesCompilerResult,
|
|
21
|
+
Options as HermesCompilerOptions,
|
|
22
|
+
} from 'metro-hermes-compiler';
|
|
23
|
+
import type {
|
|
24
|
+
BasicSourceMap,
|
|
25
|
+
FBSourceFunctionMap,
|
|
26
|
+
MetroSourceMapSegmentTuple,
|
|
27
|
+
} from 'metro-source-map';
|
|
28
|
+
import type {TransformResultDependency} from 'metro/src/DeltaBundler';
|
|
29
|
+
import type {AllowOptionalDependencies} from 'metro/src/DeltaBundler/types.flow.js';
|
|
30
|
+
import type {
|
|
31
|
+
DependencyTransformer,
|
|
32
|
+
DynamicRequiresBehavior,
|
|
33
|
+
} from 'metro/src/ModuleGraph/worker/collectDependencies';
|
|
34
|
+
import typeof CollectDependenciesFn from 'metro/src/ModuleGraph/worker/collectDependencies';
|
|
18
35
|
|
|
19
|
-
const {
|
|
20
|
-
InvalidRequireCallError: InternalInvalidRequireCallError,
|
|
21
|
-
} = require('metro/src/ModuleGraph/worker/collectDependencies');
|
|
22
|
-
const generate = require('@babel/generator').default;
|
|
23
|
-
const getCacheKey = require('metro-cache-key');
|
|
24
36
|
const getMinifier = require('./utils/getMinifier');
|
|
25
|
-
const metroTransformPlugins = require('metro-transform-plugins');
|
|
26
37
|
const {transformFromAstSync} = require('@babel/core');
|
|
27
|
-
const
|
|
38
|
+
const generate = require('@babel/generator').default;
|
|
39
|
+
const babylon = require('@babel/parser');
|
|
28
40
|
const types = require('@babel/types');
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
41
|
+
const {stableHash} = require('metro-cache');
|
|
42
|
+
const getCacheKey = require('metro-cache-key');
|
|
43
|
+
const HermesCompiler = require('metro-hermes-compiler');
|
|
32
44
|
const {
|
|
33
45
|
fromRawMappings,
|
|
34
46
|
toBabelSegments,
|
|
35
47
|
toSegmentTuple,
|
|
36
48
|
} = require('metro-source-map');
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
FBSourceFunctionMap,
|
|
46
|
-
MetroSourceMapSegmentTuple,
|
|
47
|
-
} from 'metro-source-map';
|
|
48
|
-
import type {
|
|
49
|
-
HermesCompilerResult,
|
|
50
|
-
Options as HermesCompilerOptions,
|
|
51
|
-
} from 'metro-hermes-compiler';
|
|
52
|
-
import type {
|
|
53
|
-
BabelTransformer,
|
|
54
|
-
BabelTransformerArgs,
|
|
55
|
-
CustomTransformOptions,
|
|
56
|
-
TransformProfile,
|
|
57
|
-
} from 'metro-babel-transformer';
|
|
58
|
-
import typeof CollectDependenciesFn from 'metro/src/ModuleGraph/worker/collectDependencies';
|
|
49
|
+
const metroTransformPlugins = require('metro-transform-plugins');
|
|
50
|
+
const countLines = require('metro/src/lib/countLines');
|
|
51
|
+
const {
|
|
52
|
+
InvalidRequireCallError: InternalInvalidRequireCallError,
|
|
53
|
+
} = require('metro/src/ModuleGraph/worker/collectDependencies');
|
|
54
|
+
const generateImportNames = require('metro/src/ModuleGraph/worker/generateImportNames');
|
|
55
|
+
const JsFileWrapping = require('metro/src/ModuleGraph/worker/JsFileWrapping');
|
|
56
|
+
const nullthrows = require('nullthrows');
|
|
59
57
|
|
|
60
58
|
type MinifierConfig = $ReadOnly<{[string]: mixed, ...}>;
|
|
61
59
|
|
|
@@ -74,7 +72,9 @@ export type MinifierResult = {
|
|
|
74
72
|
...
|
|
75
73
|
};
|
|
76
74
|
|
|
77
|
-
export type Minifier = MinifierOptions =>
|
|
75
|
+
export type Minifier = MinifierOptions =>
|
|
76
|
+
| MinifierResult
|
|
77
|
+
| Promise<MinifierResult>;
|
|
78
78
|
|
|
79
79
|
export type Type = 'script' | 'module' | 'asset';
|
|
80
80
|
|
|
@@ -220,7 +220,7 @@ const minifyCode = async (
|
|
|
220
220
|
const minify = getMinifier(config.minifierPath);
|
|
221
221
|
|
|
222
222
|
try {
|
|
223
|
-
const minified = minify({
|
|
223
|
+
const minified = await minify({
|
|
224
224
|
code,
|
|
225
225
|
map: sourceMap,
|
|
226
226
|
filename,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -10,13 +10,12 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
import type {File} from '@babel/types';
|
|
14
|
+
import type {BabelTransformerArgs} from 'metro-babel-transformer';
|
|
14
15
|
|
|
15
16
|
const {getAssetData} = require('metro/src/Assets');
|
|
16
17
|
const {generateAssetCodeFileAst} = require('metro/src/Bundler/util');
|
|
17
|
-
|
|
18
|
-
import type {File} from '@babel/types';
|
|
19
|
-
import type {BabelTransformerArgs} from 'metro-babel-transformer';
|
|
18
|
+
const path = require('path');
|
|
20
19
|
|
|
21
20
|
async function transform(
|
|
22
21
|
{filename, options, src}: BabelTransformerArgs,
|