-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
89 lines (74 loc) · 2.39 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
import { writeFileSync } from 'fs';
// import { builtinModules } from 'module';
import { fileURLToPath } from 'url';
import { rollup } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
const files = fileURLToPath(new URL('./files', import.meta.url));
/** @type {import('.').default} */
export default function (opts = {}) {
const {
out = 'build',
precompress = false,
envPrefix = '',
deps = fileURLToPath(new URL('./deps.ts', import.meta.url))
} = opts;
return {
name: 'svelte-adapter-deno',
async adapt(builder) {
const tmp = builder.getBuildDirectory('deno');
builder.rimraf(out);
builder.rimraf(tmp);
builder.mkdirp(tmp);
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([
builder.compress(`${out}/client`),
builder.compress(`${out}/prerendered`)
]);
}
builder.log.minor('Building server');
builder.writeServer(tmp);
writeFileSync(
`${tmp}/manifest.js`,
`export const manifest = ${builder.generateManifest({ relativePath: './' })};`
);
// const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
// we bundle the Vite output so that deployments only need
// their production dependencies. Anything in devDependencies
// will get included in the bundled code
const bundle = await rollup({
input: {
index: `${tmp}/index.js`,
manifest: `${tmp}/manifest.js`
},
external: [
// dependencies could have deep exports, so we need a regex
// ...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`))
],
plugins: [nodeResolve({ preferBuiltins: true }), commonjs(), json()]
});
await bundle.write({
dir: `${out}/server`,
format: 'esm',
sourcemap: true,
chunkFileNames: 'chunks/[name]-[hash].js'
});
builder.copy(files, out, {
replace: {
ENV: './env.js',
HANDLER: './handler.js',
MANIFEST: './server/manifest.js',
SERVER: './server/index.js',
ENV_PREFIX: JSON.stringify(envPrefix)
}
});
builder.log.minor(`Copying deps.ts: ${deps}`);
builder.copy(deps, `${out}/deps.ts`);
}
};
}