Nuxt/packages/webpack/src/configs/server.ts

80 lines
1.9 KiB
TypeScript
Raw Normal View History

import { isAbsolute } from 'pathe'
import webpack from 'webpack'
2020-09-02 12:27:27 +00:00
import { WebpackConfigContext, applyPresets, getWebpackConfig } from '../utils/config'
import { nuxt } from '../presets/nuxt'
import { node } from '../presets/node'
const assetPattern = /\.(png|jpe?g|gif|svg|woff2?|eot|ttf|otf|webp|webm|mp4|ogv)(\?.*)?$/i
2020-09-02 12:27:27 +00:00
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.mjs'
2020-09-02 12:27:27 +00:00
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/',
'#app',
2021-04-18 16:37:21 +00:00
'!',
2021-04-03 12:42:02 +00:00
'-!',
'~',
'@/',
'#',
2021-04-03 12:42:02 +00:00
...ctx.options.build.transpile
]
2022-01-18 16:59:14 +00:00
const external = ['#config']
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) => {
2022-01-18 16:59:14 +00:00
if (external.includes(request)) {
return cb(null, true)
}
2021-04-03 12:42:02 +00:00
if (
request[0] === '.' ||
2021-08-09 18:24:52 +00:00
isAbsolute(request) ||
inline.find(prefix => typeof prefix === 'string' && request.startsWith(prefix)) ||
assetPattern.test(request)
2021-04-03 12:42:02 +00:00
) {
// console.log('Inline', request)
2021-04-03 12:42:02 +00:00
return cb(null, false)
}
// console.log('Ext', request)
2021-04-03 12:42:02 +00:00
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 webpack.ProvidePlugin({
2020-09-02 12:27:27 +00:00
URL: [options.build.serverURLPolyfill, 'URL'],
URLSearchParams: [options.build.serverURLPolyfill, 'URLSearchParams']
}))
}
}