Skip to content

Commit

Permalink
feat: Always enable SSR in dev (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
saltysugar authored and egoist committed Aug 30, 2019
1 parent 75203df commit 442c990
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
7 changes: 2 additions & 5 deletions packages/saber/lib/cli-commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ module.exports = function(cli) {
.option('--lazy', 'Enable lazy page compilation')
.option('--port <port>', 'Server port', { default: 3000 })
.option('--host <host>', 'Server host', { default: '0.0.0.0' })
.option('--ssr', 'Enable server-side rendering')
.option('--inspect-webpack', 'Inspect webpack config in your editor')
.action((cwd = '.', options) => {
setNodeEnv('development')

const { host, port, lazy, ssr } = options
const { host, port, lazy } = options
delete options.host
delete options.port
delete options.lazy
delete options.ssr
return require('..')(Object.assign({ cwd, dev: true }, options), {
server: {
host,
port,
ssr
port
},
build: {
lazy
Expand Down
3 changes: 1 addition & 2 deletions packages/saber/lib/utils/validateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ module.exports = (config, { dev }) => {
const server = struct(
{
host: 'string?',
port: 'number?',
ssr: 'boolean?'
port: 'number?'
},
{
host: '0.0.0.0',
Expand Down
60 changes: 29 additions & 31 deletions packages/saber/vue-renderer/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,37 +393,35 @@ class VueRenderer {
event.emit('done', stats.hasErrors())
})

if (this.api.config.server.ssr) {
const serverConfig = this.api.getWebpackConfig({ type: 'server' })
const serverCompiler = webpack(serverConfig)
const mfs = new webpack.MemoryOutputFileSystem()
serverCompiler.outputFileSystem = mfs

let serverBundle
let clientManifest

serverCompiler.hooks.done.tap('init-renderer', stats => {
if (!stats.hasErrors()) {
serverBundle = readJSON(
this.api.resolveCache('bundle-manifest/server.json'),
mfs.readFileSync.bind(mfs)
)
this.initRenderer({ serverBundle, clientManifest })
}
})
clientCompiler.hooks.done.tap('init-renderer', stats => {
if (!stats.hasErrors()) {
clientManifest = readJSON(
this.api.resolveCache('bundle-manifest/client.json'),
clientCompiler.outputFileSystem.readFileSync.bind(
clientCompiler.outputFileSystem
)
const serverConfig = this.api.getWebpackConfig({ type: 'server' })
const serverCompiler = webpack(serverConfig)
const mfs = new webpack.MemoryOutputFileSystem()
serverCompiler.outputFileSystem = mfs

let serverBundle
let clientManifest

serverCompiler.hooks.done.tap('init-renderer', stats => {
if (!stats.hasErrors()) {
serverBundle = readJSON(
this.api.resolveCache('bundle-manifest/server.json'),
mfs.readFileSync.bind(mfs)
)
this.initRenderer({ serverBundle, clientManifest })
}
})
clientCompiler.hooks.done.tap('init-renderer', stats => {
if (!stats.hasErrors()) {
clientManifest = readJSON(
this.api.resolveCache('bundle-manifest/client.json'),
clientCompiler.outputFileSystem.readFileSync.bind(
clientCompiler.outputFileSystem
)
this.initRenderer({ serverBundle, clientManifest })
}
})
serverCompiler.watch({}, () => {})
}
)
this.initRenderer({ serverBundle, clientManifest })
}
})
serverCompiler.watch({}, () => {})

server.get('/_saber/visit-page', async (req, res) => {
let [, pathname, hash] = /^([^#]+)(#.+)?$/.exec(req.query.route) || []
Expand Down Expand Up @@ -469,7 +467,7 @@ class VueRenderer {
return res.end('404')
}

if (this.api.config.server.ssr && !this.renderer) {
if (!this.renderer) {
return res.end(`Please wait for compilation and refresh..`)
}

Expand Down

0 comments on commit 442c990

Please sign in to comment.