From adcc534f27f79e532b749973e051bbd5399b6ef3 Mon Sep 17 00:00:00 2001 From: Daniel Cousens <413395+dcousens@users.noreply.github.com> Date: Tue, 30 Jul 2024 10:49:22 +1000 Subject: [PATCH] add esbuild fn support --- .changeset/add-custom-esbuild.md | 2 +- packages/core/src/lib/esbuild.ts | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.changeset/add-custom-esbuild.md b/.changeset/add-custom-esbuild.md index 69e37e7549f..5df94ba48db 100644 --- a/.changeset/add-custom-esbuild.md +++ b/.changeset/add-custom-esbuild.md @@ -2,4 +2,4 @@ "@keystone-6/core": minor --- -Add support for developers to add `esbuild.keystone.js` to the working directory to replace the default esbuild configuration +Add support for developers to add `esbuild.keystone.js` to the working directory to mutate the default esbuild configuration diff --git a/packages/core/src/lib/esbuild.ts b/packages/core/src/lib/esbuild.ts index 81d76102ab0..44c3fb23ece 100644 --- a/packages/core/src/lib/esbuild.ts +++ b/packages/core/src/lib/esbuild.ts @@ -1,13 +1,16 @@ // WARNING: be careful not to import `esbuild` within next import { type BuildOptions } from 'esbuild' +function identity (x: BuildOptions) { return x } + export function getEsbuildConfig (cwd: string): BuildOptions { + let esbuildFn: typeof identity | undefined try { - return require(require.resolve(`${cwd}/esbuild.keystone.js`)) + esbuildFn = require(require.resolve(`${cwd}/esbuild.keystone.js`)) } catch (e) {} + esbuildFn ??= identity - // default fallback - return { + return esbuildFn({ entryPoints: ['./keystone'], absWorkingDir: cwd, bundle: true, @@ -20,18 +23,15 @@ export function getEsbuildConfig (cwd: string): BuildOptions { { name: 'external-node_modules', setup (build) { - build.onResolve( - { - // don't bundle anything that is NOT a relative import - // WARNING: we can't use a negative lookahead/lookbehind because esbuild uses Go - filter: /(?:^[^.])|(?:^\.[^/.])|(?:^\.\.[^/])/, - }, - ({ path }) => { - return { external: true, path } - } - ) + build.onResolve({ + // don't bundle anything that is NOT a relative import + // WARNING: we can't use a negative lookahead/lookbehind because esbuild uses Go + filter: /(?:^[^.])|(?:^\.[^/.])|(?:^\.\.[^/])/, + }, ({ path }) => { + return { external: true, path } + }) }, }, ], - } + } satisfies BuildOptions) }