mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
fix: support both targets by adding prepare step
This commit is contained in:
parent
bde0c7c286
commit
b15d16abd3
@ -5,17 +5,43 @@ import ora from 'ora'
|
||||
import { readFile, emptyDir, copy } from 'fs-extra'
|
||||
import { printFSTree } from './utils/tree'
|
||||
import { getRollupConfig } from './rollup/config'
|
||||
import { hl, serializeTemplate, writeFile } from './utils'
|
||||
import { hl, prettyPath, serializeTemplate, writeFile } from './utils'
|
||||
import { SigmaContext } from './context'
|
||||
|
||||
export async function build (sigmaContext: SigmaContext) {
|
||||
export async function prepare (sigmaContext: SigmaContext) {
|
||||
consola.info(`Sigma preset is ${hl(sigmaContext.preset)}`)
|
||||
|
||||
// Cleanup output dir
|
||||
if (sigmaContext.output.clean) {
|
||||
await emptyDir(sigmaContext.output.dir)
|
||||
await cleanupDir(sigmaContext.output.dir)
|
||||
|
||||
if (!sigmaContext.output.publicDir.startsWith(sigmaContext.output.dir)) {
|
||||
await cleanupDir(sigmaContext.output.publicDir)
|
||||
}
|
||||
|
||||
if (!sigmaContext.output.serverDir.startsWith(sigmaContext.output.dir)) {
|
||||
await cleanupDir(sigmaContext.output.serverDir)
|
||||
}
|
||||
}
|
||||
|
||||
async function cleanupDir (dir: string) {
|
||||
consola.info('Cleaning up', prettyPath(dir))
|
||||
await emptyDir(dir)
|
||||
}
|
||||
|
||||
export async function generate (sigmaContext: SigmaContext) {
|
||||
const spinner = ora()
|
||||
spinner.start('Generating public...')
|
||||
await copy(
|
||||
resolve(sigmaContext._nuxt.buildDir, 'dist/client'),
|
||||
join(sigmaContext.output.publicDir, sigmaContext._nuxt.publicPath)
|
||||
)
|
||||
await copy(
|
||||
resolve(sigmaContext._nuxt.srcDir, sigmaContext._nuxt.staticDir),
|
||||
sigmaContext.output.publicDir
|
||||
)
|
||||
spinner.succeed('Generated public ' + prettyPath(sigmaContext.output.publicDir))
|
||||
}
|
||||
|
||||
export async function build (sigmaContext: SigmaContext) {
|
||||
// Compile html template
|
||||
const htmlSrc = resolve(sigmaContext._nuxt.buildDir, `views/${{ 2: 'app', 3: 'document' }[2]}.template.html`)
|
||||
const htmlTemplate = { src: htmlSrc, contents: '', dst: '', compiled: '' }
|
||||
@ -25,30 +51,11 @@ 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)
|
||||
|
||||
await sigmaContext._internal.hooks.callHook('sigma:rollup:before', sigmaContext)
|
||||
|
||||
return sigmaContext._nuxt.dev ? _watch(sigmaContext) : _build(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)
|
||||
)
|
||||
await copy(
|
||||
resolve(sigmaContext._nuxt.srcDir, sigmaContext._nuxt.staticDir),
|
||||
sigmaContext.output.publicDir
|
||||
)
|
||||
}
|
||||
|
||||
async function _build (sigmaContext: SigmaContext) {
|
||||
const spinner = ora()
|
||||
|
||||
@ -58,10 +65,10 @@ async function _build (sigmaContext: SigmaContext) {
|
||||
throw error
|
||||
})
|
||||
|
||||
spinner.start('Wrting Sigma bundle...')
|
||||
spinner.start('Wrting server bundle...')
|
||||
await build.write(sigmaContext.rollupConfig.output)
|
||||
|
||||
spinner.succeed('Sigma built')
|
||||
spinner.succeed('Server built')
|
||||
await printFSTree(sigmaContext.output.serverDir)
|
||||
await sigmaContext._internal.hooks.callHook('sigma:compiled', sigmaContext)
|
||||
|
||||
|
@ -29,8 +29,7 @@ export interface SigmaContext {
|
||||
output: {
|
||||
dir: string
|
||||
serverDir: string
|
||||
publicDir: string | false
|
||||
clean: boolean
|
||||
publicDir: string
|
||||
}
|
||||
_nuxt: {
|
||||
dev: boolean
|
||||
@ -51,7 +50,9 @@ export interface SigmaContext {
|
||||
}
|
||||
}
|
||||
|
||||
export interface SigmaInput extends Partial<SigmaContext> {}
|
||||
type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]> }
|
||||
|
||||
export interface SigmaInput extends DeepPartial<SigmaContext> {}
|
||||
|
||||
export type SigmaPreset = SigmaInput | ((input: SigmaInput) => SigmaInput)
|
||||
|
||||
@ -74,8 +75,7 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
|
||||
output: {
|
||||
dir: '{{ _nuxt.rootDir }}/.output',
|
||||
serverDir: '{{ output.dir }}/server',
|
||||
publicDir: '{{ output.dir }}/public',
|
||||
clean: true
|
||||
publicDir: '{{ output.dir }}/public'
|
||||
},
|
||||
_nuxt: {
|
||||
dev: nuxtOptions.dev,
|
||||
@ -110,9 +110,7 @@ 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 = sigmaContext.output.publicDir
|
||||
? resolvePath(sigmaContext, sigmaContext.output.publicDir)
|
||||
: false
|
||||
sigmaContext.output.publicDir = resolvePath(sigmaContext, sigmaContext.output.publicDir)
|
||||
sigmaContext.output.serverDir = resolvePath(sigmaContext, sigmaContext.output.serverDir)
|
||||
|
||||
sigmaContext._internal.hooks.addHooks(sigmaContext.hooks)
|
||||
|
@ -1,6 +1,6 @@
|
||||
import fetch from 'node-fetch'
|
||||
import { resolve } from 'upath'
|
||||
import { build } from '../build'
|
||||
import { build, generate, prepare } from '../build'
|
||||
import { getsigmaContext, SigmaContext } from '../context'
|
||||
import { createDevServer } from '../server'
|
||||
import wpfs from '../utils/wpfs'
|
||||
@ -16,7 +16,7 @@ export default function (nuxt) {
|
||||
|
||||
// Create contexts
|
||||
const sigmaContext = getsigmaContext(nuxt.options, nuxt.options.sigma || {})
|
||||
const sigmaDevContext = getsigmaContext(nuxt.options, { preset: 'dev' })
|
||||
const sigmaDevContext = getsigmaContext(nuxt.options, { preset: 'local' })
|
||||
|
||||
// Connect hooks
|
||||
nuxt.addHooks(sigmaContext.nuxtHooks)
|
||||
@ -68,6 +68,8 @@ export default function (nuxt) {
|
||||
if (nuxt.options.dev) {
|
||||
await build(sigmaDevContext)
|
||||
} else if (!sigmaContext._nuxt.isStatic) {
|
||||
await prepare(sigmaContext)
|
||||
await generate(sigmaContext)
|
||||
await build(sigmaContext)
|
||||
}
|
||||
})
|
||||
@ -89,6 +91,9 @@ export default function (nuxt) {
|
||||
}
|
||||
ignore.push(...sigmaContext.ignore)
|
||||
})
|
||||
nuxt.hook('generate:before', async () => {
|
||||
await prepare(sigmaContext)
|
||||
})
|
||||
nuxt.hook('generate:extendRoutes', async () => {
|
||||
await build(sigmaDevContext)
|
||||
await nuxt.server.reload()
|
||||
|
@ -9,7 +9,7 @@ export const browser: SigmaPreset = extendPreset(worker, (input: SigmaInput) =>
|
||||
const script = `<script>
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', function () {
|
||||
navigator.serviceWorker.register('${input._nuxt.routerBase}index.js');
|
||||
navigator.serviceWorker.register('${input._nuxt.routerBase}_server/index.js');
|
||||
});
|
||||
}
|
||||
</script>`
|
||||
@ -17,10 +17,7 @@ if ('serviceWorker' in navigator) {
|
||||
return <SigmaInput> {
|
||||
entry: '{{ _internal.runtimeDir }}/entries/service-worker',
|
||||
output: {
|
||||
dir: '{{ _nuxt.rootDir }}/.output/public',
|
||||
publicDir: '{{ output.dir }}',
|
||||
serverDir: '{{ output.dir }}',
|
||||
clean: true
|
||||
serverDir: '{{ output.dir }}/public/_server'
|
||||
},
|
||||
nuxtHooks: {
|
||||
'vue-renderer:ssr:templateParams' (params) {
|
||||
@ -35,7 +32,7 @@ if ('serviceWorker' in navigator) {
|
||||
tmpl.compiled = tmpl.compiled.replace('</body>', script + '</body>')
|
||||
},
|
||||
async 'sigma:compiled' ({ output }: SigmaContext) {
|
||||
await writeFile(resolve(output.publicDir, 'index.html'), script) // TODO
|
||||
await writeFile(resolve(output.publicDir, 'sw.js'), 'self.importScripts(\'/server/index.js\');')
|
||||
consola.info('Ready to deploy to static hosting:', prettyPath(output.publicDir as string))
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ export * from './cloudflare'
|
||||
export * from './lambda'
|
||||
export * from './netlify'
|
||||
export * from './node'
|
||||
export * from './dev'
|
||||
export * from './local'
|
||||
export * from './server'
|
||||
export * from './cli'
|
||||
export * from './vercel'
|
||||
|
@ -2,13 +2,11 @@ import { extendPreset } from '../utils'
|
||||
import { SigmaPreset } from '../context'
|
||||
import { node } from './node'
|
||||
|
||||
export const dev: SigmaPreset = extendPreset(node, {
|
||||
entry: '{{ _internal.runtimeDir }}/entries/dev',
|
||||
export const local: SigmaPreset = extendPreset(node, {
|
||||
entry: '{{ _internal.runtimeDir }}/entries/local',
|
||||
// @ts-ignore
|
||||
output: {
|
||||
dir: '{{ _nuxt.rootDir }}/node_modules/.cache/sigma',
|
||||
publicDir: false,
|
||||
clean: false
|
||||
dir: '{{ _nuxt.rootDir }}/node_modules/.cache/sigma'
|
||||
},
|
||||
minify: false,
|
||||
externals: true,
|
@ -7,8 +7,7 @@ export const vercel: SigmaPreset = extendPreset(node, {
|
||||
output: {
|
||||
dir: '{{ _nuxt.rootDir }}/.vercel_build_output',
|
||||
serverDir: '{{ output.dir }}/functions/node/server/index.js',
|
||||
publicDir: '{{ output.dir }}/static',
|
||||
clean: true
|
||||
publicDir: '{{ output.dir }}/static'
|
||||
},
|
||||
ignore: [
|
||||
'vercel.json'
|
||||
|
Loading…
Reference in New Issue
Block a user