Skip to content

Commit

Permalink
fix(babelrc): Provide version to runtime transform config
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Aug 21, 2019
1 parent 313d1af commit 16affba
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 21 deletions.
69 changes: 55 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@babel/runtime": "^7.5.5",
"@babel/runtime-corejs2": "^7.5.5",
"@babel/runtime-corejs3": "^7.5.5",
"babel-core": "^7.0.0-bridge.0",
"builtin-modules": "^3.1.0",
"camelcase": "^5.3.1",
Expand All @@ -61,6 +64,7 @@
"p-map": "^3.0.0",
"prettier": "^1.18.2",
"prettier-config-zillow": "^1.1.1",
"resolve-from": "^5.0.0",
"rollup": "^1.19.3",
"tacks": "^1.3.0",
"tempy": "^0.3.0",
Expand Down
16 changes: 16 additions & 0 deletions packages/pectin-babelrc/lib/pectin-babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');
const cloneDeep = require('clone-deep');
const cosmiconfig = require('cosmiconfig');
const resolveFrom = require('resolve-from');

const explorer = cosmiconfig('babel', {
// we cannot cache transform because per-package dependencies affect result
Expand All @@ -28,6 +29,18 @@ function hasAdvancedTransform(plugin) {
return Array.isArray(plugin) && isRuntimeTransform(plugin[0]);
}

// @see https://github.com/babel/babel/issues/10261
// @see https://github.com/babel/babel/pull/10325
function resolveDependencyVersion(cwd, depName) {
// we can't do a straight-up `require('@babel/runtime/package.json')`
// because that doesn't respect the target package's cwd
const pkgPath = resolveFrom(cwd, `${depName}/package.json`);

// istanbul ignore next: undefined doesn't matter, we tried our best
// eslint-disable-next-line global-require, zillow/import/no-dynamic-require
return pkgPath ? require(pkgPath).version : undefined;
}

function ensureRuntimeHelpers(rc, entryOptions) {
if (rc.plugins.some(hasSimpleTransform)) {
const idx = rc.plugins.findIndex(hasSimpleTransform);
Expand Down Expand Up @@ -76,15 +89,18 @@ module.exports = async function pectinBabelrc(pkg, cwd, output) {
if (deps.has('@babel/runtime')) {
ensureRuntimeHelpers(rc, {
useESModules: format === 'esm',
version: resolveDependencyVersion(cwd, '@babel/runtime'),
});
} else if (deps.has('@babel/runtime-corejs2')) {
ensureRuntimeHelpers(rc, {
useESModules: format === 'esm',
version: resolveDependencyVersion(cwd, '@babel/runtime-corejs2'),
corejs: 2,
});
} else if (deps.has('@babel/runtime-corejs3')) {
ensureRuntimeHelpers(rc, {
useESModules: format === 'esm',
version: resolveDependencyVersion(cwd, '@babel/runtime-corejs3'),
corejs: 3,
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/pectin-babelrc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.2.0",
"clone-deep": "^4.0.1",
"cosmiconfig": "^5.2.0"
"cosmiconfig": "^5.2.0",
"resolve-from": "^5.0.0"
}
}
39 changes: 33 additions & 6 deletions packages/pectin-babelrc/test/pectin-babelrc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ const Tacks = require('tacks');
const tempy = require('tempy');
const pectinBabelrc = require('../');

const { Dir, File } = Tacks;
const { Dir, File, Symlink } = Tacks;

// spicy symlink prep
const REPO_ROOT = path.resolve('.');
const BABEL_RUNTIME_DEFAULT_VERSION = require('@babel/runtime/package.json').version;
const BABEL_RUNTIME_COREJS2_VERSION = require('@babel/runtime-corejs2/package.json').version;
const BABEL_RUNTIME_COREJS3_VERSION = require('@babel/runtime-corejs3/package.json').version;

function createFixture(spec) {
const cwd = tempy.directory();

new Tacks(Dir({ ...spec })).create(cwd);
new Tacks(
Dir({
// spicy symlink necessary due to potential runtime package resolution
node_modules: Symlink(path.relative(cwd, path.join(REPO_ROOT, 'node_modules'))),
...spec,
})
).create(cwd);

return cwd;
}
Expand Down Expand Up @@ -240,7 +252,10 @@ describe('pectin-babelrc', () => {

expect(opts).toHaveProperty('plugins', [
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-runtime', { useESModules: true }],
[
'@babel/plugin-transform-runtime',
{ useESModules: true, version: BABEL_RUNTIME_DEFAULT_VERSION },
],
'lodash',
]);
});
Expand Down Expand Up @@ -277,6 +292,7 @@ describe('pectin-babelrc', () => {
{
corejs: true,
useESModules: true,
version: BABEL_RUNTIME_DEFAULT_VERSION,
},
],
]);
Expand All @@ -301,7 +317,10 @@ describe('pectin-babelrc', () => {

expect(opts).toHaveProperty('plugins', [
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-runtime', { useESModules: true }],
[
'@babel/plugin-transform-runtime',
{ useESModules: true, version: BABEL_RUNTIME_DEFAULT_VERSION },
],
]);
});

Expand All @@ -322,7 +341,10 @@ describe('pectin-babelrc', () => {

expect(opts).toHaveProperty('plugins', [
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-runtime', { useESModules: true, corejs: 2 }],
[
'@babel/plugin-transform-runtime',
{ useESModules: true, corejs: 2, version: BABEL_RUNTIME_COREJS2_VERSION },
],
]);
});

Expand All @@ -343,7 +365,10 @@ describe('pectin-babelrc', () => {

expect(opts).toHaveProperty('plugins', [
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-runtime', { useESModules: true, corejs: 3 }],
[
'@babel/plugin-transform-runtime',
{ useESModules: true, corejs: 3, version: BABEL_RUNTIME_COREJS3_VERSION },
],
]);
});

Expand Down Expand Up @@ -584,6 +609,7 @@ describe('pectin-babelrc', () => {
"@babel/plugin-transform-runtime",
Object {
"useESModules": false,
"version": "${BABEL_RUNTIME_DEFAULT_VERSION}",
},
],
],
Expand Down Expand Up @@ -614,6 +640,7 @@ describe('pectin-babelrc', () => {
"@babel/plugin-transform-runtime",
Object {
"useESModules": true,
"version": "${BABEL_RUNTIME_DEFAULT_VERSION}",
},
],
],
Expand Down

0 comments on commit 16affba

Please sign in to comment.