Skip to content

Commit

Permalink
Improved Babel config.
Browse files Browse the repository at this point in the history
Includes a workaround for babel/babel#8462 .
  • Loading branch information
jaydenseric committed Mar 22, 2020
1 parent 5ae172e commit 7185b9d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"name": "Browser",
"path": "size-limit-entries/browser.mjs",
"limit": "2.5 KB",
"ignore": ["prop-types", "object-assign"]
"ignore": ["prop-types"]
}
]
26 changes: 26 additions & 0 deletions babelPluginAddBabelRuntimeFileExtensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

/**
* A Babel plugin that adds missing `.js` file extensions to Babel runtime
* import specifiers.
* @param {object} babel Current Babel object.
* @returns {object} Babel plugin object.
* @see [babel/babel#8462](https://github.com/babel/babel/issues/8462).
* @ignore
*/
module.exports = function babelPluginAddBabelRuntimeFileExtensions({ types }) {
return {
visitor: {
'ImportDeclaration|ExportNamedDeclaration'(path) {
if (
path.node.source &&
path.node.source.value.startsWith('@babel/runtime/helpers/') &&
!path.node.source.value.endsWith('.js')
)
path
.get('source')
.replaceWith(types.stringLiteral(`${path.node.source.value}.js`))
}
}
}
}
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Patch

- Updated tests for compatibility with updated dependencies.
- Removed the [`object-assign`](https://npm.im/object-assign) dependency and several Babel dev dependencies after simplifying the Babel config.
- Added a new [`babel-plugin-transform-require-extensions`](https://npm.im/babel-plugin-transform-require-extensions) dev dependency and ensured ESM import specifiers in both source and published `.mjs` files contain file names with extensions, which [are mandatory in the final Node.js ESM implementation](https://nodejs.org/api/esm.html#esm_mandatory_file_extensions). Published CJS `.js` files now also have file extensions in `require` paths.
- Stop using [`husky`](https://npm.im/husky) and [`lint-staged`](https://npm.im/lint-staged).
- Tidied Babel configs.
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,17 @@
"extract-files": "^7.0.0",
"fnv1a": "^1.0.1",
"mitt": "^1.1.3",
"object-assign": "^4.1.1",
"prop-types": "^15.7.2"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.9.0",
"@babel/plugin-transform-runtime": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@babel/preset-react": "^7.9.1",
"@size-limit/preset-small-lib": "^4.4.0",
"babel-eslint": "^10.1.0",
"babel-plugin-transform-replace-object-assign": "^2.0.0",
"babel-plugin-transform-require-extensions": "^2.0.0",
"cross-fetch": "^3.0.4",
"eslint": "^6.8.0",
Expand Down
7 changes: 6 additions & 1 deletion src/server/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict'

const plugins = ['@babel/transform-runtime', 'transform-require-extensions']

if (process.env.BABEL_ESM)
plugins.push(require('../../babelPluginAddBabelRuntimeFileExtensions.js'))

module.exports = {
comments: false,
plugins: ['@babel/transform-runtime', 'transform-require-extensions'],
plugins,
presets: [
[
'@babel/env',
Expand Down
7 changes: 6 additions & 1 deletion src/test/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict'

const plugins = ['@babel/transform-runtime', 'transform-require-extensions']

if (process.env.BABEL_ESM)
plugins.push(require('../../babelPluginAddBabelRuntimeFileExtensions.js'))

module.exports = {
comments: false,
plugins: ['@babel/transform-runtime', 'transform-require-extensions'],
plugins,
presets: [
[
'@babel/env',
Expand Down
19 changes: 11 additions & 8 deletions src/universal/.babelrc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict'

const plugins = [
['@babel/proposal-class-properties', { loose: true }],
'@babel/transform-runtime',
'transform-require-extensions'
]

if (process.env.BABEL_ESM)
plugins.push(require('../../babelPluginAddBabelRuntimeFileExtensions.js'))

module.exports = {
comments: false,
plugins: [
['@babel/proposal-object-rest-spread', { loose: true, useBuiltIns: true }],
['@babel/proposal-class-properties', { loose: true }],
'@babel/transform-runtime',
'transform-require-extensions'
],
plugins,
presets: [
{ plugins: ['transform-replace-object-assign'] },
[
'@babel/env',
{
Expand All @@ -19,6 +22,6 @@ module.exports = {
loose: true
}
],
['@babel/react', { useBuiltIns: true }]
'@babel/react'
]
}

0 comments on commit 7185b9d

Please sign in to comment.