Skip to content

Commit

Permalink
change: replace useBuiltIns with polyfill-regenerator
Browse files Browse the repository at this point in the history
- preset-env's useBuiltIns isn't pure, so it pollutes the global env
  when used
  - TSDX is used by libraries, and libraries really shouldn't pollute
    their consumers' global envs
  - so use the fairly recent babel-plugin-polyfill-regenerator with its
    usage-pure option instead

- since we're not using useBuiltIns, we don't need core-js either, and
  can remove it, reverting back to the previous behavior
  - this ensures that the PR is less breaking and does not really
    introduce new behavior, just replaces async-to-promises with
    regenerator
  - useBuiltIns was previously unset, i.e. defaulted to false, so
    @babel/polyfill was an extraneous package and we don't need to add
    that back

- change the rollup-plugin-commonjs usage to account for existence of
  bundled regenerator-runtime instead of core-js
  - core-js is removed now. not sure why this didn't error / wasn't
    needed for regenerator-runtime with useBuiltIns
    - existing generator test was erroring without this due to CJS usage
  - also add a comment to explain the plugin's usage

- ensure that customOptions.targets is passed into
  polyfill-regenerator's config so it's the same targets as preset-env
  - so there's some work/discussion upstream in Babel on consolidating
    targets, but maybe we should have polyfill-regenerator inherit
    preset-env's targets if it were customized...
    - problem with that is that it may be breaking once we switch
      babelPluginTsdx to an actual preset...

- modify existing generator integration test to account for the fact
  that regenerator is bundled automatically now
  - @babel/plugin-transform-runtime is no longer useful for this purpose
    and will be removed in the next commit during a small refactor
  • Loading branch information
