2020-11-04 12:34:18 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
import consola from 'consola'
|
2020-11-14 20:41:38 +00:00
|
|
|
import { rollup } from 'rollup'
|
2020-11-04 12:34:18 +00:00
|
|
|
import Hookable from 'hookable'
|
2020-11-14 13:05:09 +00:00
|
|
|
import { readFile, emptyDir } from 'fs-extra'
|
2020-11-14 20:41:38 +00:00
|
|
|
import { printFSTree } from './utils/tree'
|
2020-11-04 12:34:18 +00:00
|
|
|
import { getRollupConfig } from './rollup/config'
|
2020-11-14 20:41:38 +00:00
|
|
|
import { hl, serializeTemplate, writeFile } from './utils'
|
2020-11-05 13:38:15 +00:00
|
|
|
import { SLSOptions } from './config'
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2020-11-05 13:38:15 +00:00
|
|
|
export async function build (options: SLSOptions) {
|
|
|
|
consola.info(`Generating bundle for ${hl(options.target)}`)
|
2020-11-04 12:34:18 +00:00
|
|
|
|
|
|
|
const hooks = new Hookable()
|
2020-11-05 13:38:15 +00:00
|
|
|
hooks.addHooks(options.hooks)
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2020-11-14 13:05:09 +00:00
|
|
|
if (options.cleanTargetDir) {
|
|
|
|
await emptyDir(options.targetDir)
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:56:40 +00:00
|
|
|
// Compile html template
|
2020-11-06 13:46:17 +00:00
|
|
|
const htmlSrc = resolve(options.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`)
|
2020-11-06 09:51:35 +00:00
|
|
|
const htmlTemplate = { src: htmlSrc, contents: '', dst: '', compiled: '' }
|
2020-11-05 21:56:40 +00:00
|
|
|
htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.js').replace('app.', 'document.')
|
2020-11-06 09:51:35 +00:00
|
|
|
htmlTemplate.contents = await readFile(htmlTemplate.src, 'utf-8')
|
2020-11-14 13:05:09 +00:00
|
|
|
htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents)
|
2020-11-05 21:56:40 +00:00
|
|
|
await hooks.callHook('template:document', htmlTemplate)
|
2020-11-10 18:19:24 +00:00
|
|
|
await writeFile(htmlTemplate.dst, htmlTemplate.compiled)
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2020-11-05 13:38:15 +00:00
|
|
|
options.rollupConfig = getRollupConfig(options)
|
2020-11-14 20:41:38 +00:00
|
|
|
|
2020-11-05 13:38:15 +00:00
|
|
|
await hooks.callHook('rollup:before', options)
|
2020-11-14 20:41:38 +00:00
|
|
|
|
2020-11-13 16:13:18 +00:00
|
|
|
const build = await rollup(options.rollupConfig).catch((error) => {
|
|
|
|
error.message = '[serverless] Rollup Error: ' + error.message
|
|
|
|
throw error
|
|
|
|
})
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2020-11-14 20:41:38 +00:00
|
|
|
await build.write(options.rollupConfig.output)
|
|
|
|
|
|
|
|
await printFSTree(options.targetDir)
|
2020-11-04 12:34:18 +00:00
|
|
|
|
2020-11-05 13:38:15 +00:00
|
|
|
await hooks.callHook('done', options)
|
2020-11-14 00:03:26 +00:00
|
|
|
|
|
|
|
return {
|
2020-11-14 13:05:09 +00:00
|
|
|
entry: resolve(options.rollupConfig.output.dir, options.rollupConfig.output.entryFileNames)
|
2020-11-14 00:03:26 +00:00
|
|
|
}
|
2020-11-04 12:34:18 +00:00
|
|
|
}
|