nuxt-bun-compile 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dprint.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "typescript": {
3
+ },
4
+ "json": {
5
+ },
6
+ "markdown": {
7
+ },
8
+ "toml": {
9
+ },
10
+ "dockerfile": {
11
+ },
12
+ "malva": {
13
+ },
14
+ "markup": {
15
+ },
16
+ "yaml": {
17
+ },
18
+ "excludes": [
19
+ "**/node_modules",
20
+ "**/.claude",
21
+ "**/*-lock.json",
22
+ "**/bun.lock"
23
+ ],
24
+ "plugins": [
25
+ "https://plugins.dprint.dev/typescript-0.95.15.wasm",
26
+ "https://plugins.dprint.dev/json-0.21.1.wasm",
27
+ "https://plugins.dprint.dev/markdown-0.21.1.wasm",
28
+ "https://plugins.dprint.dev/toml-0.7.0.wasm",
29
+ "https://plugins.dprint.dev/dockerfile-0.3.3.wasm",
30
+ "https://plugins.dprint.dev/g-plane/malva-v0.15.2.wasm",
31
+ "https://plugins.dprint.dev/g-plane/markup_fmt-v0.26.0.wasm",
32
+ "https://plugins.dprint.dev/g-plane/pretty_yaml-v0.6.0.wasm"
33
+ ]
34
+ }
Binary file
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "nuxt-bun-compile",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./src/module.ts",
6
+ "scripts": {
7
+ "format": "dprint fmt package.json tsconfig.json dprint.json ./src/**/*.ts"
8
+ },
9
+ "dependencies": {
10
+ "@nuxt/kit": "^3.17.5",
11
+ "@nuxt/schema": "^4.3.1"
12
+ },
13
+ "devDependencies": {
14
+ "@types/node": "^25.2.3",
15
+ "dprint": "^0.51.1",
16
+ "nitropack": "^2.13.1"
17
+ },
18
+ "packageManager": "bun@1.3.9",
19
+ "engines": {
20
+ "node": "^24.13.1",
21
+ "npm": "^11.10.0",
22
+ "pnpm": "^10.29.3",
23
+ "bun": "^1.3.9"
24
+ },
25
+ "trustedDependencies": ["dprint"]
26
+ }
package/src/module.ts ADDED
@@ -0,0 +1,113 @@
1
+ import { defineNuxtModule, useLogger } from "@nuxt/kit";
2
+ import type { Nuxt } from "@nuxt/schema";
3
+ import type { NitroConfig } from "nitropack/types";
4
+ import { execSync } from "node:child_process";
5
+ import { statSync } from "node:fs";
6
+ import { join } from "node:path";
7
+
8
+ export interface ModuleOptions {
9
+ enabled: boolean;
10
+ outfile: string;
11
+ extraExternals: (string | RegExp)[];
12
+ autoCompile: boolean;
13
+ bunPath?: string;
14
+ }
15
+
16
+ const DEFAULT_EXTERNALS: (string | RegExp)[] = [
17
+ "sharp",
18
+ /^@img\//,
19
+ "css-tree",
20
+ /^css-tree\//,
21
+ "csso",
22
+ /^csso\//,
23
+ "svgo",
24
+ "mdn-data",
25
+ /^mdn-data\//,
26
+ ];
27
+
28
+ export default defineNuxtModule<ModuleOptions>({
29
+ meta: {
30
+ name: "nuxt-bun-compile",
31
+ configKey: "bunCompile",
32
+ },
33
+ defaults: {
34
+ enabled: true,
35
+ outfile: "nuxtbin",
36
+ extraExternals: [],
37
+ autoCompile: true,
38
+ },
39
+ setup(options: ModuleOptions, nuxt: Nuxt) {
40
+ if (!options.enabled) return;
41
+
42
+ const logger = useLogger("nuxt-bun-compile");
43
+
44
+ // Configure Nitro for bun compile
45
+ nuxt.hook("nitro:config" as any, (nitroConfig: NitroConfig) => {
46
+ logger.info("Configuring Nitro for bun --compile build");
47
+
48
+ nitroConfig.preset = "bun";
49
+ nitroConfig.noExternals = true;
50
+ nitroConfig.inlineDynamicImports = true;
51
+ nitroConfig.serveStatic = "inline";
52
+
53
+ nitroConfig.esbuild = nitroConfig.esbuild || {};
54
+ nitroConfig.esbuild.options = nitroConfig.esbuild.options || {};
55
+ nitroConfig.esbuild.options.target = "esnext";
56
+
57
+ const allExternals = [...DEFAULT_EXTERNALS, ...options.extraExternals];
58
+
59
+ nitroConfig.rollupConfig = nitroConfig.rollupConfig || {};
60
+ const existing = nitroConfig.rollupConfig.external;
61
+ if (Array.isArray(existing)) {
62
+ nitroConfig.rollupConfig.external = [...existing, ...allExternals];
63
+ } else if (existing) {
64
+ nitroConfig.rollupConfig.external = [existing as string | RegExp, ...allExternals];
65
+ } else {
66
+ nitroConfig.rollupConfig.external = allExternals;
67
+ }
68
+
69
+ // Auto-compile after Nitro build completes
70
+ if (options.autoCompile) {
71
+ nitroConfig.hooks = nitroConfig.hooks || {};
72
+ nitroConfig.hooks.compiled = () => {
73
+ const isBun = typeof (globalThis as any).Bun !== "undefined"
74
+ || process.versions.bun !== undefined;
75
+
76
+ if (!isBun) {
77
+ logger.warn("Bun runtime not detected, skipping --compile step. Run with bun to enable.");
78
+ logger.info("Try running: bun run -b build");
79
+ logger.info("Read more: https://github.com/jprando/nuxt-bun-compile?tab=readme-ov-file#why-is--b-required");
80
+ return;
81
+ }
82
+
83
+ const outputPath = ".output/server/index.mjs";
84
+ let bunExecutable = "bun";
85
+ if (options.bunPath) {
86
+ try {
87
+ const stats = statSync(options.bunPath);
88
+ if (stats.isDirectory()) {
89
+ bunExecutable = join(options.bunPath, "bun");
90
+ } else {
91
+ bunExecutable = options.bunPath;
92
+ }
93
+ } catch (error) {
94
+ logger.warn(`Could not stat bunPath "${options.bunPath}", assuming it's a direct path.`);
95
+ bunExecutable = options.bunPath;
96
+ }
97
+ }
98
+
99
+ const cmd = `${bunExecutable} build ${outputPath} --compile --outfile ${options.outfile}`;
100
+
101
+ logger.info(`Bun v${process.versions.bun} detected, running --compile step`);
102
+ logger.info(`Compiling binary: ${cmd}`);
103
+ try {
104
+ execSync(cmd, { stdio: "inherit", cwd: nuxt.options.rootDir });
105
+ logger.success(`Binary created: ${options.outfile}`);
106
+ } catch (err) {
107
+ logger.error("bun build --compile failed:", err);
108
+ }
109
+ };
110
+ }
111
+ });
112
+ },
113
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "strict": true,
7
+ "esModuleInterop": true,
8
+ "skipLibCheck": true
9
+ },
10
+ "include": ["src"]
11
+ }