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

89 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-01-11 19:14:59 +00:00
import webpack from 'webpack'
import VueSSRServerPlugin from 'vue-server-renderer/server-plugin'
2017-05-24 13:04:17 +00:00
import nodeExternals from 'webpack-node-externals'
import { each } from 'lodash'
2017-01-11 19:14:59 +00:00
import { resolve } from 'path'
import { existsSync } from 'fs'
2017-06-12 20:32:34 +00:00
import base from './base.config.js'
2016-11-07 01:34:58 +00:00
/*
|--------------------------------------------------------------------------
| Webpack Server Config
|--------------------------------------------------------------------------
*/
2017-06-11 14:17:36 +00:00
export default function webpackServerConfig () {
let config = base.call(this, 'server')
2017-06-15 22:19:53 +00:00
2016-11-25 14:37:06 +00:00
// env object defined in nuxt.config.js
let env = {}
2017-01-11 19:14:59 +00:00
each(this.options.env, (value, key) => {
2016-11-25 14:37:06 +00:00
env['process.env.' + key] = (typeof value === 'string' ? JSON.stringify(value) : value)
})
config = Object.assign(config, {
target: 'node',
2017-06-19 20:14:13 +00:00
node: false,
devtool: 'source-map',
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'
}),
2016-11-25 14:37:06 +00:00
new webpack.DefinePlugin(Object.assign(env, {
2017-06-15 22:19:53 +00:00
'process.env.NODE_ENV': JSON.stringify(env.NODE_ENV || (this.options.dev ? 'development' : 'production')),
'process.env.VUE_ENV': JSON.stringify('server'),
2017-02-28 12:10:58 +00:00
'process.browser': false,
2017-08-17 12:43:51 +00:00
'process.server': true,
'process.static': this.isStatic
2016-11-25 14:37:06 +00:00
}))
])
})
2017-06-15 22:19:53 +00:00
// https://webpack.js.org/configuration/externals/#externals
// https://github.com/liady/webpack-node-externals
const moduleDirs = [
this.options.modulesDir
// Temporary disabled due to vue-server-renderer module search limitations
// resolve(__dirname, '..', 'node_modules')
]
moduleDirs.forEach(dir => {
if (existsSync(dir)) {
config.externals.push(nodeExternals({
// load non-javascript files with extensions, presumably via loaders
whitelist: [/es6-promise|\.(?!(?:js|json)$).{1,5}$/i],
modulesDir: dir
}))
}
})
2017-06-15 22:19:53 +00:00
// --------------------------------------
// Production specific config
// --------------------------------------
2017-06-11 14:17:36 +00:00
if (!this.options.dev) {
2017-06-15 22:19:53 +00:00
}
2016-12-27 13:54:10 +00:00
// Extend config
if (typeof this.options.build.extend === 'function') {
const extendedConfig = this.options.build.extend.call(this, config, {
2017-06-11 14:17:36 +00:00
dev: this.options.dev,
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
}