Skip to content

Commit

Permalink
change: replace async-to-promises with regenerator
Browse files Browse the repository at this point in the history
- as an alternative to transpile async functions because
  babel-plugin-transform-async-to-promises has several
  correctness / transpilation bugs and has been unmaintained for a
  while now

- Add useBuiltIns option to automatically add regeneratorRuntime imports
  if needed
- Remove @babel/plugin-transform-regenerator and custom merging logic
  - no longer needed because we now _are_ using it for async and so can
    use the default options
  - the default options are already included with @babel/preset-env, so
    this is now an extraneous package

- Replace deprecated @babel/polyfill with direct dependencies to
  regenerator-runtime and (already installed) core-js
  - and specify the corejs version in the preset-env config to resolve
    a warning during `tsdx build`
    - warning says that matching core-js version should be installed
      and specified explicitly with useBuiltIns, because the default
      version of 2.x is "likely to change"
- Add regenerator-runtime and core-js to external, so that the polyfills
  are included in the bundle
- Always transform core-js to ESM, to fix its own internal, transitive
  dependencies not being found by the current (naive) external algorithm
  • Loading branch information
hb-seb authored and agilgur5 committed Sep 20, 2020
1 parent f6c296a commit 2aefc3d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 38 deletions.
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -44,8 +44,6 @@
"@babel/core": "^7.4.4",
"@babel/helper-module-imports": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/plugin-transform-regenerator": "^7.4.5",
"@babel/polyfill": "^7.4.4",
"@babel/preset-env": "^7.11.0",
"@rollup/plugin-commonjs": "^11.0.0",
"@rollup/plugin-json": "^4.0.0",
Expand All @@ -60,12 +58,12 @@
"babel-plugin-annotate-pure-calls": "^0.4.0",
"babel-plugin-dev-expression": "^0.2.1",
"babel-plugin-macros": "^2.6.1",
"babel-plugin-transform-async-to-promises": "^0.8.14",
"babel-plugin-transform-rename-import": "^2.3.0",
"babel-traverse": "^6.26.0",
"babylon": "^6.18.0",
"camelcase": "^6.0.0",
"chalk": "^4.0.0",
"core-js": "^2.6.5",
"enquirer": "^2.3.4",
"eslint": "^6.1.0",
"eslint-config-prettier": "^6.0.0",
Expand All @@ -86,6 +84,7 @@
"pascal-case": "^3.1.1",
"prettier": "^1.19.1",
"progress-estimator": "^0.2.2",
"regenerator-runtime": "^0.13.7",
"rollup": "^1.32.1",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-sourcemaps": "^0.5.0",
Expand Down
18 changes: 4 additions & 14 deletions src/babelPluginTsdx.ts
Expand Up @@ -74,19 +74,10 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
name: 'babel-plugin-transform-rename-import',
replacements,
},
{
name: 'babel-plugin-transform-async-to-promises',
inlineHelpers: true,
externalHelpers: true,
},
{
name: '@babel/plugin-proposal-class-properties',
loose: true,
},
{
name: '@babel/plugin-transform-regenerator',
async: false,
},
isTruthy(customOptions.extractErrors) && {
name: './errors/transformErrorMessages',
},
Expand All @@ -110,14 +101,12 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
{
loose: true,
targets: customOptions.targets,
useBuiltIns: 'usage',
corejs: 2,
},
presetEnv.options,
{
modules: false,
exclude: merge(
['transform-async-to-generator', 'transform-regenerator'],
(presetEnv.options && presetEnv.options.exclude) || []
),
}
),
],
Expand All @@ -131,9 +120,10 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
{
name: '@babel/preset-env',
targets: customOptions.targets,
useBuiltIns: 'usage',
corejs: 2,
modules: false,
loose: true,
exclude: ['transform-async-to-generator', 'transform-regenerator'],
},
]);

