-
Notifications
You must be signed in to change notification settings - Fork 51
use bun glob & update compressible extensions #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,17 +7,15 @@ import { | |
| writeFileSync, | ||
| } from "fs"; | ||
| import { pipeline } from "stream"; | ||
| import glob from "tiny-glob"; | ||
| import { fileURLToPath } from "url"; | ||
| import { promisify } from "util"; | ||
| import zlib from "zlib"; | ||
| import db from "mime-db"; | ||
|
|
||
| const pipe = promisify(pipeline); | ||
|
|
||
| const files = fileURLToPath(new URL("./files", import.meta.url).href); | ||
|
|
||
| /** @type {import('.').default} */ | ||
| export default function (opts = {}) { | ||
| /** @type {import('.').default} */ export default function (opts = {}) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| const { | ||
| out = "build", | ||
| precompress = false, | ||
|
|
@@ -32,33 +30,26 @@ export default function (opts = {}) { | |
| async adapt(builder) { | ||
| builder.rimraf(out); | ||
| builder.mkdirp(out); | ||
|
|
||
| builder.log.minor("Copying assets"); | ||
| builder.writeClient(`${out}/client${builder.config.kit.paths.base}`); | ||
| builder.writePrerendered(`${out}/prerendered${builder.config.kit.paths.base}`); | ||
|
|
||
| if (precompress) { | ||
| builder.log.minor("Compressing assets"); | ||
| await Promise.all([ | ||
| compress(`${out}/client`, precompress), | ||
| compress(`${out}/prerendered`, precompress), | ||
| ]); | ||
| } | ||
|
|
||
| builder.log.minor("Building server"); | ||
| builder.writeServer(`${out}/server`); | ||
|
|
||
| writeFileSync( | ||
| `${out}/manifest.js`, | ||
| `export const manifest = ${builder.generateManifest({ relativePath: "./server" })};\n\n` + | ||
| `export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n`, | ||
| ); | ||
|
|
||
| builder.log.minor("Patching server (websocket support)"); | ||
| patchServerWebsocketHandler(`${out}/server`); | ||
|
|
||
| const pkg = JSON.parse(readFileSync("package.json", "utf8")); | ||
|
|
||
| builder.copy(files, out, { | ||
| replace: { | ||
| SERVER: "./server/index.js", | ||
|
|
@@ -68,77 +59,68 @@ export default function (opts = {}) { | |
| BUILD_OPTIONS: JSON.stringify({ development, dynamic_origin, xff_depth, assets }), | ||
| }, | ||
| }); | ||
|
|
||
| let package_data = { | ||
| name: "bun-sveltekit-app", | ||
| version: "0.0.0", | ||
| type: "module", | ||
| private: true, | ||
| main: "index.js", | ||
| scripts: { | ||
| start: "bun ./index.js", | ||
| }, | ||
| scripts: { start: "bun ./index.js" }, | ||
| dependencies: { cookie: "latest", devalue: "latest", "set-cookie-parser": "latest" }, | ||
| }; | ||
|
|
||
| try { | ||
| pkg.name && (package_data.name = pkg.name); | ||
| pkg.version && (package_data.version = pkg.version); | ||
| pkg.dependencies && | ||
| (package_data.dependencies = { | ||
| ...pkg.dependencies, | ||
| ...package_data.dependencies, | ||
| }); | ||
| (package_data.dependencies = { ...pkg.dependencies, ...package_data.dependencies }); | ||
| } catch (error) { | ||
| builder.log.warn(`Parse package.json error: ${error.message}`); | ||
| } | ||
|
|
||
| writeFileSync(`${out}/package.json`, JSON.stringify(package_data, null, "\t")); | ||
|
|
||
| builder.log.success(`Start server with: bun ./${out}/index.js`); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} directory | ||
| * @param {import('.').CompressOptions} options | ||
| */ | ||
| async function compress(directory, options) { | ||
| /*** @param {string} directory* @param {import('.').CompressOptions} options*/ async function compress( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| directory, | ||
| options, | ||
| ) { | ||
| if (!existsSync(directory)) { | ||
| return; | ||
| } | ||
|
|
||
| let files_ext = options.files ?? ["html", "js", "json", "css", "svg", "xml", "wasm"]; | ||
| const files = await glob(`**/*.{${files_ext.join()}}`, { | ||
| cwd: directory, | ||
| dot: true, | ||
| absolute: true, | ||
| filesOnly: true, | ||
| }); | ||
|
|
||
| let files_ext = | ||
| options.files ?? | ||
| Object.entries(db) | ||
| .filter(mime => mime[1].compressible) | ||
| .map(mime => mime[1].extensions) | ||
| .flat(1) | ||
| .filter(Boolean); | ||
| const files = [ | ||
| ...new Bun.Glob(`**/*.{${files_ext.join()}}`).scanSync({ | ||
| cwd: directory, | ||
| dot: true, | ||
| absolute: true, | ||
| onlyFiles: true, | ||
| }), | ||
| ]; | ||
| let doBr = false, | ||
| doGz = false; | ||
|
|
||
| if (options === true) { | ||
| doBr = doGz = true; | ||
| } else if (typeof options == "object") { | ||
| doBr = options.brotli ?? false; | ||
| doGz = options.gzip ?? false; | ||
| } | ||
|
|
||
| await Promise.all( | ||
| files.map(file => | ||
| Promise.all([doGz && compress_file(file, "gz"), doBr && compress_file(file, "br")]), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} file | ||
| * @param {'gz' | 'br'} format | ||
| */ | ||
| async function compress_file(file, format = "gz") { | ||
| /*** @param {string} file* @param {'gz' | 'br'} format*/ async function compress_file( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good but there are some formatting issues
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
i ran |
||
| file, | ||
| format = "gz", | ||
| ) { | ||
| const compress = | ||
| format == "br" | ||
| ? zlib.createBrotliCompress({ | ||
|
|
@@ -149,25 +131,17 @@ async function compress_file(file, format = "gz") { | |
| }, | ||
| }) | ||
| : zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION }); | ||
|
|
||
| const source = createReadStream(file); | ||
| const destination = createWriteStream(`${file}.${format}`); | ||
|
|
||
| await pipe(source, compress, destination); | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} out | ||
| */ | ||
| function patchServerWebsocketHandler(out) { | ||
| /*** @param {string} out*/ function patchServerWebsocketHandler(out) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
| let src = readFileSync(`${out}/index.js`, "utf8"); | ||
| const regex_gethook = /(this\.#options\.hooks\s+=\s+{)\s+(handle:)/gm; | ||
| const substr_gethook = `$1 \nhandleWebsocket: module.handleWebsocket || null,\n$2`; | ||
| const result1 = src.replace(regex_gethook, substr_gethook); | ||
|
|
||
| const regex_sethook = /(this\.#options\s+=\s+options;)/gm; | ||
| const substr_sethook = `$1\nthis.websocket = ()=>this.#options.hooks.handleWebsocket;`; | ||
| const result = result1.replace(regex_sethook, substr_sethook); | ||
|
|
||
| writeFileSync(`${out}/index.js`, result, "utf8"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,50 @@ | ||
| { | ||
| "name": "svelte-adapter-bun", | ||
| "version": "0.5.2", | ||
| "description": "Adapter for SvelteKit apps that generates a standalone Bun.js server.", | ||
| "author": "Volodymyr Palamar <@gornostay25>", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/gornostay25/svelte-adapter-bun.git" | ||
| }, | ||
| "main": "index.js", | ||
| "types": "index.d.ts", | ||
| "type": "module", | ||
| "devDependencies": { | ||
| "@sveltejs/kit": "^1.30.4", | ||
| "@types/mime-db": "^1.43.5", | ||
| "bun-types": "latest", | ||
| "mime-db": "^1.53.0", | ||
| "mrmime": "^2.0.0", | ||
| "prettier": "^3.2.5", | ||
| "totalist": "^3.0.1" | ||
| }, | ||
| "exports": { | ||
| ".": { | ||
| "types": "./index.d.ts", | ||
| "import": "./index.js" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/gornostay25/svelte-adapter-bun/issues" | ||
| }, | ||
| "description": "Adapter for SvelteKit apps that generates a standalone Bun.js server.", | ||
| "files": [ | ||
| "files", | ||
| "index.d.ts" | ||
| ], | ||
| "scripts": { | ||
| "build": "rm -fr files && bun run build.js", | ||
| "prepare": "bun run build", | ||
| "lint": "prettier --check .", | ||
| "format": "prettier --write ." | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/gornostay25/svelte-adapter-bun.git" | ||
| }, | ||
| "homepage": "https://github.com/gornostay25/svelte-adapter-bun#readme", | ||
| "keywords": [ | ||
| "svelte", | ||
| "sveltekit", | ||
| "sveltekit-adapter", | ||
| "bun" | ||
| ], | ||
| "author": "Volodymyr Palamar <@gornostay25>", | ||
| "license": "MIT", | ||
| "bugs": { | ||
| "url": "https://github.com/gornostay25/svelte-adapter-bun/issues" | ||
| }, | ||
| "homepage": "https://github.com/gornostay25/svelte-adapter-bun#readme", | ||
| "devDependencies": { | ||
| "@sveltejs/kit": "^1.30.4", | ||
| "bun-types": "latest", | ||
| "mrmime": "^2.0.0", | ||
| "prettier": "^3.2.5", | ||
| "totalist": "^3.0.1" | ||
| "scripts": { | ||
| "build": "rm -fr files && bun run build.js", | ||
| "prepare": "bun run build", | ||
| "lint": "prettier --check .", | ||
| "format": "prettier --write ." | ||
| }, | ||
| "dependencies": { | ||
| "tiny-glob": "^0.2.9" | ||
| } | ||
| "type": "module", | ||
| "types": "index.d.ts" | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why you removed this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because its way more than those anymore