This repository was archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
88 lines (76 loc) · 2.7 KB
/
index.ts
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
import { Platform, BrowserOptions, PlatformResult } from '../../compiler/interfaces';
import { RollupOptions } from 'rollup';
import nodePath from 'path';
import write from 'write';
import defaultServerCode from './server.jstxt';
import nativeNodeModules from '../../compiler/utils/native-node-modules';
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { writeTemplate } from '../../compiler/utils/plugin-common';
import { runServer, runClient } from './run';
import liveReloader from './live-reload.jstxt';
import serviceWorkerRegistar from './service-worker-registar.jstxt';
import { read } from '../../compiler/utils/read';
const browserPlatform = (options: BrowserOptions = {}): Platform => async (buildOptions, logger) => {
const log = logger('browser');
// Set the default options
options = {
bundlePath: options.bundlePath || `bundle.js`,
tag: options.tag || `browser`,
// entryFile will be set later
workerFile: options.workerFile || `browser/service-worker.js`,
};
// Set a default entry file
if (!options.entryFile) {
options.entryFile = nodePath.join(buildOptions.outDir, options.tag, `.pre-server.js`);
await write(options.entryFile, defaultServerCode);
log.info(`options.entryFile not specified, wrote a default '.pre-server.js' file instead.`);
}
// Write the service-worker
const worker = await read(options.workerFile);
await write(nodePath.join(buildOptions.outDir, options.tag, `service-worker.js`), worker);
log.info(`Migrated service worker to 'service-worker.js'`);
// Create a server build
const serverBuild: RollupOptions = {
input: options.entryFile,
output: {
file: nodePath.join(buildOptions.outDir, options.tag, `.server.js`),
format: `cjs`,
},
external: nativeNodeModules,
plugins: [
nodeResolve({
preferBuiltins: true,
}),
commonjs(),
buildOptions.object === `run` && runServer(options, buildOptions),
],
};
// Assemble the platform
const clientRunner = buildOptions.object === `run` ? runClient() : { name: `nothing` };
const toReturn: PlatformResult = {
tag: options.tag,
data: `browser`,
isSandboxed: () => true,
bundlePath: () => options.bundlePath,
runtimePath: () => `.runtime.js`,
plugin: () => ({
name: `nothing`,
buildStart: async () => {
await writeTemplate(nodePath.join(buildOptions.outDir, options.tag), { appEntry: options.bundlePath });
},
banner: () => {
let code = ``;
if (buildOptions.object === `run`) code += liveReloader;
code += serviceWorkerRegistar;
return code;
},
}),
run: () => clientRunner,
assetsPath: () => ``,
extraBuilds: () => [serverBuild],
};
// Return
return toReturn;
};
export default browserPlatform;