forked from dothq/browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuse.js
129 lines (104 loc) · 2.62 KB
/
fuse.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
const {
FuseBox,
WebIndexPlugin,
QuantumPlugin,
EnvPlugin,
CopyPlugin,
JSONPlugin,
StyledComponentsPlugin,
} = require('fuse-box');
const { spawn } = require('child_process');
const production = process.env.NODE_ENV === 'dev' ? false : true;
const getConfig = (target, name) => {
return {
homeDir: 'src/',
cache: !production,
target,
output: `build/$name.js`,
useTypescriptCompiler: true,
plugins: [
EnvPlugin({ NODE_ENV: production ? 'production' : 'development' }),
production &&
QuantumPlugin({
bakeApiIntoBundle: name,
treeshake: true,
removeExportsInterop: false,
uglify: {
es6: true,
},
}),
],
alias: {
'~': '~/',
},
log: {
showBundledFiles: false,
},
};
};
const getRendererConfig = (target, name) => {
const cfg = Object.assign({}, getConfig(target, name), {
sourceMaps: !production,
});
return cfg;
};
const getWebIndexPlugin = name => {
return WebIndexPlugin({
template: `static/pages/${name}.html`,
path: production ? '.' : '/',
target: `${name}.html`,
bundles: [name],
});
};
const getCopyPlugin = () => {
return CopyPlugin({
files: ['*.woff2', '*.png', '*.svg'],
dest: 'assets',
resolve: production ? './assets' : '/assets',
});
};
const main = () => {
const fuse = FuseBox.init(getConfig('server', 'main'));
const app = fuse.bundle('main').instructions(`> [main/index.ts]`);
if (!production) {
app.watch();
}
fuse.run();
};
const renderer = (name, port) => {
const cfg = getRendererConfig('electron', name);
cfg.plugins.push(getWebIndexPlugin(name));
cfg.plugins.push(JSONPlugin());
cfg.plugins.push(getCopyPlugin());
cfg.plugins.push(StyledComponentsPlugin());
const fuse = FuseBox.init(cfg);
if (!production) {
fuse.dev({ httpServer: true, port, socketURI: `ws://localhost:${port}` });
}
const app = fuse.bundle(name).instructions(`> [renderer/${name}/index.tsx]`);
if (!production) {
app.hmr({ port, socketURI: `ws://localhost:${port}` }).watch();
if (name === 'app') {
return fuse.run().then(() => {
const child = spawn('npm', ['start'], {
shell: true,
stdio: 'inherit',
});
});
}
}
fuse.run();
};
const preload = name => {
const cfg = getRendererConfig('electron', name);
const fuse = FuseBox.init(cfg);
const app = fuse.bundle(name).instructions(`> [preloads/${name}.ts]`);
if (!production) {
app.watch();
}
fuse.run();
};
renderer('app', 4444);
preload('view-preload');
preload('background-preload');
main();