From 41cf86e609e1d8cd0d02aa08a02696e2fbb1447b Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Fri, 18 Sep 2020 00:41:03 -0400 Subject: [PATCH] change: replace useBuiltIns with polyfill-regenerator - 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 --- package.json | 2 +- src/babelPluginTsdx.ts | 10 +-- src/createRollupConfig.ts | 14 ++-- test/integration/tsdx-build-withBabel.test.ts | 4 +- yarn.lock | 68 +++++++++++++------ 5 files changed, 64 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index f56f5c5f0..a5ab61799 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/babelPluginTsdx.ts b/src/babelPluginTsdx.ts index 2494130f5..def0f46a7 100644 --- a/src/babelPluginTsdx.ts +++ b/src/babelPluginTsdx.ts @@ -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, @@ -101,8 +107,6 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({ { loose: true, targets: customOptions.targets, - useBuiltIns: 'usage', - corejs: 2, }, presetEnv.options, { @@ -120,8 +124,6 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({ { name: '@babel/preset-env', targets: customOptions.targets, - useBuiltIns: 'usage', - corejs: 2, modules: false, loose: true, }, diff --git a/src/createRollupConfig.ts b/src/createRollupConfig.ts index ef9b66b83..4c630f086 100644 --- a/src/createRollupConfig.ts +++ b/src/createRollupConfig.ts @@ -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; } @@ -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(), { diff --git a/test/integration/tsdx-build-withBabel.test.ts b/test/integration/tsdx-build-withBabel.test.ts index 17b59e2a7..456a329ea 100644 --- a/test/integration/tsdx-build-withBabel.test.ts +++ b/test/integration/tsdx-build-withBabel.test.ts @@ -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(); diff --git a/yarn.lock b/yarn.lock index 41c0f939e..24917b900 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -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" @@ -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== @@ -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"