agilgur5 committed Sep 20, 2020
1 parent 2aefc3d commit 6e405d5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,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-polyfill-regenerator": "^0.0.4",
"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 Down
10 changes: 6 additions & 4 deletions src/babelPluginTsdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
name: 'babel-plugin-transform-rename-import',
replacements,
},
{
name: 'babel-plugin-polyfill-regenerator',
// don't pollute global env as this is being used in a library
method: 'usage-pure',
targets: customOptions.targets,
},
{
name: '@babel/plugin-proposal-class-properties',
loose: true,
Expand Down Expand Up @@ -101,8 +107,6 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
{
loose: true,
targets: customOptions.targets,
useBuiltIns: 'usage',
corejs: 2,
},
presetEnv.options,
{
Expand All @@ -120,8 +124,6 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
{
name: '@babel/preset-env',
targets: customOptions.targets,
useBuiltIns: 'usage',
corejs: 2,
modules: false,
loose: true,
},
Expand Down
14 changes: 8 additions & 6 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export async function createRollupConfig(
input: opts.input,
// Tell Rollup which packages to ignore
external: (id: string) => {
// bundle in any polyfills as TSDX can't control whether polyfills are installed as deps
if (id.startsWith('regenerator-runtime') || id.startsWith('core-js')) {
// bundle in polyfills as TSDX can't (yet) ensure installed as deps
if (id.startsWith('regenerator-runtime')) {
return false;
}

Expand Down Expand Up @@ -120,11 +120,13 @@ export async function createRollupConfig(
// defaults + .jsx
extensions: ['.mjs', '.js', '.jsx', '.json', '.node'],
}),
// all bundled external modules need to be converted from CJS to ESM
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\//,
// use a regex to make sure to include eventual hoisted packages
include:
opts.format === 'umd'
? /\/node_modules\//
: /\/regenerator-runtime\//,
}),
json(),
{
Expand Down
4 changes: 2 additions & 2 deletions test/integration/tsdx-build-withBabel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ describe('integration :: tsdx build :: .babelrc.js', () => {
expect(matched).toBeFalsy();
});

it('should add an import of regeneratorRuntime', () => {
it('should bundle regeneratorRuntime', () => {
const output = execWithCache('node ../dist/index.js build');
expect(output.code).toBe(0);

const matched = grep(/@babel\/runtime\/regenerator/, [
const matched = grep(/regeneratorRuntime = r/, [
'dist/build-withbabel.*.js',
]);
expect(matched).toBeTruthy();
Expand Down
68 changes: 47 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
semver "^5.4.1"
source-map "^0.5.0"

"@babel/generator@^7.11.0", "@babel/generator@^7.9.0":
version "7.11.4"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.11.4.tgz#1ec7eec00defba5d6f83e50e3ee72ae2fee482be"
integrity sha512-Rn26vueFx0eOoz7iifCN2UHT6rGtnkSGWSoDRIy8jZN3B91PzeSULbswfLoOWuTuAcNwpG/mxy+uCTDnZ9Mp1g==
"@babel/generator@^7.11.0", "@babel/generator@^7.11.5", "@babel/generator@^7.9.0":
version "7.11.6"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.11.6.tgz#b868900f81b163b4d464ea24545c61cbac4dc620"
integrity sha512-DWtQ1PV3r+cLbySoHrwn9RWEgKMBLLma4OBQloPRyDYvc5msJM9kvTLo1YnlJd1P/ZuKbdli3ijr5q3FvAF3uA==
dependencies:
"@babel/types" "^7.11.0"
"@babel/types" "^7.11.5"
jsesc "^2.5.1"
source-map "^0.5.0"

Expand Down Expand Up @@ -105,6 +105,20 @@
"@babel/types" "^7.10.5"
lodash "^4.17.19"

"@babel/helper-define-polyfill-provider@^0.0.3":
version "0.0.3"
resolved "https://registry.yarnpkg.com/@babel/helper-define-polyfill-provider/-/helper-define-polyfill-provider-0.0.3.tgz#df9da66285b884ce66417abdd0b6ca91198149bd"
integrity sha512-dULDd/APiP4JowYDAMosecKOi/1v+UId99qhBGiO3myM29KtAVKS/R3x3OJJNBR0FeYB1BcYb2dCwkhqvxWXXQ==
dependencies:
"@babel/helper-compilation-targets" "^7.10.4"
"@babel/helper-module-imports" "^7.10.4"
"@babel/helper-plugin-utils" "^7.10.4"
"@babel/traverse" "^7.11.5"
debug "^4.1.1"
lodash.debounce "^4.0.8"
resolve "^1.14.2"
semver "^6.1.2"

"@babel/helper-explode-assignable-expression@^7.10.4":
version "7.11.4"
resolved "https://registry.yarnpkg.com/@babel/helper-explode-assignable-expression/-/helper-explode-assignable-expression-7.11.4.tgz#2d8e3470252cc17aba917ede7803d4a7a276a41b"
Expand Down Expand Up @@ -256,10 +270,10 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@babel/parser@^7.0.0", "@babel/parser@^7.1.0", "@babel/parser@^7.10.4", "@babel/parser@^7.11.0", "@babel/parser@^7.7.5", "@babel/parser@^7.9.0":
version "7.11.4"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.11.4.tgz#6fa1a118b8b0d80d0267b719213dc947e88cc0ca"
integrity sha512-MggwidiH+E9j5Sh8pbrX5sJvMcsqS5o+7iB42M9/k0CD63MjYbdP4nhSh7uB5wnv2/RVzTZFTxzF/kIa5mrCqA==
"@babel/parser@^7.0.0", "@babel/parser@^7.1.0", "@babel/parser@^7.10.4", "@babel/parser@^7.11.0", "@babel/parser@^7.11.5", "@babel/parser@^7.7.5", "@babel/parser@^7.9.0":
version "7.11.5"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.11.5.tgz#c7ff6303df71080ec7a4f5b8c003c58f1cf51037"
integrity sha512-X9rD8qqm695vgmeaQ4fvz/o3+Wk4ZzQvSHkDBgpYKxpD4qTAUm88ZKtHkVqIOsYFFbIQ6wQYhC6q7pjqVK0E0Q==

"@babel/plugin-proposal-async-generator-functions@^7.10.4":
version "7.10.5"
Expand Down Expand Up @@ -829,25 +843,25 @@
"@babel/parser" "^7.10.4"
"@babel/types" "^7.10.4"

"@babel/traverse@^7.0.0", "@babel/traverse@^7.1.0", "@babel/traverse@^7.10.4", "@babel/traverse@^7.4.5", "@babel/traverse@^7.7.4", "@babel/traverse@^7.9.0":
version "7.11.0"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.11.0.tgz#9b996ce1b98f53f7c3e4175115605d56ed07dd24"
integrity sha512-ZB2V+LskoWKNpMq6E5UUCrjtDUh5IOTAyIl0dTjIEoXum/iKWkoIEKIRDnUucO6f+2FzNkE0oD4RLKoPIufDtg==
"@babel/traverse@^7.0.0", "@babel/traverse@^7.1.0", "@babel/traverse@^7.10.4", "@babel/traverse@^7.11.5", "@babel/traverse@^7.4.5", "@babel/traverse@^7.7.4", "@babel/traverse@^7.9.0":
version "7.11.5"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.11.5.tgz#be777b93b518eb6d76ee2e1ea1d143daa11e61c3"
integrity sha512-EjiPXt+r7LiCZXEfRpSJd+jUMnBd4/9OUv7Nx3+0u9+eimMwJmG0Q98lw4/289JCoxSE8OolDMNZaaF/JZ69WQ==
dependencies:
"@babel/code-frame" "^7.10.4"
"@babel/generator" "^7.11.0"
"@babel/generator" "^7.11.5"
"@babel/helper-function-name" "^7.10.4"
"@babel/helper-split-export-declaration" "^7.11.0"
"@babel/parser" "^7.11.0"
"@babel/types" "^7.11.0"
"@babel/parser" "^7.11.5"
"@babel/types" "^7.11.5"
debug "^4.1.0"
globals "^11.1.0"
lodash "^4.17.19"

"@babel/types@^7.0.0", "@babel/types@^7.10.4", "@babel/types@^7.10.5", "@babel/types@^7.11.0", "@babel/types@^7.3.0", "@babel/types@^7.4.4", "@babel/types@^7.9.0":
version "7.11.0"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.11.0.tgz#2ae6bf1ba9ae8c3c43824e5861269871b206e90d"
integrity sha512-O53yME4ZZI0jO1EVGtF1ePGl0LHirG4P1ibcD80XyzZcKhcMFeCXmh4Xb1ifGBIV233Qg12x4rBfQgA+tmOukA==
"@babel/types@^7.0.0", "@babel/types@^7.10.4", "@babel/types@^7.10.5", "@babel/types@^7.11.0", "@babel/types@^7.11.5", "@babel/types@^7.3.0", "@babel/types@^7.4.4", "@babel/types@^7.9.0":
version "7.11.5"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.11.5.tgz#d9de577d01252d77c6800cee039ee64faf75662d"
integrity sha512-bvM7Qz6eKnJVFIn+1LPtjlBFPVN5jNDc1XmN15vWe7Q3DPBufWWsLiIvUu7xW87uTG6QoggpIDnUgLQvPheU+Q==
dependencies:
"@babel/helper-validator-identifier" "^7.10.4"
lodash "^4.17.19"
Expand Down Expand Up @@ -1881,6 +1895,13 @@ babel-plugin-macros@^2.6.1:
cosmiconfig "^6.0.0"
resolve "^1.12.0"

babel-plugin-polyfill-regenerator@^0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/babel-plugin-polyfill-regenerator/-/babel-plugin-polyfill-regenerator-0.0.4.tgz#588641af9a2cb4e299b1400c47672a4a104d2459"
integrity sha512-+/uCzO9JTYVZVGCpZpVAQkgPGt2zkR0VYiZvJ4aVoCe4ccgpKvNQqcjzAgQzSsjK64Jhc5hvrCR3l0087BevkA==
dependencies:
"@babel/helper-define-polyfill-provider" "^0.0.3"

babel-plugin-replace-identifiers@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/babel-plugin-replace-identifiers/-/babel-plugin-replace-identifiers-0.1.1.tgz#38819617d814ab0e4b35cfa373e40758b359f68f"
Expand Down Expand Up @@ -2540,7 +2561,7 @@ core-js-pure@^3.0.0:
resolved "https://registry.yarnpkg.com/core-js-pure/-/core-js-pure-3.4.7.tgz#c998e1892da9949200c7452cbd33c0df95be9f54"
integrity sha512-Am3uRS8WCdTFA3lP7LtKR0PxgqYzjAMGKXaZKSNSC/8sqU0Wfq8R/YzoRs2rqtOVEunfgH+0q3O0BKOg0AvjPw==

core-js@^2.4.0, core-js@^2.6.5:
core-js@^2.4.0:
version "2.6.10"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.10.tgz#8a5b8391f8cc7013da703411ce5b585706300d7f"
integrity sha512-I39t74+4t+zau64EN1fE5v2W31Adtc/REhzWN+gWRRXg6WH5qAsZm62DHpQ1+Yhe4047T55jvzz7MUqF/dBBlA==
Expand Down Expand Up @@ -5348,6 +5369,11 @@ lodash.camelcase@^4.3.0:
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"
integrity sha1-soqmKIorn8ZRA1x3EfZathkDMaY=

lodash.debounce@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=

lodash.memoize@4.x, lodash.memoize@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
Expand Down

0 comments on commit 6e405d5

Please sign in to comment.