-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild.js
156 lines (136 loc) · 4 KB
/
esbuild.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const DEV = process.argv.includes('--dev');
// Svelte compile configuration
const malinaConfig = {
css: false, //use `css:true` to inline CSS in `bundle.js`
compact: true
};
/* Edit this file below only if know what you doing! */
const { fork } = require("child_process");
const { build } = require("esbuild");
const { createRemote } = require("derver");
const {malinaPlugin} = require("malinajs/malina-esbuild");
const {sveltePaths} = require('esbuild-svelte-paths');
const watch = require("node-watch");
const path = require("path");
const CWD = process.cwd();
const remote = DEV && createRemote('malina_fullstack_template');
(async()=>{
const bundleServer = await build_server();
const bundleClient = await build_client();
await build_cli();
if(DEV){
nodemon(path.join(CWD,'dist','app.js'),{cwd:path.join(CWD,'dist')});
watch(path.join(CWD,'src','client'),{ recursive: true }, async function() {
try {
await bundleClient.rebuild();
} catch (err){
remote.error(err.message,'Svelte compile error');
}
});
watch(path.join(CWD,'src','server'),{ recursive: true }, async function() {
await bundleServer.rebuild();
await bundleClient.rebuild();
console.log('Restarting server...');
});
}
})();
async function build_server(){
return await build({
entryPoints: ['src/server/main.js'],
bundle: true,
outfile: 'dist/app.js',
platform: 'node',
sourcemap: DEV, // Use `DEV && 'inline'` to inline sourcemaps to the bundle
minify: !DEV,
incremental: DEV,
plugins:[
plugin_server()
]
});
}
async function build_cli(){
return await build({
entryPoints: ['src/cli/tcpsrv.js'],
bundle: true,
outfile: 'dist/tcpsrv.js',
platform: 'node',
minify: true
});
}
async function build_client(){
return await build({
entryPoints: ['src/client/main.js'],
bundle: true,
outfile: 'dist/static/build/bundle.js',
sourcemap: DEV, // Use `DEV && 'inline'` to inline sourcemaps to the bundle
minify: !DEV,
incremental: DEV,
plugins: [
sveltePaths( {extension: 'xht'} ),
malinaPlugin(malinaConfig)
]
});
}
function plugin_server(){
return {
name: 'server-plugin',
setup(b) {
b.onResolve({ filter: /^@server$/ }, () => {
return { path: DEV ? 'server_development.js' : 'server_production.js', namespace: 'server' };
});
b.onLoad({ filter: /^server_development\.js$/, namespace: 'server'}, () => {
return {
contents: `
import {derver} from "derver";
import path from "path";
const DIR = path.join(__dirname,'static');
export default function (options){
return derver({
dir: path.join(__dirname,'static'),
...options,
remote: 'malina_fullstack_template'
});
}
`,
resolveDir: CWD
};
});
b.onLoad({ filter: /^server_production\.js$/, namespace: 'server'}, () => {
return {
contents: `
import {derver} from "derver";
import path from "path";
const DIR = path.join(__dirname,'static');
export default function (options){
return derver({
dir: path.join(__dirname,'static'),
cache: true,
compress: true,
watch: false,
host: "0.0.0.0",
...options
});
}
`,
resolveDir: CWD
};
});
}
};
}
function nodemon(path,options){
let child;
const kill = ()=>{
child && child.kill();
};
const start = () => {
child = fork(path, [], options);
};
process.on('SIGTERM', kill);
process.on('exit', kill);
start();
watch(path,()=>{
kill();
start();
});
}