Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use @babel/preset-env with bugfixes for modern builds #702

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-insects-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Use @babel/preset-env with bugfixes instead of preset-modules to enable "Optional chaining" & "nullish coalescing" by default.
3,108 changes: 2,039 additions & 1,069 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@
"@babel/plugin-transform-flow-strip-types": "^7.10.1",
"@babel/plugin-transform-react-jsx": "^7.10.1",
"@babel/plugin-transform-regenerator": "^7.10.1",
"@babel/preset-env": "^7.10.2",
"@babel/preset-env": "^7.11.0",
"@babel/preset-flow": "^7.10.1",
"@babel/preset-modules": "^0.1.3",
"@rollup/plugin-alias": "^3.1.1",
"@rollup/plugin-babel": "^5.0.3",
"@rollup/plugin-commonjs": "^13.0.0",
Expand Down Expand Up @@ -117,7 +116,6 @@
"devDependencies": {
"@babel/cli": "^7.10.1",
"@babel/node": "^7.10.1",
"@babel/plugin-proposal-optional-chaining": "^7.10.1",
"@babel/plugin-proposal-throw-expressions": "^7.10.1",
"@changesets/changelog-github": "^0.2.6",
"@changesets/cli": "^2.9.2",
Expand Down
9 changes: 4 additions & 5 deletions src/lib/babel-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const createConfigItems = (babel, type, items) => {
});
};

const presetEnvRegex = RegExp(/@babel\/(preset-)?env/);
const environmentPreset = '@babel/preset-env';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This no longer finds @babel/env, which is an alias of @babel/preset-env

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I searched for it and couldn't find any usage of it. i'll create a follow up PR

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh - yeah we don't have a test for it, just it's possible to get that value when we read in user config.

const presetEnvRegex = new RegExp(environmentPreset);

export default () => {
return createBabelInputPluginFactory(babelCore => {
Expand Down Expand Up @@ -123,10 +124,6 @@ export default () => {
presetEnvRegex.test(preset.file.request),
);

const environmentPreset = customOptions.modern
? '@babel/preset-modules'
: '@babel/preset-env';

if (envIdx !== -1) {
const preset = babelOptions.presets[envIdx];
babelOptions.presets[envIdx] = babelCore.createConfigItem(
Expand All @@ -141,6 +138,7 @@ export default () => {
},
preset.options,
{
bugfixes: customOptions.modern,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just always be set to true, it has no effect on cases where the output is below the affected syntax level.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet

modules: false,
exclude: merge(
['transform-async-to-generator', 'transform-regenerator'],
Expand All @@ -165,6 +163,7 @@ export default () => {
modules: false,
loose: true,
useBuiltIns: false,
bugfixes: customOptions.modern,
exclude: [
'transform-async-to-generator',
'transform-regenerator',
Expand Down
18 changes: 9 additions & 9 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2232,12 +2232,12 @@ optional-chaining-ts


Build \\"optionalChainingTs\\" to dist:
104 B: optional-chaining-ts.js.gz
77 B: optional-chaining-ts.js.br
108 B: optional-chaining-ts.esm.js.gz
92 B: optional-chaining-ts.esm.js.br
207 B: optional-chaining-ts.umd.js.gz
166 B: optional-chaining-ts.umd.js.br"
109 B: optional-chaining-ts.js.gz
88 B: optional-chaining-ts.js.br
111 B: optional-chaining-ts.esm.js.gz
97 B: optional-chaining-ts.esm.js.br
211 B: optional-chaining-ts.umd.js.gz
170 B: optional-chaining-ts.umd.js.br"
`;

exports[`fixtures build optional-chaining-ts with microbundle 2`] = `7`;
Expand All @@ -2252,19 +2252,19 @@ exports[`fixtures build optional-chaining-ts with microbundle 3`] = `
`;

exports[`fixtures build optional-chaining-ts with microbundle 4`] = `
"function n(n){var r;return null===(r=n.maybeVar)||void 0===r?void 0:r.thing}export{n as chain};
"function n(n){var r,i;return null!=(r=null==(i=n.maybeVar)?void 0:i.thing)?r:void 0}export{n as chain};
//# sourceMappingURL=optional-chaining-ts.esm.js.map
"
`;

exports[`fixtures build optional-chaining-ts with microbundle 5`] = `
"exports.chain=function(n){var i;return null===(i=n.maybeVar)||void 0===i?void 0:i.thing};
"exports.chain=function(n){var i,r;return null!=(i=null==(r=n.maybeVar)?void 0:r.thing)?i:void 0};
//# sourceMappingURL=optional-chaining-ts.js.map
"
`;

exports[`fixtures build optional-chaining-ts with microbundle 6`] = `
"!function(n,e){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?e(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],e):e((n=n||self).optionalChainingTs={})}(this,function(n){n.chain=function(n){var e;return null===(e=n.maybeVar)||void 0===e?void 0:e.thing}});
"!function(n,e){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?e(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],e):e((n=n||self).optionalChainingTs={})}(this,function(n){n.chain=function(n){var e,i;return null!=(e=null==(i=n.maybeVar)?void 0:i.thing)?e:void 0}});
//# sourceMappingURL=optional-chaining-ts.umd.js.map
"
`;
Expand Down
10 changes: 0 additions & 10 deletions test/fixtures/optional-chaining-ts/.babelrc

This file was deleted.

6 changes: 4 additions & 2 deletions test/fixtures/optional-chaining-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export function chain(test: { maybeVar?: { thing: string } }): string | undefined {
return test.maybeVar?.thing;
export function chain(test: {
maybeVar?: { thing: string };
}): string | undefined {
return test.maybeVar?.thing ?? undefined;
}