This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
build.ts
81 lines (69 loc) · 2.63 KB
/
build.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
import { BundleType } from '../bundle/bundle';
import { createBundleRouter } from '../bundle/bundleRouter';
import { createServerProcess } from '../devServer/server';
import { IBundleContext } from '../moduleResolver/bundleContext';
import { IModule } from '../moduleResolver/module';
import { createServerEntry } from '../production/module/ServerEntry';
import { ISplitEntries } from '../production/module/SplitEntries';
import { IRunOnCompleteHandler, IRunResponse } from './IRunResponse';
import { Context } from './context';
interface IBuildProps {
bundleContext: IBundleContext;
ctx: Context;
entries: Array<IModule>;
modules: Array<IModule>;
rebundle?: boolean;
splitEntries?: ISplitEntries;
}
export const createBuild = async (props: IBuildProps): Promise<IRunResponse> => {
const { bundleContext, ctx, entries, modules, rebundle, splitEntries } = props;
const router = createBundleRouter({ ctx, entries });
await router.init(modules);
router.generateBundles(modules);
if (splitEntries && splitEntries.entries.length > 0) {
router.generateSplitBundles(splitEntries.entries);
}
const bundles = await router.writeBundles();
// create a server bundle if we have more than 1 bundle in a server setup
if (ctx.config.target === 'server' || ctx.config.target === 'electron') {
const indexedTypes = [BundleType.JS_APP, BundleType.JS_VENDOR];
let indexedAmount = 0;
for (const item of bundles) {
if (indexedTypes.includes(item.bundle.type)) indexedAmount++;
}
if (indexedAmount > 1) bundles.push(await createServerEntry(ctx, bundles));
}
// write the manifest
const manifest = await router.writeManifest(bundles);
if (bundleContext.cache && ctx.config.isDevelopment) await bundleContext.cache.write();
if (bundleContext.moduleIdCacheWhenCacheDisabled) {
bundleContext.moduleIdCacheWhenCacheDisabled.write();
}
ctx.isWorking = false;
const onCompleteHandler: IRunOnCompleteHandler = {
get electron() {
return createServerProcess({ bundles, ctx, processName: require('electron') });
},
get server() {
return createServerProcess({ bundles, ctx, processName: 'node' });
},
};
const response: IRunResponse = {
bundleContext,
bundles,
entries,
manifest,
modules,
onComplete: handler => handler(onCompleteHandler),
onWatch: userFn => {
userFn(bundles);
ctx.ict.on('rebundle', () => userFn(bundles));
},
};
if (rebundle) {
ctx.ict.sync('rebundle', response);
} else {
await ctx.ict.send('complete', response);
}
return response;
};