fix issues with generate and dev

This commit is contained in:
Pooya Parsa 2020-11-20 02:38:06 +01:00
parent 16451a3588
commit ec291dafc1
7 changed files with 46 additions and 19 deletions

View File

@ -12,7 +12,9 @@ export async function build (sigmaContext: SigmaContext) {
consola.info(`Sigma preset is ${hl(sigmaContext.preset)}`) consola.info(`Sigma preset is ${hl(sigmaContext.preset)}`)
// Cleanup output dir // Cleanup output dir
await emptyDir(sigmaContext.output.dir) if (sigmaContext.output.clean) {
await emptyDir(sigmaContext.output.dir)
}
// Compile html template // Compile html template
const htmlSrc = resolve(sigmaContext._nuxt.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`) const htmlSrc = resolve(sigmaContext._nuxt.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`)
@ -23,6 +25,7 @@ export async function build (sigmaContext: SigmaContext) {
await sigmaContext._internal.hooks.callHook('sigma:template:document', htmlTemplate) await sigmaContext._internal.hooks.callHook('sigma:template:document', htmlTemplate)
await writeFile(htmlTemplate.dst, htmlTemplate.compiled) await writeFile(htmlTemplate.dst, htmlTemplate.compiled)
// TODO: only when not generate
await generate(sigmaContext) await generate(sigmaContext)
sigmaContext.rollupConfig = getRollupConfig(sigmaContext) sigmaContext.rollupConfig = getRollupConfig(sigmaContext)
@ -33,6 +36,9 @@ export async function build (sigmaContext: SigmaContext) {
} }
export async function generate (sigmaContext: SigmaContext) { export async function generate (sigmaContext: SigmaContext) {
if (!sigmaContext.output.publicDir) {
return
}
await copy( await copy(
resolve(sigmaContext._nuxt.buildDir, 'dist/client'), resolve(sigmaContext._nuxt.buildDir, 'dist/client'),
join(sigmaContext.output.publicDir, sigmaContext._nuxt.publicPath) join(sigmaContext.output.publicDir, sigmaContext._nuxt.publicPath)

View File

@ -24,17 +24,20 @@ export interface SigmaContext {
renderer: string renderer: string
middleware: ServerMiddleware[] middleware: ServerMiddleware[]
hooks: configHooksT hooks: configHooksT
nuxtHooks: configHooksT
ignore: string[] ignore: string[]
output: { output: {
dir: string dir: string
serverDir: string serverDir: string
publicDir: string publicDir: string | false
clean: boolean
} }
_nuxt: { _nuxt: {
dev: boolean dev: boolean
rootDir: string rootDir: string
srcDir: string srcDir: string
buildDir: string buildDir: string
generateDir: string
staticDir: string staticDir: string
routerBase: string routerBase: string
publicPath: string publicPath: string
@ -66,16 +69,19 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
middleware: [], middleware: [],
ignore: [], ignore: [],
hooks: {}, hooks: {},
nuxtHooks: {},
output: { output: {
dir: '{{ _nuxt.rootDir }}/.output', dir: '{{ _nuxt.rootDir }}/.output',
serverDir: '{{ output.dir }}/server', serverDir: '{{ output.dir }}/server',
publicDir: '{{ output.dir }}/public' publicDir: '{{ output.dir }}/public',
clean: true
}, },
_nuxt: { _nuxt: {
dev: nuxtOptions.dev, dev: nuxtOptions.dev,
rootDir: nuxtOptions.rootDir, rootDir: nuxtOptions.rootDir,
srcDir: nuxtOptions.srcDir, srcDir: nuxtOptions.srcDir,
buildDir: nuxtOptions.buildDir, buildDir: nuxtOptions.buildDir,
generateDir: nuxtOptions.generate.dir,
staticDir: nuxtOptions.dir.static, staticDir: nuxtOptions.dir.static,
routerBase: nuxtOptions.router.base, routerBase: nuxtOptions.router.base,
publicPath: nuxtOptions.build.publicPath, publicPath: nuxtOptions.build.publicPath,
@ -85,7 +91,7 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
}, },
_internal: { _internal: {
runtimeDir: resolve(__dirname, '../runtime'), runtimeDir: resolve(__dirname, '../runtime'),
hooks: undefined hooks: new Hookable()
} }
} }
@ -102,9 +108,13 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
const sigmaContext: SigmaContext = defu(input, _preset, defaults) as any const sigmaContext: SigmaContext = defu(input, _preset, defaults) as any
sigmaContext.output.dir = resolvePath(sigmaContext, sigmaContext.output.dir) sigmaContext.output.dir = resolvePath(sigmaContext, sigmaContext.output.dir)
sigmaContext.output.publicDir = resolvePath(sigmaContext, sigmaContext.output.publicDir) sigmaContext.output.publicDir = sigmaContext.output.publicDir
? resolvePath(sigmaContext, sigmaContext.output.publicDir)
: false
sigmaContext.output.serverDir = resolvePath(sigmaContext, sigmaContext.output.serverDir) sigmaContext.output.serverDir = resolvePath(sigmaContext, sigmaContext.output.serverDir)
sigmaContext._internal.hooks.addHooks(sigmaContext.hooks)
// console.log(sigmaContext) // console.log(sigmaContext)
// process.exit(1) // process.exit(1)

View File

@ -19,15 +19,13 @@ export default function (nuxt) {
const sigmaDevContext = getsigmaContext(nuxt.options, { preset: 'dev' }) const sigmaDevContext = getsigmaContext(nuxt.options, { preset: 'dev' })
// Use nuxt as main hooks host // Use nuxt as main hooks host
sigmaContext._internal.hooks = nuxt nuxt.addHooks(sigmaContext.nuxtHooks)
sigmaDevContext._internal.hooks = nuxt
nuxt.addHooks(sigmaContext.hooks)
// Replace nuxt server // Replace nuxt server
if (nuxt.server) { if (nuxt.server) {
nuxt.server.__closed = true nuxt.server.__closed = true
nuxt.server = createNuxt2DevServer(sigmaDevContext) nuxt.server = createNuxt2DevServer(sigmaDevContext)
nuxt.addHooks(sigmaDevContext.hooks) nuxt.addHooks(sigmaDevContext.nuxtHooks)
} }
// serverMiddleware bridge // serverMiddleware bridge
@ -73,10 +71,9 @@ export default function (nuxt) {
} }
// nuxt generate // nuxt generate
nuxt.options.generate.dir = sigmaContext.output.publicDir
nuxt.hook('generate:cache:ignore', (ignore: string[]) => { nuxt.hook('generate:cache:ignore', (ignore: string[]) => {
ignore.push(sigmaContext.output.dir) ignore.push(sigmaContext.output.dir)
ignore.push(sigmaContext.output.serverDir)
ignore.push(sigmaContext.output.publicDir)
ignore.push(...sigmaContext.ignore) ignore.push(...sigmaContext.ignore)
}) })
@ -85,6 +82,10 @@ export default function (nuxt) {
await build(sigmaDevContext) await build(sigmaDevContext)
await nuxt.server.reload() await nuxt.server.reload()
}) })
nuxt.hook('generate:done', async () => {
await nuxt.server.close()
})
} }
function createNuxt2DevServer (sigmaContext: SigmaContext) { function createNuxt2DevServer (sigmaContext: SigmaContext) {

View File

@ -19,21 +19,24 @@ if ('serviceWorker' in navigator) {
output: { output: {
dir: '{{ _nuxt.rootDir }}/.output/public', dir: '{{ _nuxt.rootDir }}/.output/public',
publicDir: '{{ output.dir }}', publicDir: '{{ output.dir }}',
serverDir: '{{ output.dir }}' serverDir: '{{ output.dir }}',
clean: true
}, },
hooks: { nuxtHooks: {
'vue-renderer:ssr:templateParams' (params) { 'vue-renderer:ssr:templateParams' (params) {
params.APP += script params.APP += script
}, },
'vue-renderer:spa:templateParams' (params) { 'vue-renderer:spa:templateParams' (params) {
params.APP += script params.APP += script
}, }
},
hooks: {
'sigma:template:document' (tmpl) { 'sigma:template:document' (tmpl) {
tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>') tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>')
}, },
async 'sigma:compiled' ({ output }: SigmaContext) { async 'sigma:compiled' ({ output }: SigmaContext) {
await writeFile(resolve(output.publicDir, 'index.html'), script) // TODO await writeFile(resolve(output.publicDir, 'index.html'), script) // TODO
consola.info('Ready to deploy to static hosting:', prettyPath(output.publicDir)) consola.info('Ready to deploy to static hosting:', prettyPath(output.publicDir as string))
} }
} }
} }

View File

@ -4,6 +4,12 @@ import { node } from './node'
export const dev: SigmaPreset = extendPreset(node, { export const dev: SigmaPreset = extendPreset(node, {
entry: '{{ _internal.runtimeDir }}/entries/dev', entry: '{{ _internal.runtimeDir }}/entries/dev',
// @ts-ignore
output: {
dir: '{{ _nuxt.rootDir }}/node_modules/.cache/sigma',
publicDir: false,
clean: false
},
minify: false, minify: false,
externals: true, externals: true,
inlineChunks: true, inlineChunks: true,

View File

@ -6,8 +6,9 @@ import { node } from './node'
export const vercel: SigmaPreset = extendPreset(node, { export const vercel: SigmaPreset = extendPreset(node, {
output: { output: {
dir: '{{ _nuxt.rootDir }}/.vercel_build_output', dir: '{{ _nuxt.rootDir }}/.vercel_build_output',
serverDir: '{{ output.dir }}/functions/node/_nuxt/index.js', serverDir: '{{ output.dir }}/functions/node/server/index.js',
publicDir: '{{ output.dir }}/static' publicDir: '{{ output.dir }}/static',
clean: true
}, },
ignore: [ ignore: [
'vercel.json' 'vercel.json'
@ -40,7 +41,7 @@ async function writeRoutes ({ output }: SigmaContext) {
}, },
{ {
src: '(.*)', src: '(.*)',
dest: '/.vercel/functions/_nuxt/index' dest: '/.vercel/functions/server/index'
} }
] ]

View File

@ -121,7 +121,7 @@ export function createDevServer (sigmaContext: SigmaContext) {
await pendingWorker.terminate() await pendingWorker.terminate()
} }
await Promise.all(listeners.map(l => new Promise((resolve, reject) => { await Promise.all(listeners.map(l => new Promise((resolve, reject) => {
l.close(err => err ? reject(err) : resolve()) l.close(err => err ? reject(err) : resolve(undefined))
}))) })))
} }
sigmaContext._internal.hooks.hook('close', close) sigmaContext._internal.hooks.hook('close', close)