2021-09-27 12:49:36 +00:00
|
|
|
import { isAbsolute } from 'pathe'
|
2021-10-02 16:01:17 +00:00
|
|
|
import webpack from 'webpack'
|
2022-04-15 15:28:42 +00:00
|
|
|
import ForkTSCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
|
|
|
|
import { logger } from '@nuxt/kit'
|
2022-12-11 21:44:52 +00:00
|
|
|
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'
|
|
|
|
|
2022-04-15 10:58:22 +00:00
|
|
|
const assetPattern = /\.(css|s[ca]ss|png|jpe?g|gif|svg|woff2?|eot|ttf|otf|webp|webm|mp4|ogv)(\?.*)?$/i
|
2021-09-05 20:34:56 +00:00
|
|
|
|
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) {
|
2023-07-24 19:46:09 +00:00
|
|
|
ctx.config.output!.filename = 'server.mjs'
|
2020-09-02 12:27:27 +00:00
|
|
|
|
2023-08-24 12:06:44 +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
|
|
|
|
}
|
2022-09-07 11:32:10 +00:00
|
|
|
|
2023-07-24 19:46:09 +00:00
|
|
|
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/',
|
2021-08-11 20:28:38 +00:00
|
|
|
'#app',
|
2022-04-20 08:52:39 +00:00
|
|
|
'nuxt',
|
2022-02-18 18:26:43 +00:00
|
|
|
'nuxt3',
|
2023-10-12 14:17:38 +00:00
|
|
|
'nuxt-nightly',
|
2021-04-18 16:37:21 +00:00
|
|
|
'!',
|
2021-04-03 12:42:02 +00:00
|
|
|
'-!',
|
2021-04-19 20:41:02 +00:00
|
|
|
'~',
|
2021-04-20 13:22:24 +00:00
|
|
|
'@/',
|
2021-04-19 20:41: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) => {
|
2022-08-26 15:47:29 +00:00
|
|
|
if (!request) {
|
|
|
|
return cb(undefined, false)
|
|
|
|
}
|
2022-01-18 16:59:14 +00:00
|
|
|
if (external.includes(request)) {
|
2022-08-26 15:47:29 +00:00
|
|
|
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) ||
|
2022-02-15 09:50:11 +00:00
|
|
|
inline.find(prefix => typeof prefix === 'string' && request.startsWith(prefix)) ||
|
2021-09-05 20:34:56 +00:00
|
|
|
assetPattern.test(request)
|
2021-04-03 12:42:02 +00:00
|
|
|
) {
|
2021-04-20 13:22:24 +00:00
|
|
|
// console.log('Inline', request)
|
2022-08-26 15:47:29 +00:00
|
|
|
return cb(undefined, false)
|
2021-04-03 12:42:02 +00:00
|
|
|
}
|
2021-04-20 13:22:24 +00:00
|
|
|
// console.log('Ext', request)
|
2022-08-26 15:47:29 +00:00
|
|
|
return cb(undefined, true)
|
2021-04-03 12:42:02 +00:00
|
|
|
})
|
2020-09-02 12:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function serverPlugins (ctx: WebpackConfigContext) {
|
2023-07-24 19:46:09 +00:00
|
|
|
ctx.config.plugins = ctx.config.plugins || []
|
2022-08-26 15:47:29 +00:00
|
|
|
|
2020-09-02 12:27:27 +00:00
|
|
|
// Server polyfills
|
2023-07-24 19:46:09 +00:00
|
|
|
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
|
|
|
}))
|
|
|
|
}
|
2022-04-15 15:28:42 +00:00
|
|
|
|
|
|
|
// Add type-checking
|
|
|
|
if (ctx.nuxt.options.typescript.typeCheck === true || (ctx.nuxt.options.typescript.typeCheck === 'build' && !ctx.nuxt.options.dev)) {
|
2023-07-24 19:46:09 +00:00
|
|
|
ctx.config.plugins!.push(new ForkTSCheckerWebpackPlugin({
|
2023-03-07 14:25:27 +00:00
|
|
|
logger
|
2023-02-06 18:35:47 +00:00
|
|
|
}))
|
2022-04-15 15:28:42 +00:00
|
|
|
}
|
2020-09-02 12:27:27 +00:00
|
|
|
}
|