-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·162 lines (135 loc) · 4.04 KB
/
cli.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
157
158
159
160
161
162
#!/usr/bin/env node
import esbuild from 'esbuild';
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import readline from 'readline';
import { create } from 'browser-sync';
import { execSync as exec } from 'child_process';
import { sassPlugin as styles } from 'esbuild-sass-plugin';
import * as sass from 'sass';
const cwd = process.cwd();
const src = path.resolve(cwd, 'src');
const server = create();
const command = process.argv[2];
if (!['build', 'watch'].includes(command)) {
console.log(`Invalid command: ${chalk.red(command)}\n`);
process.exit();
}
const logger = (options = {}) => ({
name: 'logger',
setup(context) {
const path = context.initialOptions.outdir;
const slug = Object.keys(context.initialOptions.entryPoints)[0];
context.onStart(() => {
server.instance.active && server.info();
});
context.onEnd(result => {
if (result.warnings.length || result.errors.length) {
console.log('\x07');
return;
}
const css = `${path}/${slug}.css`;
const js = `${path}/${slug}.js`;
console.log(` ${chalk.green('✓')} ${css} ${Math.round(fs.statSync(css).size / 1000)} KB`);
console.log(` ${chalk.green('✓')} ${js} ${Math.round(fs.statSync(js).size / 1000)} KB`);
console.log();
});
}
});
const buildOptions = {
entryPoints: {'main.min': src},
alias: {'~': src},
outdir: 'build',
bundle: true,
minify: true,
target: 'es2019',
external: 'jpg,jpeg,webp,png,gif,svg,woff,woff2'.split(',').map(ext => `*.${ext}`),
logLevel: 'warning',
sourcemap: false,
treeShaking: true,
legalComments: 'none',
loader: {
'.glsl': 'text',
'.vert': 'text',
'.frag': 'text',
},
plugins: [
styles({sourceMap: false, logger: sass.Logger.silent}),
logger()
]
};
const watchOptions = Object.assign({}, buildOptions, {
entryPoints: {'main.dev': src},
minify: false,
logLevel: 'silent',
sourcemap: 'inline',
plugins: [
styles({sourceMap: true})
]
});
const serveOptions = {
proxy: `${process.env.npm_package_name}.test`,
files: ['assets/**', 'build/*', '**/*.php', '**/*.html'],
host: 'localhost',
open: false,
notify: false,
logLevel: 'silent',
injectChanges: false,
ui: false,
};
server.info = function() {
const proxying = this.url();
const external = this.getOption('urls').get('external');
console.clear();
console.log();
console.log(` ➜ ${chalk.bold('Proxying')}: ${chalk.green(proxying)}`);
console.log(` ➜ ${chalk.bold('External')}: ${chalk.cyan(external || 'offline')}\n`);
console.log(` ${chalk.bold('Shortcuts')}`);
for (const [key, [_, tip]] of Object.entries(shortcuts)) {
tip && console.log(chalk.grey(` press ${chalk.white.bold(key)} to ${tip}`));
}
console.log();
};
server.url = function() {
const host = this.getOption('proxy').get('target');
const port = this.getOption('port');
return `${host}:${port}`;
};
const shortcuts = {
o: [() => exec(`open ${server.url()}`), 'open in browser'],
r: [() => server.reload(), 'reload the page'],
q: [() => process.exit(), 'quit'],
'\x03': [() => process.exit()],
};
async function main() {
const watcher = await esbuild.context(watchOptions);
const builder = await esbuild.context(buildOptions);
if (command === 'build') {
await watcher.rebuild();
await builder.rebuild();
process.exit();
}
server.init(serveOptions, () => {
builder.watch();
watcher.watch();
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (_, key) => {
if (!key) return;
shortcuts[key.sequence]?.[0]();
});
});
}
if (fs.existsSync(process.argv[3])) {
function extend(options, config) {
if (typeof config === 'undefined') return;
Object.assign(options, typeof config === 'function' ? config(options) : config);
}
import(`${cwd}/${process.argv[3]}`).then(settings => {
extend(watchOptions, settings.watch);
extend(buildOptions, settings.build);
extend(serveOptions, settings.serve);
main();
});
} else { main(); }