Skip to content

Commit

Permalink
Use babel runtime instead of relying on global babelHelpers and regen…
Browse files Browse the repository at this point in the history
…erator
  • Loading branch information
janicduplessis committed Aug 3, 2018
1 parent d7ddbba commit d717970
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/metro-react-native-babel-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@babel/plugin-transform-react-jsx": "7.0.0-beta.54",
"@babel/plugin-transform-react-jsx-source": "7.0.0-beta.54",
"@babel/plugin-transform-regenerator": "7.0.0-beta.54",
"@babel/plugin-transform-runtime": "7.0.0-beta.54",
"@babel/plugin-transform-shorthand-properties": "7.0.0-beta.54",
"@babel/plugin-transform-spread": "7.0.0-beta.54",
"@babel/plugin-transform-sticky-regex": "7.0.0-beta.54",
Expand Down
8 changes: 8 additions & 0 deletions packages/metro-react-native-babel-preset/src/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ const defaultPlugins = [
[require('@babel/plugin-transform-shorthand-properties')],
[require('@babel/plugin-transform-react-jsx')],
[require('@babel/plugin-transform-regenerator')],
[
require('@babel/plugin-transform-runtime'),
{
helpers: true,
polyfill: false,
regenerator: true,
},
],
[require('@babel/plugin-transform-sticky-regex')],
[require('@babel/plugin-transform-unicode-regex')],
[
Expand Down
1 change: 1 addition & 0 deletions packages/metro-react-native-babel-preset/src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
'@babel/plugin-transform-react-jsx': require('@babel/plugin-transform-react-jsx'),
'@babel/plugin-transform-react-jsx-source': require('@babel/plugin-transform-react-jsx-source'),
'@babel/plugin-transform-regenerator': require('@babel/plugin-transform-regenerator'),
'@babel/plugin-transform-runtime': require('@babel/plugin-transform-runtime'),
'@babel/plugin-transform-spread': require('@babel/plugin-transform-spread'),
'@babel/plugin-transform-sticky-regex': require('@babel/plugin-transform-sticky-regex'),
'@babel/plugin-transform-unicode-regex': require('@babel/plugin-transform-unicode-regex'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`code transformation worker: reports filename when encountering unsupported dynamic dependency 1`] = `"/root/local/file.js:3:10: calls to \`require\` expect exactly 1 string literal argument, but this was found: \`require(a)\`."`;

exports[`code transformation worker: transforms an es module with regenerator 1`] = `
"__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {
var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], \\"@babel/runtime/helpers/interopRequireDefault\\");
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
exports.test = test;
var _regenerator = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], \\"@babel/runtime/regenerator\\"));
function test() {
return _regenerator.default.async(function test$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
case \\"end\\":
return _context.stop();
}
}
}, null, this);
}
});"
`;
48 changes: 45 additions & 3 deletions packages/metro/src/JSTransformer/worker/__tests__/worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,66 @@ describe('code transformation worker:', () => {
'__d(function (global, _$$_REQUIRE, module, exports, _dependencyMap) {',
" 'use strict';",
'',
' var _c = babelHelpers.interopRequireDefault(_$$_REQUIRE(_dependencyMap[0], "./c"));',
' var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], "@babel/runtime/helpers/interopRequireDefault");',
'',
' _$$_REQUIRE(_dependencyMap[1], "./a");',
' var _c = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], "./c"));',
'',
' _$$_REQUIRE(_dependencyMap[2], "./a");',
'',
' arbitrary(code);',
'',
' var b = _$$_REQUIRE(_dependencyMap[2], "b");',
' var b = _$$_REQUIRE(_dependencyMap[3], "b");',
'});',
].join('\n'),
);
expect(result.output[0].data.map).toHaveLength(14);
expect(result.dependencies).toEqual([
{
isAsync: false,
name: '@babel/runtime/helpers/interopRequireDefault',
},
{isAsync: false, name: './c'},
{isAsync: false, name: './a'},
{isAsync: false, name: 'b'},
]);
});

it('transforms an es module with regenerator', async () => {
fs.writeFileSync(
'/root/local/file.js',
['export async function test() {}'].join('\n'),
);

const {result} = await transformCode(
'/root/local/file.js',
'local/file.js',
transformerPath,
{
dev: true,
transform: {},
},
[],
'',
'minifyModulePath',
'asyncRequire',
'reject',
);

expect(result.output[0].type).toBe('js/module');
expect(result.output[0].data.code).toMatchSnapshot();
expect(result.output[0].data.map).toHaveLength(13);
expect(result.dependencies).toEqual([
{
isAsync: false,
name: '@babel/runtime/helpers/interopRequireDefault',
},
{
isAsync: false,
name: '@babel/runtime/regenerator',
},
]);
});

