2015-09-16 17:30:53 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-09-16 17:30:53 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-05-09 15:02:08 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2017-07-13 02:22:43 +00:00
|
|
|
* @flow
|
2015-09-16 17:30:53 +00:00
|
|
|
*/
|
2017-07-12 20:48:49 +00:00
|
|
|
|
|
|
|
/* eslint-env node */
|
|
|
|
|
2015-09-16 17:30:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-11 23:29:36 +00:00
|
|
|
const {transformSync: babelTransformSync} = require('@babel/core');
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2018-04-23 10:52:15 +00:00
|
|
|
const babelRegisterOnly = require('metro-babel-register');
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2015-09-19 22:20:25 +00:00
|
|
|
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
|
2018-04-11 23:29:36 +00:00
|
|
|
const generate = require('@babel/generator').default;
|
2015-09-16 17:30:53 +00:00
|
|
|
|
2018-08-22 20:35:52 +00:00
|
|
|
const nodeFiles = new RegExp(
|
2018-05-11 20:32:37 +00:00
|
|
|
[
|
2019-01-21 17:08:36 +00:00
|
|
|
'/metro(?:-[^/]*)?/', // metro, metro-core, metro-source-map, metro-etc.
|
2018-05-11 20:32:37 +00:00
|
|
|
].join('|'),
|
|
|
|
);
|
2016-07-15 13:25:27 +00:00
|
|
|
const nodeOptions = babelRegisterOnly.config([nodeFiles]);
|
|
|
|
|
create better debuggable source maps
Summary:
Introduces a new mechanism to build source maps that allows us to use real mapping segments instead of just mapping line-by-line.
This mechanism is only used when building development bundles to improve the debugging experience in Chrome.
The new mechanism takes advantage of a new feature in babel-generator that exposes raw mapping objects. These raw mapping objects are converted to arrays with 2, 4, or 5 for the most compact representation possible.
We no longer generate a source map for the bundle that maps each line to itself in conjunction with configuring babel generator to retain lines.
Instead, we create a source map with a large mappings object produced from the mappings of each individual file in conjunction with a “carry over” – the number of preceding lines in the bundle.
The implementation makes a couple of assumptions that hold true for babel transform results, e.g. mappings being in the order of the generated code, and that a block of mappings always belongs to the same source file. In addition, the implementation avoids allocation of objects and strings at all costs. All calculations are purely numeric, and base64 vlq produces numeric ascii character codes. These are written to a preallocated buffer objects, which is turned to a string only at the end of the building process. This implementation is ~5x faster than using the source-map library.
In addition to providing development source maps that work better, we can now also produce individual high-quality source maps for production builds and combine them to an “index source map”. This approach is unfeasable for development source maps, because index source map consistently crash Chrome.
Better production source maps are useful to get precise information about source location and symbol names when symbolicating stack traces from crashes in production.
Reviewed By: jeanlauliac
Differential Revision: D4382290
fbshipit-source-id: 365a176fa142729d0a4cef43edeb81084361e54d
2017-01-12 22:21:59 +00:00
|
|
|
babelRegisterOnly([]);
|
|
|
|
|
2018-12-14 16:12:44 +00:00
|
|
|
const transformer = require('metro-react-native-babel-transformer');
|
2015-09-16 17:30:53 +00:00
|
|
|
module.exports = {
|
2018-05-11 20:32:37 +00:00
|
|
|
process(src /*: string */, file /*: string */) {
|
|
|
|
if (nodeFiles.test(file)) {
|
|
|
|
// node specific transforms only
|
2018-08-13 18:59:59 +00:00
|
|
|
return babelTransformSync(src, {
|
|
|
|
filename: file,
|
|
|
|
sourceType: 'script',
|
|
|
|
...nodeOptions,
|
|
|
|
ast: false,
|
|
|
|
}).code;
|
2015-09-22 19:11:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 21:29:42 +00:00
|
|
|
const {ast} = transformer.transform({
|
2017-05-11 15:32:04 +00:00
|
|
|
filename: file,
|
|
|
|
options: {
|
2018-04-19 16:09:08 +00:00
|
|
|
ast: true, // needed for open source (?) https://github.com/facebook/react-native/commit/f8d6b97140cffe8d18b2558f94570c8d1b410d5c#r28647044
|
2017-05-11 15:32:04 +00:00
|
|
|
dev: true,
|
2018-10-05 18:31:17 +00:00
|
|
|
enableBabelRuntime: false,
|
2018-09-24 17:06:36 +00:00
|
|
|
experimentalImportSupport: false,
|
|
|
|
hot: false,
|
2017-05-11 15:32:04 +00:00
|
|
|
inlineRequires: true,
|
Standarize options passed to the transformer
Summary:
This is the first step to make transformers fully customizable (and not be tied to JS, or RN). In order to do that, I'm changing the signature of the transformers, which currently is:
```
function transformCode(
filename: string,
localPath: LocalPath,
transformerPath: string,
options: WorkerOptions,
assetExts: $ReadOnlyArray<string>,
assetRegistryPath: string,
minifierPath: string,
asyncRequireModulePath: string,
dynamicDepsInPackages: DynamicRequiresBehavior,
)
```
to be:
```
async function transformCode(
filename: string,
localPath: LocalPath,
options: WorkerOptions,
)
```
(so basically, all the RN-custom properties are moved to `WorkerOptions`, which in the future will be a generic to allow anybody pass any random option to their transformers).
In order to make all this work, I've had to get rid of the logic that calculates the base cache key hash based on a subset of worker options (the ones that Metro knows that are not going to change between runs).
This could potentially cause a perf regression (since we're now making the hash calculation a bit more costly), and in fact I could measure a ~400ms regression on the worse case scenario (which happens when restarting Metro and re-transforming a Wilde from a warm local cache).
I've benchmarked this regression and could find that it's caused by the array of `assetExtensions` (which is potentially large). I have a followup diff to improve this, which is able to remove the regression completely.
Reviewed By: pvdz
Differential Revision: D8695766
fbshipit-source-id: eccd18a4cbc91854f34d5c9ba7f95088f19483a1
2018-08-23 22:44:38 +00:00
|
|
|
minify: false,
|
2017-05-11 15:32:04 +00:00
|
|
|
platform: '',
|
|
|
|
projectRoot: '',
|
2018-11-09 13:49:04 +00:00
|
|
|
publicPath: '/assets',
|
2017-06-14 16:25:53 +00:00
|
|
|
retainLines: true,
|
2018-04-19 16:53:11 +00:00
|
|
|
sourceType: 'unambiguous', // b7 required. detects module vs script mode
|
2017-05-11 15:32:04 +00:00
|
|
|
},
|
|
|
|
src,
|
2018-04-11 23:29:36 +00:00
|
|
|
plugins: [
|
|
|
|
[require('@babel/plugin-transform-block-scoping')],
|
|
|
|
// the flow strip types plugin must go BEFORE class properties!
|
|
|
|
// there'll be a test case that fails if you don't.
|
|
|
|
[require('@babel/plugin-transform-flow-strip-types')],
|
|
|
|
[
|
|
|
|
require('@babel/plugin-proposal-class-properties'),
|
|
|
|
// use `this.foo = bar` instead of `this.defineProperty('foo', ...)`
|
|
|
|
{loose: true},
|
|
|
|
],
|
|
|
|
[require('@babel/plugin-transform-computed-properties')],
|
|
|
|
[require('@babel/plugin-transform-destructuring')],
|
|
|
|
[require('@babel/plugin-transform-function-name')],
|
|
|
|
[require('@babel/plugin-transform-literals')],
|
|
|
|
[require('@babel/plugin-transform-parameters')],
|
|
|
|
[require('@babel/plugin-transform-shorthand-properties')],
|
|
|
|
[require('@babel/plugin-transform-react-jsx')],
|
|
|
|
[require('@babel/plugin-transform-regenerator')],
|
|
|
|
[require('@babel/plugin-transform-sticky-regex')],
|
|
|
|
[require('@babel/plugin-transform-unicode-regex')],
|
|
|
|
[
|
|
|
|
require('@babel/plugin-transform-modules-commonjs'),
|
|
|
|
{strict: false, allowTopLevelThis: true},
|
|
|
|
],
|
|
|
|
[require('@babel/plugin-transform-classes')],
|
|
|
|
[require('@babel/plugin-transform-arrow-functions')],
|
|
|
|
[require('@babel/plugin-transform-spread')],
|
|
|
|
[require('@babel/plugin-proposal-object-rest-spread')],
|
|
|
|
[
|
|
|
|
require('@babel/plugin-transform-template-literals'),
|
|
|
|
{loose: true}, // dont 'a'.concat('b'), just use 'a'+'b'
|
|
|
|
],
|
|
|
|
[require('@babel/plugin-transform-exponentiation-operator')],
|
|
|
|
[require('@babel/plugin-transform-object-assign')],
|
|
|
|
[require('@babel/plugin-transform-for-of'), {loose: true}],
|
|
|
|
[require('@babel/plugin-transform-react-display-name')],
|
|
|
|
[require('@babel/plugin-transform-react-jsx-source')],
|
|
|
|
],
|
2017-11-22 21:29:42 +00:00
|
|
|
});
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
return generate(
|
|
|
|
ast,
|
|
|
|
{
|
|
|
|
code: true,
|
|
|
|
comments: false,
|
|
|
|
compact: false,
|
|
|
|
filename: file,
|
|
|
|
retainLines: true,
|
|
|
|
sourceFileName: file,
|
|
|
|
sourceMaps: true,
|
|
|
|
},
|
|
|
|
src,
|
|
|
|
).code;
|
2015-09-19 22:20:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getCacheKey: createCacheKeyFunction([
|
|
|
|
__filename,
|
2018-12-14 16:12:44 +00:00
|
|
|
require.resolve('metro-react-native-babel-transformer'),
|
2018-04-11 23:29:36 +00:00
|
|
|
require.resolve('@babel/core/package.json'),
|
2015-09-19 22:20:25 +00:00
|
|
|
]),
|
2015-09-16 17:30:53 +00:00
|
|
|
};
|