2018-03-16 19:52:17 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
import webpack from 'webpack'
|
|
|
|
import nodeExternals from 'webpack-node-externals'
|
2018-03-16 19:11:24 +00:00
|
|
|
import _ from 'lodash'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-16 19:11:24 +00:00
|
|
|
import base from './base.config'
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2018-03-16 19:52:17 +00:00
|
|
|
// import VueSSRServerPlugin from 'vue-server-renderer/server-plugin'
|
|
|
|
import VueSSRServerPlugin from './plugins/vue/server'
|
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Webpack Server Config
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
2018-03-16 16:12:06 +00:00
|
|
|
export default function webpackServerConfig() {
|
2017-12-08 14:18:37 +00:00
|
|
|
let config = base.call(this, { name: 'server', isServer: true })
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2018-01-04 23:34:20 +00:00
|
|
|
// Env object defined in nuxt.config.js
|
2016-11-25 14:37:06 +00:00
|
|
|
let env = {}
|
2018-03-16 19:11:24 +00:00
|
|
|
_.each(this.options.env, (value, key) => {
|
2018-01-13 05:22:11 +00:00
|
|
|
env['process.env.' + key] =
|
|
|
|
['boolean', 'number'].indexOf(typeof value) !== -1
|
|
|
|
? value
|
|
|
|
: JSON.stringify(value)
|
2016-11-25 14:37:06 +00:00
|
|
|
})
|
|
|
|
|
2018-01-04 23:34:20 +00:00
|
|
|
// Config devtool
|
|
|
|
config.devtool = this.options.dev ? 'cheap-source-map' : false
|
|
|
|
|
2016-11-09 22:59:41 +00:00
|
|
|
config = Object.assign(config, {
|
|
|
|
target: 'node',
|
2017-06-19 20:14:13 +00:00
|
|
|
node: false,
|
2018-03-16 19:11:24 +00:00
|
|
|
entry: path.resolve(this.options.buildDir, 'server.js'),
|
2016-11-10 18:34:59 +00:00
|
|
|
output: Object.assign({}, config.output, {
|
2016-11-09 22:59:41 +00:00
|
|
|
filename: 'server-bundle.js',
|
|
|
|
libraryTarget: 'commonjs2'
|
|
|
|
}),
|
2017-03-27 16:06:57 +00:00
|
|
|
performance: {
|
2017-07-31 20:46:46 +00:00
|
|
|
hints: false,
|
|
|
|
maxAssetSize: Infinity
|
2017-03-27 16:06:57 +00:00
|
|
|
},
|
2017-07-31 23:28:53 +00:00
|
|
|
externals: [],
|
2016-11-17 21:12:21 +00:00
|
|
|
plugins: (config.plugins || []).concat([
|
2017-04-27 08:54:18 +00:00
|
|
|
new VueSSRServerPlugin({
|
2017-03-22 14:47:34 +00:00
|
|
|
filename: 'server-bundle.json'
|
|
|
|
}),
|
2018-01-13 05:22:11 +00:00
|
|
|
new webpack.DefinePlugin(
|
|
|
|
Object.assign(env, {
|
|
|
|
'process.env.VUE_ENV': JSON.stringify('server'),
|
|
|
|
'process.mode': JSON.stringify(this.options.mode),
|
|
|
|
'process.browser': false,
|
|
|
|
'process.client': false,
|
|
|
|
'process.server': true,
|
|
|
|
'process.static': this.isStatic
|
|
|
|
})
|
|
|
|
)
|
2016-11-17 21:12:21 +00:00
|
|
|
])
|
2016-11-09 22:59:41 +00:00
|
|
|
})
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2017-07-31 23:28:53 +00:00
|
|
|
// https://webpack.js.org/configuration/externals/#externals
|
|
|
|
// https://github.com/liady/webpack-node-externals
|
2017-11-24 09:40:01 +00:00
|
|
|
this.options.modulesDir.forEach(dir => {
|
2018-03-16 19:11:24 +00:00
|
|
|
if (fs.existsSync(dir)) {
|
2018-01-13 05:22:11 +00:00
|
|
|
config.externals.push(
|
|
|
|
nodeExternals({
|
|
|
|
// load non-javascript files with extensions, presumably via loaders
|
|
|
|
whitelist: [/es6-promise|\.(?!(?:js|json)$).{1,5}$/i],
|
|
|
|
modulesDir: dir
|
|
|
|
})
|
|
|
|
)
|
2017-08-10 11:09:25 +00:00
|
|
|
}
|
|
|
|
})
|
2017-07-31 23:28:53 +00:00
|
|
|
|
2016-12-27 13:54:10 +00:00
|
|
|
// Extend config
|
|
|
|
if (typeof this.options.build.extend === 'function') {
|
2017-10-20 10:05:22 +00:00
|
|
|
const isDev = this.options.dev
|
2017-08-21 20:21:16 +00:00
|
|
|
const extendedConfig = this.options.build.extend.call(this, config, {
|
2017-10-20 10:05:22 +00:00
|
|
|
isDev,
|
2016-12-27 13:54:10 +00:00
|
|
|
isServer: true
|
|
|
|
})
|
2017-08-21 20:21:16 +00:00
|
|
|
// Only overwrite config when something is returned for backwards compatibility
|
|
|
|
if (extendedConfig !== undefined) {
|
|
|
|
config = extendedConfig
|
|
|
|
}
|
2016-12-27 13:54:10 +00:00
|
|
|
}
|
2017-06-15 22:19:53 +00:00
|
|
|
|
2016-11-09 22:59:41 +00:00
|
|
|
return config
|
|
|
|
}
|