Skip to content

Commit

Permalink
fix(bun): Support application builds with Bun (#6344)
Browse files Browse the repository at this point in the history
  • Loading branch information
octet-stream committed May 21, 2024
1 parent 6d4a549 commit 0b8410b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/qwik/src/optimizer/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function getSystem() {
}

if (globalThis.IS_CJS) {
if (sysEnv === 'node') {
if (sysEnv === 'node' || sysEnv === 'bun') {
// using this api object as a way to ensure bundlers
// do not try to inline or rewrite require()
sys.dynamicImport = (path) => require(path);
Expand All @@ -62,7 +62,7 @@ export async function getSystem() {
}
}

if (sysEnv === 'node') {
if (sysEnv === 'node' || sysEnv === 'bun') {
sys.path = await sys.dynamicImport('node:path');
sys.cwd = () => process.cwd();
sys.os = process.platform;
Expand Down Expand Up @@ -135,7 +135,7 @@ export async function loadPlatformBinding(sys: OptimizerSystem) {
const sysEnv = getEnv();

// Try native build
if (sysEnv === 'node') {
if (sysEnv === 'node' || sysEnv === 'bun') {
// Node.js
const platform = (QWIK_BINDING_MAP as any)[process.platform];
if (platform) {
Expand Down Expand Up @@ -164,7 +164,7 @@ export async function loadPlatformBinding(sys: OptimizerSystem) {
if (globalThis.IS_CJS) {
// CJS WASM

if (sysEnv === 'node') {
if (sysEnv === 'node' || sysEnv === 'bun') {
// CJS WASM Node.js
const wasmPath = sys.path.join(__dirname, 'bindings', 'qwik_wasm_bg.wasm');
const mod = await sys.dynamicImport(`./bindings/qwik.wasm.cjs`);
Expand Down Expand Up @@ -225,7 +225,7 @@ export async function loadPlatformBinding(sys: OptimizerSystem) {
}

if (globalThis.IS_ESM) {
if (sysEnv === 'node') {
if (sysEnv === 'node' || sysEnv === 'bun') {
// CJS WASM Node.js
const url: typeof import('url') = await sys.dynamicImport('node:url');
const __dirname = sys.path.dirname(url.fileURLToPath(import.meta.url));
Expand Down
5 changes: 5 additions & 0 deletions starters/adapters/bun/adapters/bun/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { bunServerAdapter } from "@builder.io/qwik-city/adapters/bun-server/vite";
import { extendConfig } from "@builder.io/qwik-city/vite";
import { _TextEncoderStream_polyfill } from "@builder.io/qwik-city/middleware/request-handler";
import baseConfig from "../../vite.config";

// This polyfill is required when you use SSG and build your app with Bun, because Bun does not have TextEncoderStream. See: https://github.com/oven-sh/bun/issues/5648
globalThis.TextEncoderStream ||= _TextEncoderStream_polyfill;

export default extendConfig(baseConfig, () => {
return {
build: {
Expand All @@ -16,6 +20,7 @@ export default extendConfig(baseConfig, () => {
ssg: {
include: ["/*"],
origin: "https://yoursite.dev",
maxWorkers: 1, // Limit Workers to 1, otherwise SSG will hang when compiling Qwik City app with `bun run --bun build`.
},
}),
],
Expand Down

0 comments on commit 0b8410b

Please sign in to comment.