From 4703194ba3d9d015e604509a46e300b3b18fa572 Mon Sep 17 00:00:00 2001 From: Nikhil Saraf Date: Wed, 28 Feb 2024 04:14:03 +0530 Subject: [PATCH 1/3] remove unncessary console log --- packages/vinxi/lib/nitro-dev.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/vinxi/lib/nitro-dev.js b/packages/vinxi/lib/nitro-dev.js index d31d81e5..024004b8 100644 --- a/packages/vinxi/lib/nitro-dev.js +++ b/packages/vinxi/lib/nitro-dev.js @@ -186,7 +186,6 @@ export async function createDevServer(nitro) { // Dev-only handlers for (const handler of nitro.options.devHandlers) { - console.log(handler); app.use( joinURL(nitro.options.runtimeConfig.app.baseURL, handler.route ?? "/"), handler.handler, From 63ecf3360f00916322481c746c85303a4e2e02e1 Mon Sep 17 00:00:00 2001 From: Nikhil Saraf Date: Wed, 28 Feb 2024 04:14:32 +0530 Subject: [PATCH 2/3] feat: bundle config file with esbuild before loading, adds support for ts, jsx, etc in config files --- packages/vinxi/lib/load-app.js | 96 ++++++++++++++++++++++++++-------- 1 file changed, 75 insertions(+), 21 deletions(-) diff --git a/packages/vinxi/lib/load-app.js b/packages/vinxi/lib/load-app.js index 1b9949cc..b2782da9 100644 --- a/packages/vinxi/lib/load-app.js +++ b/packages/vinxi/lib/load-app.js @@ -1,11 +1,12 @@ /// -import { loadConfig } from "c12"; import { existsSync } from "fs"; +import { rm } from "fs/promises"; +import { isBuiltin } from "module"; import { fileURLToPath, pathToFileURL } from "url"; import { createApp } from "./app.js"; import { log } from "./logger.js"; -import { join } from "./path.js"; +import { isAbsolute, join } from "./path.js"; function isBun() { return !!process.versions.bun; @@ -15,44 +16,97 @@ async function fileExists(path) { return isBun() ? Bun.file(path).exists() : existsSync(path); } +const bundleConfigFile = async (configFile, out) => { + const esbuild = await import("esbuild"); + + // const out = `app.config.timestamp_${Date.now()}.js`; + await esbuild.build({ + entryPoints: [configFile], + bundle: true, + outfile: out, + platform: "node", + format: "esm", + resolveExtensions: [".js", ".mjs", ".ts", ".jsx", ".tsx"], + plugins: [ + { + name: "externalize-deps", + setup(build) { + build.onResolve( + { filter: /^[^.].*/ }, + async ({ path: id, importer, kind }) => { + if ( + kind === "entry-point" || + isAbsolute(id) || + id.match(/node:.*/) + ) { + return; + } + + // With the `isNodeBuiltin` check above, this check captures if the builtin is a + // non-node built-in, which esbuild doesn't know how to handle. In that case, we + // externalize it so the non-node runtime handles it instead. + if (isBuiltin(id)) { + return { external: true }; + } + + return { external: true }; + }, + ); + }, + }, + ], + loader: { + ".js": "jsx", + ".ts": "tsx", + ".jsx": "jsx", + ".tsx": "tsx", + ".mjs": "jsx", + }, + }); +}; + async function loadFile({ ...options }) { if (options.name) { - for (const ext of ["js", "mjs", "ts"]) { - if (ext === "ts" && !isBun()) continue; - + for (const ext of ["js", "mjs", "ts", "tsx", "jsx"]) { const filepath = join(process.cwd(), `${options.name}.config.${ext}`); if (await fileExists(filepath)) { - return import( - pathToFileURL(filepath).href + `?time=${Date.now()}` - ).then((m) => ({ + let out = `${options.name}.config.timestamp_${Date.now()}.${ext}`; + await bundleConfigFile(`${options.name}.config.${ext}`, out); + const importedApp = import(pathToFileURL(out).href).then((m) => ({ config: m.default, })); + + await rm(out); + return importedApp; } } + + throw new Error(`Config file not found: ${options.name}`); } else if (options.configFile) { const ext = options.configFile.slice( options.configFile.lastIndexOf(".") + 1, ); - if (["js", "mjs", "ts"].includes(ext) && (ext !== "ts" || isBun())) { - const filepath = join(process.cwd(), options.configFile); + const configFileName = options.configFile.slice( + 0, + options.configFile.lastIndexOf("."), + ); + + if (["js", "mjs", "ts", "tsx", "jsx"].includes(ext)) { + const filepath = join(process.cwd(), `${configFileName}.${ext}`); if (await fileExists(filepath)) { - return import( - pathToFileURL(filepath).href + `?time=${Date.now()}` - ).then((m) => ({ + let out = `${configFileName}.timestamp_${Date.now()}.${ext}`; + await bundleConfigFile(options.configFile, out); + const importedApp = import(pathToFileURL(out).href).then((m) => ({ config: m.default, })); + await rm(out); + return importedApp; } } - } - return loadConfig({ - jitiOptions: { - esmResolve: true, - nativeModules: ["acorn"], - }, - ...options, - }); + throw new Error(`Config file not found: ${options.configFile}`); + } } /** From 345ed49aef51b68327c756a9bb24f6ad2d25d445 Mon Sep 17 00:00:00 2001 From: Nikhil Saraf Date: Wed, 28 Feb 2024 04:16:39 +0530 Subject: [PATCH 3/3] Create cuddly-balloons-yawn.md --- .changeset/cuddly-balloons-yawn.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/cuddly-balloons-yawn.md diff --git a/.changeset/cuddly-balloons-yawn.md b/.changeset/cuddly-balloons-yawn.md new file mode 100644 index 00000000..926c5cff --- /dev/null +++ b/.changeset/cuddly-balloons-yawn.md @@ -0,0 +1,6 @@ +--- +"vinxi": patch +"react-ssr-basic": patch +--- + +feat: bundle config file with esbuild before loading, adds support for ts, jsx, etc in config files