Nuxt/packages/nitro/src/build.ts

50 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-11-04 12:34:18 +00:00
import { resolve } from 'path'
import consola from 'consola'
import { rollup, OutputOptions } from 'rollup'
import Hookable from 'hookable'
import prettyBytes from 'pretty-bytes'
import gzipSize from 'gzip-size'
import chalk from 'chalk'
import { readFile, emptyDir } from 'fs-extra'
2020-11-04 12:34:18 +00:00
import { getRollupConfig } from './rollup/config'
import { hl, prettyPath, serializeTemplate, writeFile } from './utils'
import { SLSOptions } from './config'
2020-11-04 12:34:18 +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()
hooks.addHooks(options.hooks)
2020-11-04 12:34:18 +00:00
if (options.cleanTargetDir) {
await emptyDir(options.targetDir)
}
// Compile html template
2020-11-06 13:46:17 +00:00
const htmlSrc = resolve(options.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`)
const htmlTemplate = { src: htmlSrc, contents: '', dst: '', compiled: '' }
htmlTemplate.dst = htmlTemplate.src.replace(/.html$/, '.js').replace('app.', 'document.')
htmlTemplate.contents = await readFile(htmlTemplate.src, 'utf-8')
htmlTemplate.compiled = 'module.exports = ' + serializeTemplate(htmlTemplate.contents)
await hooks.callHook('template:document', htmlTemplate)
await writeFile(htmlTemplate.dst, htmlTemplate.compiled)
2020-11-04 12:34:18 +00:00
options.rollupConfig = getRollupConfig(options)
await hooks.callHook('rollup:before', options)
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
const { output } = await build.write(options.rollupConfig.output as OutputOptions)
2020-11-04 12:34:18 +00:00
const size = prettyBytes(output[0].code.length)
const zSize = prettyBytes(await gzipSize(output[0].code))
consola.success('Generated bundle in', prettyPath(options.targetDir), chalk.gray(`(Size: ${size} Gzip: ${zSize})`))
2020-11-04 12:34:18 +00:00
await hooks.callHook('done', options)
return {
entry: resolve(options.rollupConfig.output.dir, options.rollupConfig.output.entryFileNames)
}
2020-11-04 12:34:18 +00:00
}