mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-16 21:58:19 +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 { readFile, emptyDir, copy } from 'fs-extra'
|
||||||
import { printFSTree } from './utils/tree'
|
import { printFSTree } from './utils/tree'
|
||||||
import { getRollupConfig } from './rollup/config'
|
import { getRollupConfig } from './rollup/config'
|
||||||
import { hl, serializeTemplate, writeFile } from './utils'
|
import { hl, prettyPath, serializeTemplate, writeFile } from './utils'
|
||||||
import { SigmaContext } from './context'
|
import { SigmaContext } from './context'
|
||||||
|
|
||||||
export async function build (sigmaContext: SigmaContext) {
|
export async function prepare (sigmaContext: SigmaContext) {
|
||||||
consola.info(`Sigma preset is ${hl(sigmaContext.preset)}`)
|
consola.info(`Sigma preset is ${hl(sigmaContext.preset)}`)
|
||||||
|
|
||||||
// Cleanup output dir
|
await cleanupDir(sigmaContext.output.dir)
|
||||||
if (sigmaContext.output.clean) {
|
|
||||||
await emptyDir(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
|
// 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`)
|
||||||
const htmlTemplate = { src: htmlSrc, contents: '', dst: '', compiled: '' }
|
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 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)
|
|
||||||
|
|
||||||
sigmaContext.rollupConfig = getRollupConfig(sigmaContext)
|
sigmaContext.rollupConfig = getRollupConfig(sigmaContext)
|
||||||
|
|
||||||
await sigmaContext._internal.hooks.callHook('sigma:rollup:before', sigmaContext)
|
await sigmaContext._internal.hooks.callHook('sigma:rollup:before', sigmaContext)
|
||||||
|
|
||||||
return sigmaContext._nuxt.dev ? _watch(sigmaContext) : _build(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) {
|
async function _build (sigmaContext: SigmaContext) {
|
||||||
const spinner = ora()
|
const spinner = ora()
|
||||||
|
|
||||||
@ -58,10 +65,10 @@ async function _build (sigmaContext: SigmaContext) {
|
|||||||
throw error
|
throw error
|
||||||
})
|
})
|
||||||
|
|
||||||
spinner.start('Wrting Sigma bundle...')
|
spinner.start('Wrting server bundle...')
|
||||||
await build.write(sigmaContext.rollupConfig.output)
|
await build.write(sigmaContext.rollupConfig.output)
|
||||||
|
|
||||||
spinner.succeed('Sigma built')
|
spinner.succeed('Server built')
|
||||||
await printFSTree(sigmaContext.output.serverDir)
|
await printFSTree(sigmaContext.output.serverDir)
|
||||||
await sigmaContext._internal.hooks.callHook('sigma:compiled', sigmaContext)
|
await sigmaContext._internal.hooks.callHook('sigma:compiled', sigmaContext)
|
||||||
|
|
||||||
|
@ -29,8 +29,7 @@ export interface SigmaContext {
|
|||||||
output: {
|
output: {
|
||||||
dir: string
|
dir: string
|
||||||
serverDir: string
|
serverDir: string
|
||||||
publicDir: string | false
|
publicDir: string
|
||||||
clean: boolean
|
|
||||||
}
|
}
|
||||||
_nuxt: {
|
_nuxt: {
|
||||||
dev: boolean
|
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)
|
export type SigmaPreset = SigmaInput | ((input: SigmaInput) => SigmaInput)
|
||||||
|
|
||||||
@ -74,8 +75,7 @@ export function getsigmaContext (nuxtOptions: NuxtOptions, input: SigmaInput): S
|
|||||||
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,
|
||||||
@ -110,9 +110,7 @@ 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 = sigmaContext.output.publicDir
|
sigmaContext.output.publicDir = resolvePath(sigmaContext, 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)
|
sigmaContext._internal.hooks.addHooks(sigmaContext.hooks)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import fetch from 'node-fetch'
|
import fetch from 'node-fetch'
|
||||||
import { resolve } from 'upath'
|
import { resolve } from 'upath'
|
||||||
import { build } from '../build'
|
import { build, generate, prepare } from '../build'
|
||||||
import { getsigmaContext, SigmaContext } from '../context'
|
import { getsigmaContext, SigmaContext } from '../context'
|
||||||
import { createDevServer } from '../server'
|
import { createDevServer } from '../server'
|
||||||
import wpfs from '../utils/wpfs'
|
import wpfs from '../utils/wpfs'
|
||||||
@ -16,7 +16,7 @@ export default function (nuxt) {
|
|||||||
|
|
||||||
// Create contexts
|
// Create contexts
|
||||||
const sigmaContext = getsigmaContext(nuxt.options, nuxt.options.sigma || {})
|
const sigmaContext = getsigmaContext(nuxt.options, nuxt.options.sigma || {})
|
||||||
const sigmaDevContext = getsigmaContext(nuxt.options, { preset: 'dev' })
|
const sigmaDevContext = getsigmaContext(nuxt.options, { preset: 'local' })
|
||||||
|
|
||||||
// Connect hooks
|
// Connect hooks
|
||||||
nuxt.addHooks(sigmaContext.nuxtHooks)
|
nuxt.addHooks(sigmaContext.nuxtHooks)
|
||||||
@ -68,6 +68,8 @@ export default function (nuxt) {
|
|||||||
if (nuxt.options.dev) {
|
if (nuxt.options.dev) {
|
||||||
await build(sigmaDevContext)
|
await build(sigmaDevContext)
|
||||||
} else if (!sigmaContext._nuxt.isStatic) {
|
} else if (!sigmaContext._nuxt.isStatic) {
|
||||||
|
await prepare(sigmaContext)
|
||||||
|
await generate(sigmaContext)
|
||||||
await build(sigmaContext)
|
await build(sigmaContext)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -89,6 +91,9 @@ export default function (nuxt) {
|
|||||||
}
|
}
|
||||||
ignore.push(...sigmaContext.ignore)
|
ignore.push(...sigmaContext.ignore)
|
||||||
})
|
})
|
||||||
|
nuxt.hook('generate:before', async () => {
|
||||||
|
await prepare(sigmaContext)
|
||||||
|
})
|
||||||
nuxt.hook('generate:extendRoutes', async () => {
|
nuxt.hook('generate:extendRoutes', async () => {
|
||||||
await build(sigmaDevContext)
|
await build(sigmaDevContext)
|
||||||
await nuxt.server.reload()
|
await nuxt.server.reload()
|
||||||
|
@ -9,7 +9,7 @@ export const browser: SigmaPreset = extendPreset(worker, (input: SigmaInput) =>
|
|||||||
const script = `<script>
|
const script = `<script>
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
window.addEventListener('load', function () {
|
window.addEventListener('load', function () {
|
||||||
navigator.serviceWorker.register('${input._nuxt.routerBase}index.js');
|
navigator.serviceWorker.register('${input._nuxt.routerBase}_server/index.js');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
</script>`
|
</script>`
|
||||||
@ -17,10 +17,7 @@ if ('serviceWorker' in navigator) {
|
|||||||
return <SigmaInput> {
|
return <SigmaInput> {
|
||||||
entry: '{{ _internal.runtimeDir }}/entries/service-worker',
|
entry: '{{ _internal.runtimeDir }}/entries/service-worker',
|
||||||
output: {
|
output: {
|
||||||
dir: '{{ _nuxt.rootDir }}/.output/public',
|
serverDir: '{{ output.dir }}/public/_server'
|
||||||
publicDir: '{{ output.dir }}',
|
|
||||||
serverDir: '{{ output.dir }}',
|
|
||||||
clean: true
|
|
||||||
},
|
},
|
||||||
nuxtHooks: {
|
nuxtHooks: {
|
||||||
'vue-renderer:ssr:templateParams' (params) {
|
'vue-renderer:ssr:templateParams' (params) {
|
||||||
@ -35,7 +32,7 @@ if ('serviceWorker' in navigator) {
|
|||||||
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, 'sw.js'), 'self.importScripts(\'/server/index.js\');')
|
||||||
consola.info('Ready to deploy to static hosting:', prettyPath(output.publicDir as string))
|
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 './lambda'
|
||||||
export * from './netlify'
|
export * from './netlify'
|
||||||
export * from './node'
|
export * from './node'
|
||||||
export * from './dev'
|
export * from './local'
|
||||||
export * from './server'
|
export * from './server'
|
||||||
export * from './cli'
|
export * from './cli'
|
||||||
export * from './vercel'
|
export * from './vercel'
|
||||||
|
@ -2,13 +2,11 @@ import { extendPreset } from '../utils'
|
|||||||
import { SigmaPreset } from '../context'
|
import { SigmaPreset } from '../context'
|
||||||
import { node } from './node'
|
import { node } from './node'
|
||||||
|
|
||||||
export const dev: SigmaPreset = extendPreset(node, {
|
export const local: SigmaPreset = extendPreset(node, {
|
||||||
entry: '{{ _internal.runtimeDir }}/entries/dev',
|
entry: '{{ _internal.runtimeDir }}/entries/local',
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
output: {
|
output: {
|
||||||
dir: '{{ _nuxt.rootDir }}/node_modules/.cache/sigma',
|
dir: '{{ _nuxt.rootDir }}/node_modules/.cache/sigma'
|
||||||
publicDir: false,
|
|
||||||
clean: false
|
|
||||||
},
|
},
|
||||||
minify: false,
|
minify: false,
|
||||||
externals: true,
|
externals: true,
|
@ -7,8 +7,7 @@ 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/server/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'
|
||||||
|
Loading…
Reference in New Issue
Block a user