forked from moebooru/moebooru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·104 lines (95 loc) · 2.87 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
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
#!/usr/bin/env node
import babel from '@babel/core';
import { createHash } from 'crypto';
import esbuild from 'esbuild';
import coffeeScriptPlugin from 'esbuild-coffeescript';
import { lessLoader } from 'esbuild-plugin-less';
import fsPromises from 'fs/promises';
import { globSync } from 'glob';
const outdir = 'app/assets/builds';
const plugins = [
coffeeScriptPlugin({
bare: true,
inlineMap: true
}),
lessLoader({
rootpath: '',
sourceMap: {
sourceMapFileInline: false
}
}),
{
name: 'babel',
setup (build) {
build.onEnd(async () => {
const esbuildFilepath = `${outdir}/application.js`;
const inputSourceMap = JSON.parse(await fsPromises.readFile(`${esbuildFilepath}.map`));
const options = {
inputSourceMap,
minified: true,
presets: [
['@babel/preset-env']
],
sourceMaps: true
};
const esbuildOutput = await fsPromises.readFile(esbuildFilepath);
const result = await babel.transformAsync(esbuildOutput, options);
const filename = 'application.jsout';
const outfileBabel = `${outdir}/${filename}`;
result.map.sources = result.map.sources
// CoffeeScript sourcemap and Esbuild sourcemap combined generates duplicated source paths
.map((path) => path.replace(/\.\.\/\.\.\/javascript(\/.+)?\/app\/javascript\//, '../../javascript/'));
const resultMap = JSON.stringify(result.map);
const resultMapHash = createHash('sha256').update(resultMap).digest('hex');
// add hash so it matches sprocket output
fsPromises.writeFile(outfileBabel, `${result.code}\n//# sourceMappingURL=${filename}-${resultMapHash}.map`);
fsPromises.writeFile(`${outfileBabel}.map`, JSON.stringify(result.map));
});
}
},
{
name: 'analyze',
setup (build) {
build.onEnd(async (result) => {
if (options.analyze) {
const analyzeResult = await esbuild.analyzeMetafile(result.metafile);
console.log(analyzeResult);
}
});
}
},
{
name: 'log',
setup (build) {
let startTime = Date.now();
build.onStart(() => {
startTime = Date.now();
console.log(new Date(), 'Build started');
});
build.onEnd(() => {
console.log(new Date(), `Build finished (${Date.now() - startTime}ms)`);
});
}
}
];
const args = process.argv.slice(2);
const options = {
watch: args.includes('--watch'),
analyze: args.includes('--analyze')
};
const config = {
bundle: true,
entryPoints: globSync('app/javascript/*.*'),
external: ['*.gif', '*.png'],
metafile: options.analyze,
nodePaths: ['app/javascript'],
outdir,
plugins,
resolveExtensions: ['.coffee', '.js'],
sourcemap: 'external'
};
if (options.watch) {
(await esbuild.context(config)).watch();
} else {
esbuild.build(config);
}