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-02 00:49:55 +09:00
|
|
|
import esbuild from 'esbuild'
|
2022-02-04 06:27:00 +09:00
|
|
|
import coffeeScriptPlugin from 'esbuild-coffeescript'
|
|
|
|
import fsPromises from 'fs/promises'
|
2022-02-04 04:46:03 +09:00
|
|
|
|
2022-02-04 08:57:06 +09:00
|
|
|
const outfileName = 'application.js'
|
|
|
|
const outfileEsbuild = `tmp/${outfileName}`
|
|
|
|
const outfileBabel = `app/assets/builds/${outfileName}`
|
2022-02-04 04:46:03 +09:00
|
|
|
|
2022-02-04 06:40:58 +09:00
|
|
|
const analyzeOnEnd = {
|
|
|
|
name: 'analyzeOnEnd',
|
|
|
|
setup (build) {
|
|
|
|
build.onEnd(async (result) => {
|
|
|
|
if (options.analyze) {
|
|
|
|
const analyzeResult = await esbuild.analyzeMetafile(result.metafile)
|
|
|
|
|
|
|
|
console.log(analyzeResult)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 04:46:03 +09:00
|
|
|
const babelOnEnd = {
|
|
|
|
name: 'babelOnEnd',
|
|
|
|
setup (build) {
|
2022-02-04 06:27:00 +09:00
|
|
|
build.onEnd(async () => {
|
|
|
|
const options = {
|
2022-02-04 04:46:03 +09:00
|
|
|
presets: [
|
|
|
|
['@babel/preset-env']
|
2022-02-04 05:00:21 +09:00
|
|
|
],
|
|
|
|
sourceMaps: true
|
2022-02-04 06:27:00 +09:00
|
|
|
}
|
2022-02-04 08:57:06 +09:00
|
|
|
const outEsbuild = await fsPromises.readFile(outfileEsbuild)
|
2022-02-04 06:27:00 +09:00
|
|
|
const result = await babel.transformAsync(outEsbuild, options)
|
|
|
|
|
|
|
|
return Promise.all([
|
2022-02-04 08:57:06 +09:00
|
|
|
fsPromises.writeFile(outfileBabel, `${result.code}\n//# sourceMappingURL=${outfileName}.map`),
|
|
|
|
fsPromises.writeFile(`${outfileBabel}.map`, JSON.stringify(result.map))
|
2022-02-04 06:27:00 +09:00
|
|
|
])
|
2022-02-04 04:46:03 +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-02 00:45:52 +09:00
|
|
|
esbuild.build({
|
|
|
|
bundle: true,
|
|
|
|
entryPoints: ['app/javascript/application.coffee'],
|
2022-02-04 06:40:58 +09:00
|
|
|
metafile: options.analyze,
|
2022-02-02 00:45:52 +09:00
|
|
|
nodePaths: ['app/javascript'],
|
2022-02-04 08:57:06 +09:00
|
|
|
outfile: outfileEsbuild,
|
2022-02-04 06:40:58 +09:00
|
|
|
plugins: [coffeeScriptPlugin({ bare: true }), babelOnEnd, analyzeOnEnd],
|
2022-02-02 00:45:52 +09:00
|
|
|
resolveExtensions: ['.coffee', '.js'],
|
2022-02-04 08:57:06 +09:00
|
|
|
sourcemap: 'inline',
|
2022-02-04 06:40:58 +09:00
|
|
|
watch: options.watch
|
2022-02-02 00:49:55 +09:00
|
|
|
})
|