-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
58 lines (50 loc) · 1.57 KB
/
build.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
#!/usr/bin/env node
import estrella from "estrella";
import fsExtra from "fs-extra";
import chalk from "chalk";
import sass from "sass";
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const { build, watch } = estrella;
const { remove, copy, writeFile } = fsExtra
const cssEntryFile = "src/styles/app.scss";
const cssTargetFile = "dist/static/app.css";
async function buildStyles(config) {
const result = sass.renderSync({
file: cssEntryFile,
outputStyle: config.debug ? 'expanded' : 'compressed',
});
try {
await writeFile(cssTargetFile, result.css)
console.log(chalk.greenBright(`Wrote ${cssTargetFile}`))
} catch (e) {
console.warn(chalk.yellow(`Could not write ${cssTargetFile}`))
}
return result.stats.includedFiles;
}
build({
entry: __dirname + "/src/index.ts",
bundle: true,
outdir: __dirname + "/dist",
minify: false,
external: ["__STATIC_CONTENT_MANIFEST"],
outExtension: { ".js": ".mjs" },
format: "esm",
onStart: async (config, changedFiles, context) => {
const isInitialBuild = changedFiles.length === 0;
if (isInitialBuild) {
try {
await copy("static", "dist/static", { recursive: true });
} catch (e) {
console.warn(chalk.yellow("Could not remove existing dist folder and copy static assets (maybe you are running wrangler dev?)"))
}
const cssInputFiles = await buildStyles(config);
if (config.watch) {
watch(cssInputFiles, f => {
buildStyles(config);
});
}
}
}
});