Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: static target DX improvements #7712

Merged
merged 22 commits into from Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion jest.config.js
Expand Up @@ -19,7 +19,8 @@ module.exports = {
// But its performance overhead is pretty bad (30+%).
// detectOpenHandles: true

setupFilesAfterEnv: ['./test/utils/setup'],
setupFilesAfterEnv: ['./test/utils/setup-env'],
setupFiles: ['./test/utils/setup'],

coverageDirectory: './coverage',

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/package.json
Expand Up @@ -20,10 +20,13 @@
"compression": "^1.7.4",
"connect": "^3.7.0",
"consola": "^2.14.0",
"crc": "^3.8.0",
"destr": "^1.0.0",
"esm": "^3.2.25",
"execa": "^3.4.0",
"exit": "^0.1.2",
"fs-extra": "^8.1.0",
"globby": "^11.0.1",
"hable": "^3.0.0",
"minimist": "^1.2.5",
"opener": "1.5.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/build.js
Expand Up @@ -85,7 +85,7 @@ export default {
const builder = await cmd.getBuilder(nuxt)
await builder.build()

const nextCommand = nuxt.options.target === TARGETS.static ? 'nuxt export' : 'nuxt start'
const nextCommand = nuxt.options.target === TARGETS.static ? 'nuxt generate' : 'nuxt start'
consola.info('Ready to run `' + (nextCommand) + '`')
}
}
Expand Down
50 changes: 5 additions & 45 deletions packages/cli/src/commands/export.js
@@ -1,50 +1,10 @@
import path from 'path'
import consola from 'consola'
import { TARGETS } from '@nuxt/utils'
import { common, locking } from '../options'
import { createLock } from '../utils'
import generate from './generate'

export default {
name: 'export',
description: 'Export a static generated web application',
usage: 'export <dir>',
options: {
...common,
...locking,
'fail-on-error': {
type: 'boolean',
default: false,
description: 'Exit with non-zero status code if there are errors when exporting pages'
}
},
async run (cmd) {
const config = await cmd.getNuxtConfig({
dev: false,
target: TARGETS.static,
_export: true
})
const nuxt = await cmd.getNuxt(config)

if (cmd.argv.lock) {
await cmd.setLock(await createLock({
id: 'export',
dir: nuxt.options.generate.dir,
root: config.rootDir
}))
}

const generator = await cmd.getGenerator(nuxt)
await nuxt.server.listen(0)

const { errors } = await generator.generate({
init: true,
build: false
})

await nuxt.close()
if (cmd.argv['fail-on-error'] && errors.length > 0) {
throw new Error('Error exporting pages, exiting with non-zero code')
}
consola.info('Ready to run `nuxt serve` or deploy `' + path.basename(nuxt.options.generate.dir) + '/` directory')
...generate,
run (...args) {
consola.warn('`nuxt export` has been deprecated! Please use `nuxt generate`.')
return generate.run.call(this, ...args)
}
}
20 changes: 10 additions & 10 deletions packages/cli/src/commands/generate.js
@@ -1,6 +1,7 @@
import { TARGETS } from '@nuxt/utils'
import { common, locking } from '../options'
import { normalizeArg, createLock } from '../utils'
import { ensureBuild, generate } from '../utils/generate'

export default {
name: 'generate',
Expand Down Expand Up @@ -54,23 +55,22 @@ export default {
}
},
async run (cmd) {
const config = await cmd.getNuxtConfig({
dev: false,
_build: cmd.argv.build,
_generate: true
})
const config = await cmd.getNuxtConfig({ dev: false })

// Disable analyze if set by the nuxt config
config.build = config.build || {}
config.build.analyze = false

// Full static
if (config.target === TARGETS.static) {
throw new Error("Please use `nuxt export` when using `target: 'static'`")
await ensureBuild(cmd)
await generate(cmd)
return
}

// Forcing static target anyway
config.target = TARGETS.static

// Disable analyze if set by the nuxt config
config.build = config.build || {}
config.build.analyze = false

// Set flag to keep the prerendering behaviour
config._legacyGenerate = true

Expand Down
86 changes: 6 additions & 80 deletions packages/cli/src/commands/serve.js
@@ -1,84 +1,10 @@
import { promises as fs } from 'fs'
import { join, extname, basename } from 'path'
import connect from 'connect'
import serveStatic from 'serve-static'
import compression from 'compression'
import { getNuxtConfig } from '@nuxt/config'
import { TARGETS } from '@nuxt/utils'
import { common, server } from '../options'
import { showBanner } from '../utils/banner'
import * as imports from '../imports'
import consola from 'consola'
import start from './start'

export default {
name: 'serve',
description: 'Serve the exported static application (should be compiled with `nuxt build` and `nuxt export` first)',
usage: 'serve <dir>',
options: {
'config-file': common['config-file'],
version: common.version,
help: common.help,
...server
},
async run (cmd) {
let options = await cmd.getNuxtConfig({ dev: false })
// add default options
options = getNuxtConfig(options)
try {
// overwrites with build config
const buildConfig = require(join(options.buildDir, 'nuxt/config.json'))
options.target = buildConfig.target
} catch (err) {}

if (options.target === TARGETS.server) {
throw new Error('You cannot use `nuxt serve` with ' + TARGETS.server + ' target, please use `nuxt start`')
}
const distStat = await fs.stat(options.generate.dir).catch(err => null) // eslint-disable-line handle-callback-err
if (!distStat || !distStat.isDirectory()) {
throw new Error('Output directory `' + basename(options.generate.dir) + '/` does not exists, please run `nuxt export` before `nuxt serve`.')
}
const app = connect()
app.use(compression({ threshold: 0 }))
app.use(
options.router.base,
serveStatic(options.generate.dir, {
extensions: ['html']
})
)
if (options.generate.fallback) {
const fallbackFile = await fs.readFile(join(options.generate.dir, options.generate.fallback), 'utf-8')
app.use((req, res, next) => {
const ext = extname(req.url) || '.html'

if (ext !== '.html') {
return next()
}
res.writeHeader(200, {
'Content-Type': 'text/html'
})
res.write(fallbackFile)
res.end()
})
}

const { port, host, socket, https } = options.server
const { Listener } = await imports.server()
const listener = new Listener({
port,
host,
socket,
https,
app,
dev: true, // try another port if taken
baseURL: options.router.base
})
await listener.listen()
const { Nuxt } = await imports.core()
showBanner({
constructor: Nuxt,
options,
server: {
listeners: [listener]
}
}, false)
...start,
run (...args) {
consola.warn('`nuxt serve` has been deprecated! Please use `nuxt start`.')
return start.run.call(this, ...args)
}
}
7 changes: 6 additions & 1 deletion packages/cli/src/commands/start.js
@@ -1,6 +1,8 @@
import { TARGETS } from '@nuxt/utils'
import consola from 'consola'
import { common, server } from '../options'
import { showBanner } from '../utils/banner'
import { serve } from '../utils/serve'

export default {
name: 'start',
Expand All @@ -12,9 +14,12 @@ export default {
},
async run (cmd) {
const config = await cmd.getNuxtConfig({ dev: false, _start: true })

if (config.target === TARGETS.static) {
throw new Error('You cannot use `nuxt start` with ' + TARGETS.static + ' target, please use `nuxt export` and `nuxt serve`')
consola.info('Serving static dist')
return serve(cmd)
}

const nuxt = await cmd.getNuxt(config)

// Listen and show ready banner
Expand Down