From 5b9d63d005bfd0093da0889a80416f8b79035e60 Mon Sep 17 00:00:00 2001 From: Luke D Date: Sun, 4 Jun 2023 09:09:36 -0700 Subject: [PATCH] update esbuild to work with babel --- tools/lib/esbuildConfig.js | 46 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/tools/lib/esbuildConfig.js b/tools/lib/esbuildConfig.js index ae68822a58..435ef6c45e 100644 --- a/tools/lib/esbuildConfig.js +++ b/tools/lib/esbuildConfig.js @@ -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"; @@ -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; @@ -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; }); },