Skip to content

Commit 2aefc3d

Browse files
hb-sebagilgur5
authored andcommitted
change: replace async-to-promises with regenerator
- 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
1 parent f6c296a commit 2aefc3d

4 files changed

Lines changed: 17 additions & 38 deletions

File tree

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
"@babel/core": "^7.4.4",
4545
"@babel/helper-module-imports": "^7.0.0",
4646
"@babel/plugin-proposal-class-properties": "^7.4.4",
47-
"@babel/plugin-transform-regenerator": "^7.4.5",
48-
"@babel/polyfill": "^7.4.4",
4947
"@babel/preset-env": "^7.11.0",
5048
"@rollup/plugin-commonjs": "^11.0.0",
5149
"@rollup/plugin-json": "^4.0.0",
@@ -60,12 +58,12 @@
6058
"babel-plugin-annotate-pure-calls": "^0.4.0",
6159
"babel-plugin-dev-expression": "^0.2.1",
6260
"babel-plugin-macros": "^2.6.1",
63-
"babel-plugin-transform-async-to-promises": "^0.8.14",
6461
"babel-plugin-transform-rename-import": "^2.3.0",
6562
"babel-traverse": "^6.26.0",
6663
"babylon": "^6.18.0",
6764
"camelcase": "^6.0.0",
6865
"chalk": "^4.0.0",
66+
"core-js": "^2.6.5",
6967
"enquirer": "^2.3.4",
7068
"eslint": "^6.1.0",
7169
"eslint-config-prettier": "^6.0.0",
@@ -86,6 +84,7 @@
8684
"pascal-case": "^3.1.1",
8785
"prettier": "^1.19.1",
8886
"progress-estimator": "^0.2.2",
87+
"regenerator-runtime": "^0.13.7",
8988
"rollup": "^1.32.1",
9089
"rollup-plugin-babel": "^4.3.2",
9190
"rollup-plugin-sourcemaps": "^0.5.0",

src/babelPluginTsdx.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,10 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
7474
name: 'babel-plugin-transform-rename-import',
7575
replacements,
7676
},
77-
{
78-
name: 'babel-plugin-transform-async-to-promises',
79-
inlineHelpers: true,
80-
externalHelpers: true,
81-
},
8277
{
8378
name: '@babel/plugin-proposal-class-properties',
8479
loose: true,
8580
},
86-
{
87-
name: '@babel/plugin-transform-regenerator',
88-
async: false,
89-
},
9081
isTruthy(customOptions.extractErrors) && {
9182
name: './errors/transformErrorMessages',
9283
},
@@ -110,14 +101,12 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
110101
{
111102
loose: true,
112103
targets: customOptions.targets,
104+
useBuiltIns: 'usage',
105+
corejs: 2,
113106
},
114107
presetEnv.options,
115108
{
116109
modules: false,
117-
exclude: merge(
118-
['transform-async-to-generator', 'transform-regenerator'],
119-
(presetEnv.options && presetEnv.options.exclude) || []
120-
),
121110
}
122111
),
123112
],
@@ -131,9 +120,10 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
131120
{
132121
name: '@babel/preset-env',
133122
targets: customOptions.targets,
123+
useBuiltIns: 'usage',
124+
corejs: 2,
134125
modules: false,
135126
loose: true,
136-
exclude: ['transform-async-to-generator', 'transform-regenerator'],
137127
},
138128
]);
139129

src/createRollupConfig.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ export async function createRollupConfig(
6060
input: opts.input,
6161
// Tell Rollup which packages to ignore
6262
external: (id: string) => {
63-
if (id === 'babel-plugin-transform-async-to-promises/helpers') {
63+
// bundle in any polyfills as TSDX can't control whether polyfills are installed as deps
64+
if (id.startsWith('regenerator-runtime') || id.startsWith('core-js')) {
6465
return false;
6566
}
67+
6668
return external(id);
6769
},
6870
// Rollup has treeshaking by default, but we can optimize it further...
@@ -118,11 +120,12 @@ export async function createRollupConfig(
118120
// defaults + .jsx
119121
extensions: ['.mjs', '.js', '.jsx', '.json', '.node'],
120122
}),
121-
opts.format === 'umd' &&
122-
commonjs({
123-
// use a regex to make sure to include eventual hoisted packages
124-
include: /\/node_modules\//,
125-
}),
123+
commonjs({
124+
// Use a regex to make sure to include eventual hoisted packages (umd).
125+
// Always transform core-js, so its internal dependencies are found
126+
// by rollup's external() resolution.
127+
include: opts.format === 'umd' ? /\/node_modules\// : /core-js\//,
128+
}),
126129
json(),
127130
{
128131
// Custom plugin that removes shebang from code because newer

yarn.lock

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@
643643
dependencies:
644644
"@babel/helper-plugin-utils" "^7.10.4"
645645

646-
"@babel/plugin-transform-regenerator@^7.10.4", "@babel/plugin-transform-regenerator@^7.4.5":
646+
"@babel/plugin-transform-regenerator@^7.10.4":
647647
version "7.10.4"
648648
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-regenerator/-/plugin-transform-regenerator-7.10.4.tgz#2015e59d839074e76838de2159db421966fd8b63"
649649
integrity sha512-3thAHwtor39A7C04XucbMg17RcZ3Qppfxr22wYzZNcVIkPHfpM9J0SO8zuCV6SZa265kxBJSrfKTvDCYqBFXGw==
@@ -720,14 +720,6 @@
720720
"@babel/helper-create-regexp-features-plugin" "^7.10.4"
721721
"@babel/helper-plugin-utils" "^7.10.4"
722722

723-
"@babel/polyfill@^7.4.4":
724-
version "7.7.0"
725-
resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.7.0.tgz#e1066e251e17606ec7908b05617f9b7f8180d8f3"
726-
integrity sha512-/TS23MVvo34dFmf8mwCisCbWGrfhbiWZSwBo6HkADTBhUa2Q/jWltyY/tpofz/b6/RIhqaqQcquptCirqIhOaQ==
727-
dependencies:
728-
core-js "^2.6.5"
729-
regenerator-runtime "^0.13.2"
730-
731723
"@babel/preset-env@^7.11.0":
732724
version "7.11.0"
733725
resolved "https://registry.yarnpkg.com/@babel/preset-env/-/preset-env-7.11.0.tgz#860ee38f2ce17ad60480c2021ba9689393efb796"
@@ -1909,11 +1901,6 @@ babel-plugin-syntax-jsx@^6.18.0:
19091901
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
19101902
integrity sha1-CvMqmm4Tyno/1QaeYtew9Y0NiUY=
19111903

1912-
babel-plugin-transform-async-to-promises@^0.8.14:
1913-
version "0.8.15"
1914-
resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-promises/-/babel-plugin-transform-async-to-promises-0.8.15.tgz#13b6d8ef13676b4e3c576d3600b85344bb1ba346"
1915-
integrity sha512-fDXP68ZqcinZO2WCiimCL9zhGjGXOnn3D33zvbh+yheZ/qOrNVVDDIBtAaM3Faz8TRvQzHiRKsu3hfrBAhEncQ==
1916-
19171904
babel-plugin-transform-rename-import@^2.3.0:
19181905
version "2.3.0"
19191906
resolved "https://registry.yarnpkg.com/babel-plugin-transform-rename-import/-/babel-plugin-transform-rename-import-2.3.0.tgz#5d9d645f937b0ca5c26a24b2510a06277b6ffd9b"
@@ -6961,7 +6948,7 @@ regenerator-runtime@^0.11.0:
69616948
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
69626949
integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==
69636950

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

0 commit comments

Comments
 (0)