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)}`)
// Cleanup output dir
if (sigmaContext.output.clean) {
await emptyDir(sigmaContext.output.dir)
}
// Compile html template
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 writeFile(htmlTemplate.dst, htmlTemplate.compiled)
// TODO: only when not generate
await generate(sigmaContext)
sigmaContext.rollupConfig = getRollupConfig(sigmaContext)
@ -33,6 +36,9 @@ export async function build (sigmaContext: SigmaContext) {
}
export async function generate (sigmaContext: SigmaContext) {
if (!sigmaContext.output.publicDir) {
return
}
await copy(
resolve(sigmaContext._nuxt.buildDir, 'dist/client'),
join(sigmaContext.output.publicDir, sigmaContext._nuxt.publicPath)

View File

@ -24,17 +24,20 @@ export interface SigmaContext {
renderer: string
middleware: ServerMiddleware[]
hooks: configHooksT
nuxtHooks: configHooksT
ignore: string[]
output: {
dir: string
serverDir: string
publicDir: string
publicDir: string | false
clean: boolean
}
_nuxt: {
dev: boolean
rootDir: string
srcDir: string
buildDir: string
generateDir: string
staticDir: string
routerBase: string
publicPath: string
@ -66,16 +69,19 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
middleware: [],
ignore: [],
hooks: {},
nuxtHooks: {},
output: {
dir: '{{ _nuxt.rootDir }}/.output',
serverDir: '{{ output.dir }}/server',
publicDir: '{{ output.dir }}/public'
publicDir: '{{ output.dir }}/public',
clean: true
},
_nuxt: {
dev: nuxtOptions.dev,
rootDir: nuxtOptions.rootDir,
srcDir: nuxtOptions.srcDir,
buildDir: nuxtOptions.buildDir,
generateDir: nuxtOptions.generate.dir,
staticDir: nuxtOptions.dir.static,
routerBase: nuxtOptions.router.base,
publicPath: nuxtOptions.build.publicPath,
@ -85,7 +91,7 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
},
_internal: {
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
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._internal.hooks.addHooks(sigmaContext.hooks)
// console.log(sigmaContext)
// process.exit(1)

View File

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

View File

@ -19,21 +19,24 @@ if ('serviceWorker' in navigator) {
output: {
dir: '{{ _nuxt.rootDir }}/.output/public',
publicDir: '{{ output.dir }}',
serverDir: '{{ output.dir }}'
serverDir: '{{ output.dir }}',
clean: true
},
hooks: {
nuxtHooks: {
'vue-renderer:ssr:templateParams' (params) {
params.APP += script
},
'vue-renderer:spa:templateParams' (params) {
params.APP += script
}
},
hooks: {
'sigma:template:document' (tmpl) {
tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>')
},
async 'sigma:compiled' ({ output }: SigmaContext) {
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, {
entry: '{{ _internal.runtimeDir }}/entries/dev',
// @ts-ignore
output: {
dir: '{{ _nuxt.rootDir }}/node_modules/.cache/sigma',
publicDir: false,
clean: false
},
minify: false,
externals: true,
inlineChunks: true,

View File

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