-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.config.mjs
More file actions
89 lines (71 loc) · 1.95 KB
/
build.config.mjs
File metadata and controls
89 lines (71 loc) · 1.95 KB
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
import path from 'path';
import fs from 'fs';
import {glob} from 'glob'
import { rollup } from 'rollup'
import resolve from "@rollup/plugin-node-resolve"
import commonjs from '@rollup/plugin-commonjs';
const isWatchCommand = process.argv.includes('--watch')
const buildDirectory = path.join(process.cwd(), "app/assets/builds")
const javascriptDirectory = "test/dummy/app/javascript"
const getEntrypointsMapping = async () => {
const entrypoints = await glob(path.join(process.cwd(), javascriptDirectory, "*.js"))
var mapping = {}
entrypoints.forEach(x => {
mapping[path.basename(x, '.js')] = x
})
return mapping
}
const buildEntrypoints = async () => {
const inputs = await getEntrypointsMapping()
var outputPlugins = [resolve(), commonjs()]
const inputOptions = {
input: inputs,
plugins: outputPlugins
}
const outputOptions = {
dir: "test/dummy/app/assets/builds/",
format: "esm",
sourcemap: true,
chunkFileNames: "[name]-[hash].digested.js"
}
let bundle;
try {
bundle = await rollup(inputOptions);
const { output } = await bundle.write(outputOptions);
console.log("✅ build succeeded")
} catch (error) {
console.error(error);
}
if (bundle) {
await bundle.close();
}
}
const build = async (config) => {
const result = await Bun.build(config);
if (!result.success) {
if (isWatchCommand) {
console.error("🚨 Build failed");
for (const message of result.logs) {
console.error(message);
}
return;
} else {
throw new AggregateError(result.logs, "Build failed");
}
}
return result
};
const buildAll = async () => {
await buildEntrypoints();
}
(async () => {
await buildAll()
if (isWatchCommand) {
fs.watch(path.join(process.cwd(), javascriptDirectory), { recursive: true }, (eventType, filename) => {
console.log(`File changed: ${filename}. Rebuilding...`);
buildAll();
});
} else {
process.exit(0);
}
})();