2022-02-02 00:45:52 +09:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2022-02-04 04:46:03 +09:00
|
|
|
import babel from '@babel/core'
|
2022-02-05 23:41:35 +09:00
|
|
|
import { createHash } from 'crypto'
|
2022-02-02 00:49:55 +09:00
|
|
|
import esbuild from 'esbuild'
|
2022-02-04 06:27:00 +09:00
|
|
|
import coffeeScriptPlugin from 'esbuild-coffeescript'
|
2022-02-12 06:05:38 +09:00
|
|
|
import { lessLoader } from 'esbuild-plugin-less'
|
2022-02-04 06:27:00 +09:00
|
|
|
import fsPromises from 'fs/promises'
|
2022-02-12 06:05:38 +09:00
|
|
|
import glob from 'glob'
|
2022-02-04 04:46:03 +09:00
|
|
|
|
2022-02-12 06:05:38 +09:00
|
|
|
const outdir = 'app/assets/builds'
|
2022-02-04 04:46:03 +09:00
|
|
|
|
2022-02-05 23:41:35 +09:00
|
|
|
const plugins = [
|
|
|
|
coffeeScriptPlugin({
|
|
|
|
bare: true,
|
|
|
|
inlineMap: true
|
|
|
|
}),
|
2022-02-12 06:05:38 +09:00
|
|
|
lessLoader({
|
|
|
|
rootpath: '',
|
|
|
|
sourceMap: {
|
|
|
|
sourceMapFileInline: false
|
|
|
|
}
|
|
|
|
}),
|
2022-02-05 23:41:35 +09:00
|
|
|
{
|
|
|
|
name: 'babel',
|
|
|
|
setup (build) {
|
|
|
|
build.onEnd(async () => {
|
2022-02-12 06:05:38 +09:00
|
|
|
const esbuildFilepath = `${outdir}/application.js`
|
|
|
|
const inputSourceMap = JSON.parse(await fsPromises.readFile(`${esbuildFilepath}.map`))
|
2022-02-05 23:41:35 +09:00
|
|
|
const options = {
|
2022-02-12 06:05:38 +09:00
|
|
|
inputSourceMap,
|
2022-02-05 23:41:35 +09:00
|
|
|
minified: true,
|
|
|
|
presets: [
|
|
|
|
['@babel/preset-env']
|
|
|
|
],
|
|
|
|
sourceMaps: true
|
|
|
|
}
|
2022-02-12 06:05:38 +09:00
|
|
|
const esbuildOutput = await fsPromises.readFile(esbuildFilepath)
|
|
|
|
const result = await babel.transformAsync(esbuildOutput, options)
|
|
|
|
const filename = 'application.jsout'
|
|
|
|
const outfileBabel = `${outdir}/${filename}`
|
2022-02-05 23:41:35 +09:00
|
|
|
result.map.sources = result.map.sources
|
|
|
|
// CoffeeScript sourcemap and Esbuild sourcemap combined generates duplicated source paths
|
2022-02-12 06:05:38 +09:00
|
|
|
.map((path) => path.replace(/\.\.\/\.\.\/javascript(\/.+)?\/app\/javascript\//, '../app/javascript/'))
|
2022-02-05 23:41:35 +09:00
|
|
|
const resultMap = JSON.stringify(result.map)
|
|
|
|
const resultMapHash = createHash('sha256').update(resultMap).digest('hex')
|
2022-02-04 06:40:58 +09:00
|
|
|
|
2022-02-05 23:41:35 +09:00
|
|
|
return Promise.all([
|
|
|
|
// add hash so it matches sprocket output
|
2022-02-12 06:05:38 +09:00
|
|
|
fsPromises.writeFile(outfileBabel, `${result.code}\n//# sourceMappingURL=${filename}-${resultMapHash}.map`),
|
2022-02-05 23:41:35 +09:00
|
|
|
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)
|
2022-02-04 06:27:00 +09:00
|
|
|
|
2022-02-05 23:41:35 +09:00
|
|
|
console.log(analyzeResult)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-02-12 06:05:38 +09:00
|
|
|
}
|
2022-02-05 23:41:35 +09:00
|
|
|
]
|
2022-02-02 00:45:52 +09:00
|
|
|
|
2022-02-04 06:40:58 +09:00
|
|
|
const args = process.argv.slice(2)
|
|
|
|
const options = {
|
|
|
|
watch: args.includes('--watch'),
|
|
|
|
analyze: args.includes('--analyze')
|
|
|
|
}
|
2022-02-12 06:05:38 +09:00
|
|
|
const watch = options.watch
|
|
|
|
? {
|
|
|
|
onRebuild (error) {
|
|
|
|
if (error == null) {
|
|
|
|
console.log('Rebuild succeeded')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
: false
|
2022-02-04 06:40:58 +09:00
|
|
|
|
2022-02-02 00:45:52 +09:00
|
|
|
esbuild.build({
|
|
|
|
bundle: true,
|
2022-02-12 06:05:38 +09:00
|
|
|
entryPoints: glob.sync('app/javascript/*.*'),
|
|
|
|
external: ['*.gif', '*.png'],
|
2022-02-04 06:40:58 +09:00
|
|
|
metafile: options.analyze,
|
2022-02-02 00:45:52 +09:00
|
|
|
nodePaths: ['app/javascript'],
|
2022-02-12 06:05:38 +09:00
|
|
|
outdir,
|
2022-02-05 23:41:35 +09:00
|
|
|
plugins,
|
2022-02-02 00:45:52 +09:00
|
|
|
resolveExtensions: ['.coffee', '.js'],
|
2022-02-12 06:05:38 +09:00
|
|
|
sourcemap: 'external',
|
|
|
|
watch
|
|
|
|
}).then(() => console.log('Build succeeded'))
|
|
|
|
.catch((e) => console.debug(e))
|