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

In tests, skip Babel on node_modules when not down leveling #426

Merged
merged 2 commits into from
Oct 5, 2023
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
50 changes: 26 additions & 24 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ function createEsbuildPlugin() {
return { path: path.join(__dirname, "test/browser/mockFs.js") };
});

// Apply babel pass whenever we load a .js file
// Apply babel pass whenever we load a TS or JS file
build.onLoad({ filter: /\.[mc]?[jt]sx?$/ }, async args => {
// But skip any file from node_modules if we aren't down-leveling
if (!downlevel && args.path.includes("node_modules")) return;

const contents = await fs.readFile(args.path, "utf-8");

// Using a cache is crucial as babel is 30x slower than esbuild
Expand Down Expand Up @@ -126,6 +129,7 @@ function createEsbuildPlugin() {
pragmaFrag: "Fragment",
},
];

const ts = [
"@babel/preset-typescript",
{
Expand All @@ -141,36 +145,34 @@ function createEsbuildPlugin() {
},
];

const downlevelPlugin = [
"@babel/preset-env",
{
loose: true,
modules: false,
targets: {
browsers: ["last 2 versions", "IE >= 11"],
},
},
];

const coveragePlugin = [
"istanbul",
{
include: minify ? "**/dist/**/*.js" : "**/src/**/*.{ts,js}",
},
];

const tmp = await babel.transformAsync(result, {
filename: args.path,
sourceMaps: "inline",
presets: downlevel
? [
ts,
jsx,
[
"@babel/preset-env",
{
loose: true,
modules: false,
targets: {
browsers: ["last 2 versions", "IE >= 11"],
},
},
],
]
: [ts, jsx],
presets: downlevel ? [ts, jsx, downlevelPlugin] : [ts, jsx],
plugins: [
coverage && [
"istanbul",
{
include: minify ? "**/dist/**/*.js" : "**/src/**/*.{ts,js}",
},
],
coverage && coveragePlugin,
minify && renamePlugin,
].filter(Boolean),
});
result = tmp.code || result;
result = (tmp && tmp.code) || result;
cache.set(args.path, { input: contents, result });

// Fire all pending listeners that are waiting on the same
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,8 @@
"[email protected]": "patches/[email protected]",
"@babel/[email protected]": "patches/@[email protected]"
}
},
"volta": {
"node": "18.18.0"
}
}