Nuxt/lib/builder/webpack/server.config.js

96 lines
2.9 KiB
JavaScript
Raw Normal View History

const webpack = require('webpack')
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin')
const nodeExternals = require('webpack-node-externals')
const { each } = require('lodash')
const { resolve } = require('path')
const { existsSync } = require('fs')
2018-01-13 05:22:11 +00:00
const { printWarn } = require('../../common/utils')
const base = require('./base.config.js')
2016-11-07 01:34:58 +00:00
/*
|--------------------------------------------------------------------------
| Webpack Server Config
|--------------------------------------------------------------------------
*/
module.exports = function webpackServerConfig() {
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 = {}
2017-01-11 19:14:59 +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
config = Object.assign(config, {
target: 'node',
2017-06-19 20:14:13 +00:00
node: false,
2017-06-11 14:17:36 +00:00
entry: resolve(this.options.buildDir, 'server.js'),
2016-11-10 18:34:59 +00:00
output: Object.assign({}, config.output, {
filename: 'server-bundle.js',
libraryTarget: 'commonjs2'
}),
2017-03-27 16:06:57 +00:00
performance: {
hints: false,
maxAssetSize: Infinity
2017-03-27 16:06:57 +00:00
},
externals: [],
plugins: (config.plugins || []).concat([
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.NODE_ENV': JSON.stringify(
env.NODE_ENV || (this.options.dev ? 'development' : 'production')
),
'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
})
)
])
})
2017-06-15 22:19: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 => {
if (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
})
)
}
})
2016-12-27 13:54:10 +00:00
// Extend config
if (typeof this.options.build.extend === 'function') {
const isDev = this.options.dev
const extendedConfig = this.options.build.extend.call(this, config, {
get dev() {
2018-01-13 05:22:11 +00:00
printWarn('dev has been deprecated in build.extend(), please use isDev')
return isDev
},
isDev,
2016-12-27 13:54:10 +00:00
isServer: true
})
// 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
return config
}