Nuxt/packages/nitro/src/build.ts

98 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-11-20 00:16:31 +00:00
import { resolve, join } from 'upath'
2020-11-04 12:34:18 +00:00
import consola from 'consola'
2020-11-20 00:16:31 +00:00
import { rollup, watch as rollupWatch } from 'rollup'
import ora from 'ora'
import { readFile, emptyDir, copy } 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-20 00:16:31 +00:00
import { SigmaContext } from './context'
2020-11-04 12:34:18 +00:00
2020-11-20 00:16:31 +00:00
export async function build (sigmaContext: SigmaContext) {
consola.info(`Sigma preset is ${hl(sigmaContext.preset)}`)
2020-11-04 12:34:18 +00:00
2020-11-20 00:16:31 +00:00
// Cleanup output dir
await emptyDir(sigmaContext.output.dir)
// Compile html template
2020-11-20 00:16:31 +00:00
const htmlSrc = resolve(sigmaContext._nuxt.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)
2020-11-20 00:16:31 +00:00
await sigmaContext._internal.hooks.callHook('sigma:template:document', htmlTemplate)
await writeFile(htmlTemplate.dst, htmlTemplate.compiled)
2020-11-04 12:34:18 +00:00
2020-11-20 00:16:31 +00:00
await generate(sigmaContext)
sigmaContext.rollupConfig = getRollupConfig(sigmaContext)
2020-11-14 20:41:38 +00:00
2020-11-20 00:16:31 +00:00
await sigmaContext._internal.hooks.callHook('sigma:rollup:before', sigmaContext)
return sigmaContext._nuxt.dev ? _watch(sigmaContext) : _build(sigmaContext)
}
2020-11-14 20:41:38 +00:00
2020-11-20 00:16:31 +00:00
export async function generate (sigmaContext: SigmaContext) {
await copy(
resolve(sigmaContext._nuxt.buildDir, 'dist/client'),
join(sigmaContext.output.publicDir, sigmaContext._nuxt.publicPath)
)
await copy(
resolve(sigmaContext._nuxt.rootDir, sigmaContext._nuxt.staticDir),
sigmaContext.output.publicDir
)
}
async function _build (sigmaContext: SigmaContext) {
const spinner = ora()
spinner.start('Building server...')
const build = await rollup(sigmaContext.rollupConfig).catch((error) => {
spinner.fail('Rollup error: ' + error.messsage)
2020-11-13 16:13:18 +00:00
throw error
})
2020-11-04 12:34:18 +00:00
2020-11-20 00:16:31 +00:00
spinner.start('Wrting Sigma bundle...')
await build.write(sigmaContext.rollupConfig.output)
2020-11-04 12:34:18 +00:00
2020-11-20 00:16:31 +00:00
spinner.succeed('Sigma built')
await printFSTree(sigmaContext.output.serverDir)
await sigmaContext._internal.hooks.callHook('sigma:compiled', sigmaContext)
return {
2020-11-20 00:16:31 +00:00
entry: resolve(sigmaContext.rollupConfig.output.dir, sigmaContext.rollupConfig.output.entryFileNames)
}
2020-11-04 12:34:18 +00:00
}
2020-11-20 00:16:31 +00:00
function _watch (sigmaContext: SigmaContext) {
const spinner = ora()
const watcher = rollupWatch(sigmaContext.rollupConfig)
let start
watcher.on('event', (event) => {
switch (event.code) {
// The watcher is (re)starting
case 'START':
return
// Building an individual bundle
case 'BUNDLE_START':
start = Date.now()
spinner.start('Building Sigma...')
return
// Finished building all bundles
case 'END':
sigmaContext._internal.hooks.callHook('sigma:compiled', sigmaContext)
return spinner.succeed(`Sigma built in ${Date.now() - start} ms`)
// Encountered an error while bundling
case 'ERROR':
spinner.fail('Rollup error: ' + event.error)
// consola.error(event.error)
}
})
}