it('reports filename when encountering unsupported dynamic dependency', async () => {
fs.writeFileSync(
'/root/local/file.js',
Expand Down
11 changes: 3 additions & 8 deletions packages/metro/src/lib/polyfills/__tests__/require-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ const fs = require('fs');

const {transformSync} = require('@babel/core');

// Include the external-helpers plugin to be able to detect if they're
// needed when transforming the requirejs implementation.
const PLUGINS = ['@babel/plugin-external-helpers'];

function createModule(
moduleSystem,
moduleId,
Expand All @@ -34,7 +30,6 @@ describe('require', () => {
return transformSync(rawCode, {
ast: false,
babelrc: false,
plugins: PLUGINS.map(require),
presets: [require.resolve('metro-react-native-babel-preset')],
retainLines: true,
sourceMaps: 'inline',
Expand All @@ -56,9 +51,9 @@ describe('require', () => {
});

it('does not need any babel helper logic', () => {
// Super-simple check to validate that no babel helpers are used.
// This check will need to be updated if https://fburl.com/6z0y2kf8 changes.
expect(moduleSystemCode.includes('babelHelpers')).toBe(false);
// The react native preset uses @babel/transform-runtime so helpers will be
// imported from @babel/runtime.
expect(moduleSystemCode.includes('@babel/runtime')).toBe(false);
});

it('works with plain bundles', () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/metro/src/reactNativeTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
'use strict';

const crypto = require('crypto');
const externalHelpersPlugin = require('babel-plugin-external-helpers');
const fs = require('fs');
const inlineRequiresPlugin = require('babel-preset-fbjs/plugins/inline-requires');
const json5 = require('json5');
Expand All @@ -28,7 +27,6 @@ type ModuleES6 = {__esModule?: boolean, default?: {}};

const cacheKeyParts = [
fs.readFileSync(__filename),
require('babel-plugin-external-helpers/package.json').version,
require('babel-preset-fbjs/package.json').version,
];

Expand Down Expand Up @@ -127,7 +125,7 @@ function buildBabelConfig(filename, options, plugins?: BabelPlugins = []) {
let config = Object.assign({}, babelRC, extraConfig);

// Add extra plugins
const extraPlugins = [externalHelpersPlugin];
const extraPlugins = [];

if (options.inlineRequires) {
extraPlugins.push(inlineRequiresPlugin);
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,22 @@
dependencies:
"@babel/helper-plugin-utils" "7.0.0-beta.54"

<<<<<<< HEAD
"@babel/[email protected]":
version "7.0.0-beta.54"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-spread/-/plugin-transform-spread-7.0.0-beta.54.tgz#4f0852df0f4b1db2426c40facd8fe5f028a3dbc9"
=======
"@babel/[email protected]":
version "7.0.0-beta.48"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.0.0-beta.48.tgz#166ceb0a96948507c851edbd9e9c5aa98f8ead71"
dependencies:
"@babel/helper-module-imports" "7.0.0-beta.48"
"@babel/helper-plugin-utils" "7.0.0-beta.48"

"@babel/[email protected]":
version "7.0.0-beta.48"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.0.0-beta.48.tgz#5cc1c14bbc4f5132e5bd1ecf7ff3e9adeb9dfe35"
>>>>>>> Use babel runtime instead of relying on global babelHelpers and regenerator
dependencies:
"@babel/helper-plugin-utils" "7.0.0-beta.54"

Expand Down Expand Up @@ -5152,9 +5165,15 @@ regenerator-runtime@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.0.tgz#7e54fe5b5ccd5d6624ea6255c3473be090b802e1"

<<<<<<< HEAD
regenerator-transform@^0.13.3:
version "0.13.3"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.13.3.tgz#264bd9ff38a8ce24b06e0636496b2c856b57bcbb"
=======
regenerator-transform@^0.12.3:
version "0.12.4"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.12.4.tgz#aa9b6c59f4b97be080e972506c560b3bccbfcff0"
>>>>>>> Use babel runtime instead of relying on global babelHelpers and regenerator
dependencies:
private "^0.1.6"

Expand Down

0 comments on commit d717970

Please sign in to comment.