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

148 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-03-16 19:52:17 +00:00
import path from 'path'
import fs from 'fs'
import { DefinePlugin, ProvidePlugin } from 'webpack'
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
const nativeFileExtensions = [
'.json',
'.js'
]
export default class WebpackServerConfig extends WebpackBaseConfig {
constructor (...args) {
super(...args)
this.name = 'server'
this.isServer = true
2018-03-22 14:06:54 +00:00
}
2016-11-25 14:37:06 +00:00
get devtool () {
return 'cheap-module-source-map'
}
get externalsAllowlist () {
return [
this.isNonNativeImport.bind(this),
...this.normalizeTranspile()
]
}
/**
* files *not* ending on js|json should be processed by webpack
*
* this might generate false-positives for imports like
* - "someFile.umd" (actually requiring someFile.umd.js)
* - "some.folder" (some.folder being a directory containing a package.json)
*/
isNonNativeImport (modulePath) {
const extname = path.extname(modulePath)
return extname !== '' && !nativeFileExtensions.includes(extname)
}
env () {
return Object.assign(
super.env(),
{
'process.env.VUE_ENV': JSON.stringify('server'),
'process.browser': false,
'process.client': false,
'process.server': true,
'process.modern': false
}
)
2018-03-22 14:06:54 +00:00
}
2018-01-04 23:34:20 +00:00
optimization () {
const { _minifyServer } = this.buildContext.buildOptions
return {
splitChunks: false,
minimizer: _minifyServer ? this.minimizer() : []
}
}
resolve () {
const resolveConfig = super.resolve()
resolveConfig.resolve.mainFields = ['main', 'module']
return resolveConfig
}
alias () {
const aliases = super.alias()
for (const p of this.buildContext.plugins) {
if (!aliases[p.name]) {
// Do not load client-side plugins on server-side
aliases[p.name] = p.mode === 'client' ? './empty.js' : p.src
}
}
return aliases
}
plugins () {
2018-03-22 14:06:54 +00:00
const plugins = super.plugins()
plugins.push(
new VueSSRServerPlugin({ filename: `${this.name}.manifest.json` }),
new DefinePlugin(this.env())
2018-03-22 14:06:54 +00:00
)
const { serverURLPolyfill } = this.buildContext.options.build
if (serverURLPolyfill) {
plugins.push(new ProvidePlugin({
URL: [serverURLPolyfill, 'URL'],
URLSearchParams: [serverURLPolyfill, 'URLSearchParams']
}))
}
2018-03-22 14:06:54 +00:00
return plugins
}
2017-06-15 22:19:53 +00:00
config () {
const config = super.config()
2018-03-22 14:06:54 +00:00
Object.assign(config, {
target: 'node',
node: false,
entry: Object.assign({}, config.entry, {
app: [path.resolve(this.buildContext.options.buildDir, 'server.js')]
}),
2018-03-22 14:06:54 +00:00
output: Object.assign({}, config.output, {
filename: 'server.js',
chunkFilename: '[name].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: [].concat(config.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
if (!this.buildContext.buildOptions.standalone) {
this.buildContext.options.modulesDir.forEach((dir) => {
if (fs.existsSync(dir)) {
config.externals.push(
nodeExternals({
allowlist: this.externalsAllowlist,
modulesDir: dir
})
)
}
})
}
2018-03-22 14:06:54 +00:00
return config
2018-03-22 14:06:54 +00:00
}
}