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

Babel helper things #358

Merged
merged 3 commits into from
Jan 5, 2021
Merged

Babel helper things #358

merged 3 commits into from
Jan 5, 2021

Conversation

emmatown
Copy link
Member

@emmatown emmatown commented Jan 5, 2021

Closes #318

cc @Andarist thought you might be interested in this

@changeset-bot
Copy link

changeset-bot bot commented Jan 5, 2021

🦋 Changeset detected

Latest commit: eebf097

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@preconstruct/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov
Copy link

codecov bot commented Jan 5, 2021

Codecov Report

Merging #358 (c503b63) into master (b9e9137) will increase coverage by 0.22%.
The diff coverage is 97.05%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #358      +/-   ##
==========================================
+ Coverage   89.34%   89.56%   +0.22%     
==========================================
  Files          32       32              
  Lines        1276     1303      +27     
  Branches      322      336      +14     
==========================================
+ Hits         1140     1167      +27     
  Misses        132      132              
  Partials        4        4              
Impacted Files Coverage Δ
packages/cli/src/worker.ts 91.30% <ø> (ø)
packages/cli/src/rollup-plugins/babel.ts 98.43% <95.45%> (-1.57%) ⬇️
packages/cli/src/build/config.ts 84.21% <100.00%> (+2.72%) ⬆️
packages/cli/src/build/rollup.ts 90.47% <100.00%> (+0.82%) ⬆️
.../cli/src/rollup-plugins/flow-and-prod-dev-entry.ts 87.75% <100.00%> (+0.25%) ⬆️
...rc/rollup-plugins/rewrite-babel-runtime-helpers.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b9e9137...eebf097. Read the comment docs.

@@ -57,6 +57,11 @@ function getGlobal(project: Project, name: string) {
}
}

const babelHelperId = /@babel\/runtime(|-corejs[23])\/helpers\//;

const interop = (id: string | null): "auto" | "default" =>
Copy link
Member

Choose a reason for hiding this comment

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

TIL that this can be a function

Copy link
Member

Choose a reason for hiding this comment

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

Have you checked how this will behave in Babel 8? I know that more than this would have to be re-checked to check against Babel 8 compatibility but maybe worth to be future-proof here.

In Babel 8 there won't be separate cjs/esm versions of helpers as they have started to use conditional exports. You would have to check if interop: "default" works with Babel 8 though - there might have been changes around this (but they might have also left it as is - I don't recall, I think Nicolo has wanted to support .default-less requires in node so probably this has not been touched but... well, worth re-checking).

Copy link
Member Author

Choose a reason for hiding this comment

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

Given the PR about it isn't merged yet, I'm not overly concerned about it. Preconstruct has a direct dependency on @babel/core@7 rn anyway.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/test.cjs.dev.js, dist/test.cjs.prod.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
'use strict';

var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
Copy link
Member

Choose a reason for hiding this comment

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

shouldnt u add a very similar test that would check that this helper has been inlined rather than required?

Copy link
Member Author

Choose a reason for hiding this comment

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

As in when the version of @babel/runtime doesn't include that helper? That test already exists before this PR -

test("does not duplicate babel helpers when using @babel/plugin-transform-runtime but the helper isn't in the version of @babel/runtime that the user has specified", async () => {
const dir = await testdir({
"package.json": JSON.stringify({
name: "test",
main: "dist/test.cjs.js",
dependencies: {
"@babel/runtime": "*",
},
}),
"babel.config.json": JSON.stringify({
presets: [require.resolve("@babel/preset-env")],
plugins: [require.resolve("@babel/plugin-transform-runtime")],
}),
"src/index.js": "import './other'; for (const x of something) {}",
"src/other.js": "for (const x of something) {}",
});
await build(dir);
expect(await getDist(dir)).toMatchInlineSnapshot(`
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/test.cjs.dev.js, dist/test.cjs.prod.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
'use strict';
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _createForOfIteratorHelper(o) {
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) {
var i = 0;
var F = function () {};
return {
s: F,
n: function () {
if (i >= o.length) return {
done: true
};
return {
done: false,
value: o[i++]
};
},
e: function (e) {
throw e;
},
f: F
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var it,
normalCompletion = true,
didErr = false,
err;
return {
s: function () {
it = o[Symbol.iterator]();
},
n: function () {
var step = it.next();
normalCompletion = step.done;
return step;
},
e: function (e) {
didErr = true;
err = e;
},
f: function () {
try {
if (!normalCompletion && it.return != null) it.return();
} finally {
if (didErr) throw err;
}
}
};
}
var _iterator = _createForOfIteratorHelper(something),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var x = _step.value;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
var _iterator$1 = _createForOfIteratorHelper(something),
_step$1;
try {
for (_iterator$1.s(); !(_step$1 = _iterator$1.n()).done;) {
var x$1 = _step$1.value;
}
} catch (err) {
_iterator$1.e(err);
} finally {
_iterator$1.f();
}
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/test.cjs.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
'use strict';
if (process.env.NODE_ENV === "production") {
module.exports = require("./test.cjs.prod.js");
} else {
module.exports = require("./test.cjs.dev.js");
}
`);
});

Copy link
Member

Choose a reason for hiding this comment

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

a similar test - yes, but without @babel/plugin-transform-runtime being used

@@ -49,6 +49,7 @@ export default function flowAndNodeDevProdEntry(
}

if (
source.startsWith("\0") ||
Copy link
Member

Choose a reason for hiding this comment

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

wait - is \0babelWhatever added to the top of a generated virtual source code? 🤔 how does it work? kinda blurry for me in a rush

Copy link
Member Author

Choose a reason for hiding this comment

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

source is referring to the unresolved module specifier, not source code.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, gotcha.

@Andarist
Copy link
Member

Andarist commented Jan 5, 2021

How do u ensure that this doesn't clash with configurations using the transform plugin explicitly? 🤔

@emmatown
Copy link
Member Author

emmatown commented Jan 5, 2021

How do u ensure that this doesn't clash with configurations using the transform plugin explicitly? 🤔

const previousHelperGenerator = file.get("helperGenerator");
file.set("helperGenerator", (name: string) => {
if (previousHelperGenerator) {
const helperFromPrev = previousHelperGenerator(name);
if (helperFromPrev != null) return helperFromPrev;
}

(that plugin is always last)

Copy link
Member

@Andarist Andarist left a comment

Choose a reason for hiding this comment

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

From whatever my ✅ is worth here:

✅ ✅ ✅ ✅ ...

Great attention to little things as always 👍

@emmatown emmatown merged commit e3b4196 into master Jan 5, 2021
@emmatown emmatown deleted the babel-helper-things branch January 5, 2021 20:20
@github-actions github-actions bot mentioned this pull request Jan 5, 2021
@github-actions github-actions bot mentioned this pull request Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate that @babel/plugin-transform-runtime is configured correctly
2 participants