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

update esbuild to work with babel #449

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 21 additions & 25 deletions tools/lib/esbuildConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs/promises";
import path from "path";
import babel from "@babel/core";
import babelPluginSyntaxTypescript from "@babel/plugin-syntax-typescript";
import babelPluginSportFunctions from "../babel-plugin-sport-functions/index.cjs";
Expand All @@ -10,7 +11,14 @@ const babelCache = {};
const pluginSportFunctions = nodeEnv => ({
name: "sport-functions",
setup(build) {
build.onLoad({ filter: /\.tsx?$/, namespace: "file" }, async args => {
build.onResolve({ filter: /\..{1,}Sport/ }, async args => {
return {
path: path.join(args.resolveDir, args.path) + ".ts",
namespace: "by-sport",
};
});

build.onLoad({ filter: /\.tsx?$/, namespace: "by-sport" }, async args => {
const { mtimeMs } = await fs.stat(args.path);
if (babelCache[args.path] && babelCache[args.path].mtimeMs === mtimeMs) {
return babelCache[args.path].result;
Expand All @@ -22,37 +30,25 @@ const pluginSportFunctions = nodeEnv => ({

const text = await fs.readFile(args.path, "utf8");

// result is undefined if no match, meaning just do normal stuff
let result;
if (
text.includes("bySport") ||
(nodeEnv === "production" && text.includes("isSport"))
) {
const contents = (
await babel.transformAsync(text, {
babelrc: false,
configFile: false,
sourceMaps: "inline",
plugins: [
[babelPluginSyntaxTypescript, { isTSX }],
babelPluginSportFunctions,
],
})
).code;
const contents = (
await babel.transformAsync(text, {
babelrc: false,
configFile: false,
sourceMaps: "inline",
plugins: [
[babelPluginSyntaxTypescript, { isTSX }],
babelPluginSportFunctions,
],
})
).code;

result = { contents, loader };
}
const result = { contents, loader };

babelCache[args.path] = {
mtimeMs,
result,
};

if (result === undefined) {
// Might as well return the text, since we have it in memory already
result = { contents: text, loader };
}

return result;
});
},
Expand Down