Nuxt/packages/webpack/src/config/server.js

103 lines
2.4 KiB
JavaScript
Raw Normal View History

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'
2018-11-15 22:02:55 +00:00
import escapeRegExp from 'lodash/escapeRegExp'
2018-03-16 16:12:06 +00:00
import nodeExternals from 'webpack-node-externals'
2018-03-16 19:52:17 +00:00
import VueSSRServerPlugin from '../plugins/vue/server'
import WebpackBaseConfig from './base'
2018-03-16 19:52:17 +00:00
export default class WebpackServerConfig extends WebpackBaseConfig {
2018-03-22 14:06:54 +00:00
constructor(builder) {
super(builder, { name: 'server', isServer: true })
2018-11-15 22:02:55 +00:00
this.whitelist = this.normalizeWhitelist()
}
normalizeWhitelist() {
const whitelist = [
/\.css$/,
/\?vue&type=style/
]
for (const pattern of this.options.build.transpile) {
if (pattern instanceof RegExp) {
whitelist.push(pattern)
} else {
const posixModule = pattern.replace(/\\/g, '/')
whitelist.push(new RegExp(escapeRegExp(posixModule)))
}
}
return whitelist
2018-03-22 14:06:54 +00:00
}
2016-11-25 14:37:06 +00:00
get devtool() {
return 'cheap-module-source-map'
}
2018-03-22 14:06:54 +00:00
env() {
return Object.assign(super.env(), {
'process.env.VUE_ENV': JSON.stringify('server'),
'process.browser': false,
'process.client': false,
2018-12-12 09:33:19 +00:00
'process.server': true,
'process.modern': false
2018-03-22 14:06:54 +00:00
})
}
2018-01-04 23:34:20 +00:00
optimization() {
return {
splitChunks: false,
minimizer: this.minimizer()
}
}
2018-03-22 14:06:54 +00:00
plugins() {
const plugins = super.plugins()
plugins.push(
new VueSSRServerPlugin({
filename: `${this.name}.manifest.json`
2017-03-22 14:47:34 +00:00
}),
2018-03-22 14:06:54 +00:00
new webpack.DefinePlugin(this.env())
)
return plugins
}
2017-06-15 22:19:53 +00:00
2018-03-22 14:06:54 +00:00
config() {
const config = super.config()
2018-03-22 14:06:54 +00:00
Object.assign(config, {
target: 'node',
node: false,
entry: {
app: [path.resolve(this.options.buildDir, 'server.js')]
},
2018-03-22 14:06:54 +00:00
output: Object.assign({}, config.output, {
filename: 'server.js',
2018-03-22 14:06:54 +00:00
libraryTarget: 'commonjs2'
}),
performance: {
hints: false,
maxEntrypointSize: Infinity,
2018-03-22 14:06:54 +00:00
maxAssetSize: Infinity
},
externals: []
2018-03-22 14:06:54 +00:00
})
// https://webpack.js.org/configuration/externals/#externals
// https://github.com/liady/webpack-node-externals
// https://vue-loader.vuejs.org/migrating.html#ssr-externals
this.options.modulesDir.forEach((dir) => {
2018-03-22 14:06:54 +00:00
if (fs.existsSync(dir)) {
config.externals.push(
nodeExternals({
2018-11-15 22:02:55 +00:00
whitelist: this.whitelist,
2018-03-22 14:06:54 +00:00
modulesDir: dir
})
)
}
2016-12-27 13:54:10 +00:00
})
2018-03-22 14:06:54 +00:00
return config
2018-03-22 14:06:54 +00:00
}
}