2018-03-16 19:52:17 +00:00
|
|
|
import path from 'path'
|
|
|
|
import fs from 'fs'
|
2020-01-19 08:37:06 +00:00
|
|
|
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
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
import VueSSRServerPlugin from '../plugins/vue/server'
|
|
|
|
|
2018-10-24 16:55:18 +00:00
|
|
|
import WebpackBaseConfig from './base'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2020-06-09 16:40:39 +00:00
|
|
|
const nativeFileExtensions = [
|
|
|
|
'.json',
|
|
|
|
'.js'
|
|
|
|
]
|
|
|
|
|
2018-10-24 16:55:18 +00:00
|
|
|
export default class WebpackServerConfig extends WebpackBaseConfig {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (...args) {
|
2019-02-18 17:00:51 +00:00
|
|
|
super(...args)
|
|
|
|
this.name = 'server'
|
|
|
|
this.isServer = true
|
2018-03-22 14:06:54 +00:00
|
|
|
}
|
2016-11-25 14:37:06 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
get devtool () {
|
2018-12-01 10:13:28 +00:00
|
|
|
return 'cheap-module-source-map'
|
2018-08-22 14:24:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 14:23:33 +00:00
|
|
|
get externalsWhitelist () {
|
|
|
|
return [
|
2020-06-09 16:40:39 +00:00
|
|
|
this.isNonNativeImport.bind(this),
|
2019-08-04 14:23:33 +00:00
|
|
|
...this.normalizeTranspile()
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:40:39 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
env () {
|
2019-11-24 15:39:45 +00:00
|
|
|
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
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
optimization () {
|
2018-08-22 14:24:47 +00:00
|
|
|
return {
|
|
|
|
splitChunks: false,
|
2018-12-01 10:13:28 +00:00
|
|
|
minimizer: this.minimizer()
|
2018-08-22 14:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
resolve () {
|
2019-04-02 13:00:32 +00:00
|
|
|
const resolveConfig = super.resolve()
|
|
|
|
|
|
|
|
resolveConfig.resolve.mainFields = ['main', 'module']
|
|
|
|
|
|
|
|
return resolveConfig
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
alias () {
|
2019-03-31 21:15:46 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
plugins () {
|
2018-03-22 14:06:54 +00:00
|
|
|
const plugins = super.plugins()
|
|
|
|
plugins.push(
|
2020-01-19 08:37:06 +00:00
|
|
|
new VueSSRServerPlugin({ filename: `${this.name}.manifest.json` }),
|
|
|
|
new DefinePlugin(this.env())
|
2018-03-22 14:06:54 +00:00
|
|
|
)
|
2020-01-19 08:37:06 +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
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
config () {
|
2018-08-16 17:14:26 +00:00
|
|
|
const config = super.config()
|
2018-03-22 14:06:54 +00:00
|
|
|
|
|
|
|
Object.assign(config, {
|
|
|
|
target: 'node',
|
|
|
|
node: false,
|
2019-03-14 10:05:19 +00:00
|
|
|
entry: Object.assign({}, config.entry, {
|
2019-02-18 17:00:51 +00:00
|
|
|
app: [path.resolve(this.buildContext.options.buildDir, 'server.js')]
|
2019-03-14 10:05:19 +00:00
|
|
|
}),
|
2018-03-22 14:06:54 +00:00
|
|
|
output: Object.assign({}, config.output, {
|
2018-12-01 10:13:28 +00:00
|
|
|
filename: 'server.js',
|
2018-03-22 14:06:54 +00:00
|
|
|
libraryTarget: 'commonjs2'
|
|
|
|
}),
|
|
|
|
performance: {
|
|
|
|
hints: false,
|
2018-12-01 10:13:28 +00:00
|
|
|
maxEntrypointSize: Infinity,
|
2018-03-22 14:06:54 +00:00
|
|
|
maxAssetSize: Infinity
|
|
|
|
},
|
2019-04-02 12:57:45 +00:00
|
|
|
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
|
2018-05-02 08:27:26 +00:00
|
|
|
// https://vue-loader.vuejs.org/migrating.html#ssr-externals
|
2019-04-03 09:44:36 +00:00
|
|
|
if (!this.buildContext.buildOptions.standalone) {
|
2019-02-18 17:00:51 +00:00
|
|
|
this.buildContext.options.modulesDir.forEach((dir) => {
|
2019-01-03 20:27:50 +00:00
|
|
|
if (fs.existsSync(dir)) {
|
|
|
|
config.externals.push(
|
|
|
|
nodeExternals({
|
2019-08-04 14:23:33 +00:00
|
|
|
whitelist: this.externalsWhitelist,
|
2019-01-03 20:27:50 +00:00
|
|
|
modulesDir: dir
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-03-22 14:06:54 +00:00
|
|
|
|
2018-09-10 08:27:01 +00:00
|
|
|
return config
|
2018-03-22 14:06:54 +00:00
|
|
|
}
|
2016-11-09 22:59:41 +00:00
|
|
|
}
|