Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Palamar (gornostay25) committed Aug 27, 2023
1 parent 856afd1 commit d0312c6
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 2,224 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
insert_final_newline = true
12 changes: 12 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.DS_Store
node_modules
/files
.env
.env.*
!.env.example
**/*.d.ts

# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock
14 changes: 14 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
root: true,
extends: ["eslint:recommended", "prettier"],
env: {
browser: true,
es2017: true,
node: true,
},
rules: {
"no-console": ["error", { allow: ["warn", "error", "info"] }],
"no-debugger": "error",
"no-eval": "error",
},
};
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.DS_Store
node_modules
.DS_Store
node_modules
/files
12 changes: 6 additions & 6 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"semi": true,
"trailingComma": "es5",
"useTabs": false,
"bracketSpacing": true,
"bracketSameLine": true,
"arrowParens": "avoid",
"bracketSameLine": true,
"bracketSpacing": true,
"endOfLine": "lf",
"printWidth": 100
"printWidth": 100,
"semi": true,
"trailingComma": "all",
"useTabs": false
}
40 changes: 20 additions & 20 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2022 Volodymyr Palamar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
MIT License
Copyright (c) 2022 Volodymyr Palamar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@

[Adapter](https://kit.svelte.dev/docs/adapters) for SvelteKit apps that generates a standalone [Bun](https://github.com/oven-sh/bun) server.

> **Warning** > **This is experimental module!** Bun does not support many things.
> So I can't promise it will work
> 🩼 Added polyfill for
>
> - [`File`](https://github.com/oven-sh/bun/issues/621#issuecomment-1396462734)
>
> You can add your polyfills in [`src/polyfills.js`](src/polyfills.js) file.
## :zap: Usage

Install with `bun add -d svelte-adapter-bun` or `npm i -D svelte-adapter-bun`, then add the adapter to your `svelte.config.js`:
Install with `bun add -d svelte-adapter-bun`, then add the adapter to your `svelte.config.js`:

```js
// svelte.config.js
Expand Down Expand Up @@ -142,6 +133,22 @@ export const handleWebsocket = {
};
```

## Polyfills

If you need to use polyfills in your app, you can add them to the [`src/polyfills.js`](src/polyfills.js) file:

```js
class Polifill {
constructor() {
...
}
}

const globals = {
Polifill,
};
```

## :desktop_computer: Environment variables

> Bun automatically reads configuration from `.env.local`, `.env.development` and `.env`
Expand Down
34 changes: 10 additions & 24 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import * as esbuild from "esbuild";
import fs from "fs/promises";

await fs.mkdir("files");

await Promise.all([
esbuild.build({
entryPoints: ["src/index.js"],
outdir: "files",
bundle: true,
// minify: true,
minifySyntax: true,
format: "esm",
target: ["esnext"],
platform: "node",
external: ["./mime.conf.js", "bun", "SERVER", "MANIFEST"],
legalComments: "external",
}),
fs.copyFile("src/.env.example", "files/.env.example"),
esbuild.build({
entryPoints: ["src/mime.conf.js"],
outfile: "files/mime.conf.js",
format: "esm",
target: ["esnext"],
}),
]);
await fs.rm("./files", { recursive: true, force: true });
await Bun.build({
entrypoints: ["src/index.js", "src/mime.conf.js"],
outdir: "./files",
splitting: true,
external: ["SERVER", "MANIFEST"],
format: "esm",
target: "bun",
});
await Promise.all([fs.copyFile("src/.env.example", "files/.env.example")]);
Binary file added bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ interface ServerWebSocket<T = undefined> {
publish(
topic: string,
data: string | ArrayBufferView | ArrayBuffer,
compress?: boolean
compress?: boolean,
): ServerWebSocketSendStatus;

/**
Expand Down Expand Up @@ -460,8 +460,8 @@ export interface WebSocketHandler<T = undefined> {
* This value is passed to the {@link ServerWebSocket.data} property
*/
data?: T;
}
) => boolean
},
) => boolean,
) => boolean;
/**
* Handle an incoming message to a {@link ServerWebSocket}
Expand Down
68 changes: 34 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,34 @@ export default function (opts = {}) {
name: "svelte-adapter-bun",
async adapt(builder) {
builder.rimraf(out);
builder.mkdirp(out);

builder.log.minor("Copying assets");
builder.writeClient(`${out}/client`);
builder.writeServer(`${out}/server`);
builder.writePrerendered(`${out}/prerendered`);
builder.writeClient(`${out}/client${builder.config.kit.paths.base}`);
builder.writePrerendered(`${out}/prerendered${builder.config.kit.paths.base}`);

builder.log.minor("Patching server (websocket support)");
patchServerWebsocketHandler(out);
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`
`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",
Expand All @@ -57,14 +69,6 @@ export default function (opts = {}) {
},
});

if (precompress) {
builder.log.minor("Compressing assets");
await compress(`${out}/client`, precompress);
await compress(`${out}/static`, precompress);
await compress(`${out}/prerendered`, precompress);
builder.log.success("Compression success");
}

let package_data = {
name: "bun-sveltekit-app",
version: "0.0.0",
Expand All @@ -77,20 +81,16 @@ export default function (opts = {}) {
dependencies: { cookie: "latest", devalue: "latest", "set-cookie-parser": "latest" },
};

if (process.env.npm_package_json) {
try {
let packageraw = readFileSync(process.env.npm_package_json, { encoding: "utf-8" });
let package_json = JSON.parse(packageraw);
package_json.name && (package_data.name = package_json.name);
package_json.version && (package_data.version = package_json.version);
package_json.dependencies &&
(package_data.dependencies = {
...package_json.dependencies,
...package_data.dependencies,
});
} catch (error) {
builder.log.warn(`Parse package.json error: ${error.message}`);
}
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,
});
} catch (error) {
builder.log.warn(`Parse package.json error: ${error.message}`);
}

writeFileSync(`${out}/package.json`, JSON.stringify(package_data, null, "\t"));
Expand Down Expand Up @@ -129,8 +129,8 @@ async function compress(directory, options) {

await Promise.all(
files.map(file =>
Promise.all([doGz && compress_file(file, "gz"), doBr && compress_file(file, "br")])
)
Promise.all([doGz && compress_file(file, "gz"), doBr && compress_file(file, "br")]),
),
);
}

Expand Down Expand Up @@ -160,7 +160,7 @@ async function compress_file(file, format = "gz") {
* @param {string} out
*/
function patchServerWebsocketHandler(out) {
let src = readFileSync(`${out}/server/index.js`, "utf8");
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);
Expand All @@ -169,5 +169,5 @@ function patchServerWebsocketHandler(out) {
const substr_sethook = `$1\nthis.websocket = ()=>this.#options.hooks.handleWebsocket;`;
const result = result1.replace(regex_sethook, substr_sethook);

writeFileSync(`${out}/server/index.js`, result, "utf8");
writeFileSync(`${out}/index.js`, result, "utf8");
}
Loading

0 comments on commit d0312c6

Please sign in to comment.