2020-09-02 12:27:27 +00:00
|
|
|
import { ProvidePlugin } from 'webpack'
|
|
|
|
import { WebpackConfigContext, applyPresets, getWebpackConfig } from '../utils/config'
|
|
|
|
import { nuxt } from '../presets/nuxt'
|
|
|
|
import { node } from '../presets/node'
|
|
|
|
|
|
|
|
export function server (ctx: WebpackConfigContext) {
|
|
|
|
ctx.name = 'server'
|
|
|
|
ctx.isServer = true
|
|
|
|
|
|
|
|
applyPresets(ctx, [
|
|
|
|
nuxt,
|
|
|
|
node,
|
|
|
|
serverStandalone,
|
|
|
|
serverPreset,
|
|
|
|
serverPlugins
|
|
|
|
])
|
|
|
|
|
|
|
|
return getWebpackConfig(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
function serverPreset (ctx: WebpackConfigContext) {
|
|
|
|
const { config } = ctx
|
|
|
|
|
|
|
|
config.output.filename = 'server.js'
|
|
|
|
config.devtool = 'cheap-module-source-map'
|
|
|
|
|
|
|
|
config.optimization = {
|
|
|
|
splitChunks: false,
|
|
|
|
minimize: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function serverStandalone (ctx: WebpackConfigContext) {
|
2021-04-03 12:42:02 +00:00
|
|
|
// TODO: Refactor this out of webpack
|
|
|
|
const inline = [
|
|
|
|
'src/',
|
|
|
|
'nuxt/app',
|
|
|
|
'nuxt/build',
|
|
|
|
'@nuxt/app',
|
|
|
|
'vuex5',
|
|
|
|
'-!',
|
|
|
|
...ctx.options.build.transpile
|
|
|
|
]
|
2020-09-02 12:27:27 +00:00
|
|
|
|
2021-04-03 12:42:02 +00:00
|
|
|
if (!Array.isArray(ctx.config.externals)) { return }
|
|
|
|
ctx.config.externals.push(({ request }, cb) => {
|
|
|
|
if (
|
|
|
|
request[0] === '.' ||
|
|
|
|
request[0] === '/' ||
|
|
|
|
inline.find(prefix => request.startsWith(prefix))
|
|
|
|
) {
|
|
|
|
return cb(null, false)
|
|
|
|
}
|
|
|
|
// console.log('External:', request)
|
|
|
|
return cb(null, true)
|
|
|
|
})
|
2020-09-02 12:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function serverPlugins (ctx: WebpackConfigContext) {
|
|
|
|
const { config, options } = ctx
|
|
|
|
|
|
|
|
// Server polyfills
|
|
|
|
if (options.build.serverURLPolyfill) {
|
|
|
|
config.plugins.push(new ProvidePlugin({
|
|
|
|
URL: [options.build.serverURLPolyfill, 'URL'],
|
|
|
|
URLSearchParams: [options.build.serverURLPolyfill, 'URLSearchParams']
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|