Skip to content

Commit

Permalink
add esbuild fn support
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Jul 30, 2024
1 parent f58a5d3 commit adcc534
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .changeset/add-custom-esbuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 14 additions & 14 deletions packages/core/src/lib/esbuild.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
}

0 comments on commit adcc534

Please sign in to comment.