Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/dry-pots-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'flatfile': major
---

This release adds bun build support to the flatfile cli
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
"bin": {
"flatfile": "./dist/index.js"
},
"type": "module",
"files": [
"dist",
"templates"
],
"scripts": {
"build": "tsup src/index.ts --format esm,cjs --dts",
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
"build": "bun build src/index.ts --target node --outdir dist",
"dev": "bun run src/index.ts --target node --watch",
"lint": "TIMING=1 eslint \"src/**/*.{ts,tsx,js,jsx}\" --fix",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"test": "jest --runInBand --forceExit --passWithNoTests"
Expand Down
88 changes: 72 additions & 16 deletions packages/cli/src/x/actions/deploy.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export async function deployAction(

try {
const data = fs.readFileSync(
path.join(__dirname, '..', 'templates', 'entry.js'),
path.join(__dirname, '../../../', 'templates', 'entry.js'),
'utf8'
)
const result = data.replace(
Expand Down Expand Up @@ -303,21 +303,17 @@ export async function deployAction(
}).start()

try {
const {
err,
code,
map: sourceMapBase,
} = await ncc(path.join(outDir, '_entry.js'), {
minify: liteMode,
target: 'es2020',
sourceMap: true,
sourceMapIncludeSources: true,
sourceMapRegister: false,
cache: false,
// TODO: add debug flag to add this and other debug options
quiet: true,
// debugLog: false
})
let code: string
let err: any
let sourceMapBase: string

if (process.versions.bun) {
// Runtime is bun, so we can use Bun.build
;({ code, sourceMapBase, err } = await bundleWithBun(outDir, liteMode));
} else {
// Runtime is node, so we can use ncc
;({ code, sourceMapBase, err } = await bundleWithNcc(outDir, liteMode));
}

const deployFile = path.join(outDir, 'deploy.js')
fs.writeFileSync(deployFile, code, 'utf8')
Expand Down Expand Up @@ -362,3 +358,63 @@ export async function deployAction(
return program.error(messages.error(e))
}
}

async function bundleWithBun(outDir: string, liteMode: boolean) {
// Kick off the build
// @ts-expect-error Bun is not defined in the global scope
// eslint-disable-next-line no-undef
const result = await Bun.build({
entrypoints: [path.join(outDir, "_entry.js")],
outdir: outDir, // write files to disk
bundle: true, // bundle dependencies
minify: liteMode, // equivalent to ncc’s `minify`
target: "node", // same as ncc’s `target`
sourcemap: "linked", // generate source maps
format: "esm", // or "cjs" if you need CommonJS
// note: Bun.build doesn’t support sourceMapIncludeSources,
// sourceMapRegister, cache or quiet flags––you can control
// logs via `result.logs` below.
});

// Check for build errors
let err: any
if (!result.success) {
err = 'Build failed:'
for (const msg of result.logs) {
err += `\n${msg}`
}
}

// Extract code and source map from the outputs
let code = "";
let sourceMapBase = "";
for (const artifact of result.outputs) {
if (artifact.kind === "entry-point") {
code = await artifact.text();
} else if (artifact.kind === "sourcemap") {
sourceMapBase = await artifact.text();
}
}

return { code, sourceMapBase, err };
}

async function bundleWithNcc(outDir: string, liteMode: boolean) {
const {
err,
code,
map: sourceMapBase,
} = await ncc(path.join(outDir, '_entry.js'), {
minify: liteMode,
target: 'es2020',
sourceMap: true,
sourceMapIncludeSources: true,
sourceMapRegister: false,
cache: false,
// TODO: add debug flag to add this and other debug options
quiet: true,
// debugLog: false
})

return { code, sourceMapBase, err };
}
Loading