-
Notifications
You must be signed in to change notification settings - Fork 1
/
esbuild.mjs
81 lines (71 loc) · 2.22 KB
/
esbuild.mjs
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 * as esbuild from 'esbuild';
import { sassPlugin } from "esbuild-sass-plugin";
import fileSystem from 'fs';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
const ENVIRONMENT = process.env.NODE_ENV || 'development';
const WATCH = process.argv.includes('--watch');
console.time('Build time');
const buildCss = async () => {
await esbuild.build({
entryPoints: ['src/scss/comparing-slider.scss'],
outfile: 'dist/comparing-slider.min.css',
bundle: true,
minify: true,
plugins: [
sassPlugin({
async transform(source) {
const { css } = await postcss([
autoprefixer
])
.process(source, { from: undefined });
return css;
},
cssImports: true,
}),
],
define: {
'process.env.NODE_ENV': `"${ENVIRONMENT}"`,
},
});
}
const buildJs = async () => {
await esbuild.build({
entryPoints: ['src/comparing-slider.js'],
bundle: true,
minify: true,
metafile: process.env.NODE_ENV !== 'production',
sourcemap: process.env.NODE_ENV !== 'production',
outfile: 'dist/comparing-slider.min.js',
define: {
'process.env.NODE_ENV': `"${ENVIRONMENT}"`,
}
});
}
try {
buildCss();
buildJs();
} catch (e) {
console.error(e);
}
console.timeEnd('Build time');
if (WATCH) {
console.log('Watching for changes...');
console.log();
const watcher = fileSystem.watch('src', { recursive: true }, async (eventType, filename) => {
if (filename.includes('scss')) {
console.log(`${new Date().toLocaleTimeString()} Recompiling css ...`);
await buildCss();
} else if (filename.includes('js')) {
console.log(`${new Date().toLocaleTimeString()} Recompiling javascript ...`);
await buildJs();
}
if (filename.includes('scss') || filename.includes('js')) {
console.log(`${new Date().toLocaleTimeString()} Done`);
}
});
process.on('SIGINT', () => {
watcher.close();
process.exit();
});
}