Skip to content

Commit

Permalink
Move build prod server in a separate command
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyzu committed Oct 29, 2018
1 parent c36c00a commit 9d6cea9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 22 deletions.
87 changes: 66 additions & 21 deletions activator/RenderServerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ class RenderServerPlugin extends PluginInstance {
this.cache = cache
}

runBuild() {
const config = this.webpack.getCommonConfig(serverWebpackConfig)
const { compiler, start } = this.createRenderServer(config)

start()

compiler.run(logBuildResult)
}

createRenderServer(webpackConfig) {
const universalSettings = this.universalSettings

Expand All @@ -88,25 +97,32 @@ class RenderServerPlugin extends PluginInstance {
},
})

let render
let asyncRender = startCompileRenderServer(serverConfig, universalSettings)

if (process.env.NODE_ENV === 'production') {
compiler.run(logBuildResult)
} else {
compiler.watch({
aggregateTimeout: 300,
}, (err, stats) => {
logBuildResult(err, stats)

if (!err && !stats.compilation.errors.length) {
delete require.cache[universalSettings.server.output]

render = null
asyncRender = startCompileRenderServer(serverConfig, universalSettings)
}
})
return {
compiler,
start: () => startCompileRenderServer(serverConfig, universalSettings),
universalSettings,
}
}

createDevRenderServer() {
const config = this.webpack.getCommonConfig(serverWebpackConfig)
const { compiler, start, universalSettings } = this.createRenderServer(config)

let render
let asyncRender = start()

compiler.watch({
aggregateTimeout: 300,
}, (err, stats) => {
logBuildResult(err, stats)

if (!err && !stats.compilation.errors.length) {
delete require.cache[universalSettings.server.output]

render = null
asyncRender = start()
}
})

return (side, req, res) => {
if (render) {
Expand All @@ -120,10 +136,39 @@ class RenderServerPlugin extends PluginInstance {
}
}

render(app) {
const config = this.webpack.getCommonConfig(serverWebpackConfig)
createProdRenderServer() {
try {
const configuration = Object.assign(this.webpack.getCommonConfig(serverWebpackConfig), {
cacheConfig: {
components: this.cache,
},
})

// eslint-disable-next-line import/no-dynamic-require,global-require
const { default: starter } = require(this.universalSettings.server.output)

const chunksPath = path.resolve(configuration.output.path, 'webpack-chunks.json')
// eslint-disable-next-line import/no-dynamic-require,global-require
const chunks = require(chunksPath)

const doRender = this.createRenderServer(config)
const doRender = starter({
configuration,
chunks: () => chunks,
})

return (side, req, res) => doRender[side](req, res, this.config)
} catch (error) {
logError(error)
logError('Failed to run prod server.')

throw error
}
}

render(app) {
const doRender = process.env.NODE_ENV === 'production'
? this.createProdRenderServer()
: this.createDevRenderServer()

app.use(cookiesMiddleware())

Expand Down
10 changes: 10 additions & 0 deletions activator/RenderServerPluginApi.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const { PluginApi } = require('@rispa/core')

class RenderServerPluginApi extends PluginApi {
static startHandler(context) {
const instance = context.get(RenderServerPluginApi.pluginName)

return instance.runBuild()
}

setCache(cache) {
this.instance.setCache(cache)
}

runBuild() {
return this.instance.runBuild()
}
}

RenderServerPluginApi.pluginName = '@rispa/render-server'
Expand Down
4 changes: 4 additions & 0 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { init } = require('@rispa/core')
const { startHandler } = require('../activator/RenderServerPluginApi')

init(startHandler)
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,26 @@
"universal-webpack": "^0.3.9",
"webpack": "^2.4.1",
"webpack-flush-chunks": "^0.1.1",
"write-file-webpack-plugin": "^4.0.2"
"write-file-webpack-plugin": "^4.0.2",
"better-npm-run": "0.1.0"
},
"devDependencies": {
"@rispa/eslint-config": ">=3.0.0",
"babel-cli": "^6.26.0"
},
"scripts": {
"build": "better-npm-run build",
"compile": "babel src --out-dir lib --ignore spec.js,test.js",
"lint": "rispa-eslint .",
"lint:fix": "rispa-eslint . --fix"
},
"betterScripts": {
"build": {
"command": "node ./bin/build.js",
"env": {
"NODE_ENV": "production",
"DEBUG": "rispa:*"
}
}
}
}

0 comments on commit 9d6cea9

Please sign in to comment.