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

Worker: Babel ignore node_modules by default + some minor babel transform perf improvements #435

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
26 changes: 19 additions & 7 deletions src/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ function prepareJscodeshift(options) {
}

function setup(tr, babel) {
let babelRegister;

if (babel === 'babel') {
const presets = [];
if (presetEnv) {
Expand All @@ -66,19 +68,24 @@ function setup(tr, babel) {
require('@babel/preset-flow').default
);

require('@babel/register')({
const plugins = [
require('@babel/plugin-proposal-class-properties').default,
require('@babel/plugin-proposal-nullish-coalescing-operator').default,
require('@babel/plugin-proposal-optional-chaining').default,
require('@babel/plugin-transform-modules-commonjs').default,
];

babelRegister = require('@babel/register');

babelRegister({
babelrc: false,
presets,
plugins: [
require('@babel/plugin-proposal-class-properties').default,
require('@babel/plugin-proposal-nullish-coalescing-operator').default,
require('@babel/plugin-proposal-optional-chaining').default,
require('@babel/plugin-transform-modules-commonjs').default,
],
plugins,
extensions: [...DEFAULT_EXTENSIONS, '.ts', '.tsx'],
// By default, babel register only compiles things inside the current working directory.
// https://github.com/babel/babel/blob/2a4f16236656178e84b05b8915aab9261c55782c/packages/babel-register/src/node.js#L140-L157
ignore: [
/\/node_modules\//,
// Ignore parser related files
/@babel\/parser/,
/\/flow-parser\//,
Expand All @@ -89,6 +96,11 @@ function setup(tr, babel) {
}

const module = require(tr);

if (babelRegister) {
babelRegister.revert();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test:

it('transpiles imported Typescript files in transform files', () => {
const source = createTempFileWith('a');
const helper = createTempFileWith(
'module.exports = function(x: string): x is string {};',
undefined,
'.ts'
);
const transform = createTransformWith(
`return require('${helper}').toString();`,
'.ts'
);
return Promise.all([
run(['-t', transform, source]).then(
args => {
expect(readFile(source).toString())
.toMatch(/function\s*\(x\)\s*{}/);
}
),
]);
});

Catches that while reverting the babel register here works for any require/import that would be run at require time, it wouldn't catch any that would only be executed at run time.

This part isn't too significant, so supporting that case probably more important than worrying about reverting the register? Happy to 🔪


transform = typeof module.default === 'function' ?
module.default :
module;
Expand Down