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

100 lines
2.6 KiB
TypeScript
Raw Normal View History

import { isAbsolute } from 'pathe'
import webpack from 'webpack'
import ForkTSCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import { logger } from '@nuxt/kit'
import type { WebpackConfigContext } from '../utils/config'
import { applyPresets, getWebpackConfig } from '../utils/config'
2020-09-02 12:27:27 +00:00
import { nuxt } from '../presets/nuxt'
import { node } from '../presets/node'
const assetPattern = /\.(css|s[ca]ss|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) {
ctx.config.output!.filename = 'server.mjs'
2020-09-02 12:27:27 +00:00
if (ctx.nuxt.options.sourcemap.server) {
const prefix = ctx.nuxt.options.sourcemap.server === 'hidden' ? 'hidden-' : ''
ctx.config.devtool = prefix + ctx.isDev ? 'cheap-module-source-map' : 'source-map'
} else {
ctx.config.devtool = false
}
ctx.config.optimization = {
2020-09-02 12:27:27 +00:00
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',
'nuxt',
'nuxt3',
'nuxt-nightly',
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-04-19 19:10:32 +00:00
const external = ['#internal/nitro']
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) {
return cb(undefined, false)
}
2022-01-18 16:59:14 +00:00
if (external.includes(request)) {
return cb(undefined, true)
2022-01-18 16:59:14 +00:00
}
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)
return cb(undefined, false)
2021-04-03 12:42:02 +00:00
}
// console.log('Ext', request)
return cb(undefined, true)
2021-04-03 12:42:02 +00:00
})
2020-09-02 12:27:27 +00:00
}
function serverPlugins (ctx: WebpackConfigContext) {
ctx.config.plugins = ctx.config.plugins || []
2020-09-02 12:27:27 +00:00
// Server polyfills
if (ctx.userConfig.serverURLPolyfill) {
ctx.config.plugins.push(new webpack.ProvidePlugin({
URL: [ctx.userConfig.serverURLPolyfill, 'URL'],
URLSearchParams: [ctx.userConfig.serverURLPolyfill, 'URLSearchParams']
2020-09-02 12:27:27 +00:00
}))
}
// Add type-checking
if (ctx.nuxt.options.typescript.typeCheck === true || (ctx.nuxt.options.typescript.typeCheck === 'build' && !ctx.nuxt.options.dev)) {
ctx.config.plugins!.push(new ForkTSCheckerWebpackPlugin({
logger
}))
}
2020-09-02 12:27:27 +00:00
}