-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
141 lines (120 loc) · 3.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { createReadStream, createWriteStream, existsSync, readFileSync, statSync, writeFileSync } from 'fs';
import { pipeline } from 'stream';
import glob from 'tiny-glob';
import { fileURLToPath } from 'url';
import { promisify } from 'util';
import zlib from 'zlib';
const pipe = promisify(pipeline);
const files = fileURLToPath(new URL('./files', import.meta.url).href);
/** @type {import('.').default} */
export default function (opts = {}) {
const { out = 'build', precompress = false, envPrefix = '', development = false, dynamic_origin = false, xff_depth = 1, assets = true } = opts;
return {
name: 'svelte-adapter-bun',
async adapt(builder) {
builder.rimraf(out);
builder.log.minor('Copying assets');
builder.writeClient(`${out}/client`);
builder.writeServer(`${out}/server`);
builder.writeStatic(`${out}/static`);
builder.writePrerendered(`${out}/prerendered`);
writeFileSync(
`${out}/manifest.js`,
`export const manifest = ${builder.generateManifest({
relativePath: './server'
})};\n`
);
builder.copy(files, out, {
replace: {
SERVER: './server/index.js',
MANIFEST: './manifest.js',
ENV_PREFIX: JSON.stringify(envPrefix),
dotENV_PREFIX: envPrefix,
BUILD_OPTIONS: JSON.stringify({ development, dynamic_origin, xff_depth, assets })
}
});
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-the-best",
"version": "0.0.0",
"type": "module",
"private": true,
"main": "index.js",
"scripts": {
"start": "./index.js"
},
"dependencies": {}
};
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);
} 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 /build/index.js")
}
};
}
/**
* @param {string} directory
* @param {import('.').CompressOptions} options
*/
async function compress(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 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') {
const compress =
format == 'br'
? zlib.createBrotliCompress({
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
}
})
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });
const source = createReadStream(file);
const destination = createWriteStream(`${file}.${format}`);
await pipe(source, compress, destination);
}