Expand Down
15 changes: 9 additions & 6 deletions src/createRollupConfig.ts
Expand Up @@ -60,9 +60,11 @@ export async function createRollupConfig(
input: opts.input,
// Tell Rollup which packages to ignore
external: (id: string) => {
if (id === 'babel-plugin-transform-async-to-promises/helpers') {
// bundle in any polyfills as TSDX can't control whether polyfills are installed as deps
if (id.startsWith('regenerator-runtime') || id.startsWith('core-js')) {
return false;
}

return external(id);
},
// Rollup has treeshaking by default, but we can optimize it further...
Expand Down Expand Up @@ -118,11 +120,12 @@ export async function createRollupConfig(
// defaults + .jsx
extensions: ['.mjs', '.js', '.jsx', '.json', '.node'],
}),
opts.format === 'umd' &&
commonjs({
// use a regex to make sure to include eventual hoisted packages
include: /\/node_modules\//,
}),
commonjs({
// Use a regex to make sure to include eventual hoisted packages (umd).
// Always transform core-js, so its internal dependencies are found
// by rollup's external() resolution.
include: opts.format === 'umd' ? /\/node_modules\// : /core-js\//,
}),
json(),
{
// Custom plugin that removes shebang from code because newer
Expand Down
17 changes: 2 additions & 15 deletions yarn.lock
Expand Up @@ -643,7 +643,7 @@
dependencies:
"@babel/helper-plugin-utils" "^7.10.4"

"@babel/plugin-transform-regenerator@^7.10.4", "@babel/plugin-transform-regenerator@^7.4.5":
"@babel/plugin-transform-regenerator@^7.10.4":
version "7.10.4"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-regenerator/-/plugin-transform-regenerator-7.10.4.tgz#2015e59d839074e76838de2159db421966fd8b63"
integrity sha512-3thAHwtor39A7C04XucbMg17RcZ3Qppfxr22wYzZNcVIkPHfpM9J0SO8zuCV6SZa265kxBJSrfKTvDCYqBFXGw==
Expand Down Expand Up @@ -720,14 +720,6 @@
"@babel/helper-create-regexp-features-plugin" "^7.10.4"
"@babel/helper-plugin-utils" "^7.10.4"

"@babel/polyfill@^7.4.4":
version "7.7.0"
resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.7.0.tgz#e1066e251e17606ec7908b05617f9b7f8180d8f3"
integrity sha512-/TS23MVvo34dFmf8mwCisCbWGrfhbiWZSwBo6HkADTBhUa2Q/jWltyY/tpofz/b6/RIhqaqQcquptCirqIhOaQ==
dependencies:
core-js "^2.6.5"
regenerator-runtime "^0.13.2"

"@babel/preset-env@^7.11.0":
version "7.11.0"
resolved "https://registry.yarnpkg.com/@babel/preset-env/-/preset-env-7.11.0.tgz#860ee38f2ce17ad60480c2021ba9689393efb796"
Expand Down Expand Up @@ -1909,11 +1901,6 @@ babel-plugin-syntax-jsx@^6.18.0:
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
integrity sha1-CvMqmm4Tyno/1QaeYtew9Y0NiUY=

babel-plugin-transform-async-to-promises@^0.8.14:
version "0.8.15"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-promises/-/babel-plugin-transform-async-to-promises-0.8.15.tgz#13b6d8ef13676b4e3c576d3600b85344bb1ba346"
integrity sha512-fDXP68ZqcinZO2WCiimCL9zhGjGXOnn3D33zvbh+yheZ/qOrNVVDDIBtAaM3Faz8TRvQzHiRKsu3hfrBAhEncQ==

babel-plugin-transform-rename-import@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-rename-import/-/babel-plugin-transform-rename-import-2.3.0.tgz#5d9d645f937b0ca5c26a24b2510a06277b6ffd9b"
Expand Down Expand Up @@ -6961,7 +6948,7 @@ regenerator-runtime@^0.11.0:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==

regenerator-runtime@^0.13.2, regenerator-runtime@^0.13.4:
regenerator-runtime@^0.13.2, regenerator-runtime@^0.13.4, regenerator-runtime@^0.13.7:
version "0.13.7"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz#cac2dacc8a1ea675feaabaeb8ae833898ae46f55"
integrity sha512-a54FxoJDIr27pgf7IgeQGxmqUNYrcV338lf/6gH456HZ/PhX+5BcwHXG9ajESmwe6WRO0tAzRUrRmNONWgkrew==
Expand Down

0 comments on commit 2aefc3d

Please sign in to comment.