2024-11-02 21:13:41 +00:00
|
|
|
import { isAbsolute, resolve } from 'pathe'
|
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'
|
2023-10-23 14:35:06 +00:00
|
|
|
import { applyPresets } from '../utils/config'
|
2020-09-02 12:27:27 +00:00
|
|
|
import { nuxt } from '../presets/nuxt'
|
|
|
|
import { node } from '../presets/node'
|
2024-10-09 13:57:54 +00:00
|
|
|
import { webpack } from '#builder'
|
2020-09-02 12:27:27 +00:00
|
|
|
|
2024-05-14 17:54:37 +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
|
|
|
|
2024-07-02 18:28:48 +00:00
|
|
|
export async function server (ctx: WebpackConfigContext) {
|
2020-09-02 12:27:27 +00:00
|
|
|
ctx.name = 'server'
|
|
|
|
ctx.isServer = true
|
|
|
|
|
2024-07-02 18:28:48 +00:00
|
|
|
await applyPresets(ctx, [
|
2020-09-02 12:27:27 +00:00
|
|
|
nuxt,
|
|
|
|
node,
|
|
|
|
serverStandalone,
|
|
|
|
serverPreset,
|
2024-04-05 18:08:32 +00:00
|
|
|
serverPlugins,
|
2020-09-02 12:27:27 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2024-04-05 18:08:32 +00:00
|
|
|
minimize: false,
|
2020-09-02 12:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
'#',
|
2024-04-05 18:08:32 +00:00
|
|
|
...ctx.options.build.transpile,
|
2021-04-03 12:42:02 +00:00
|
|
|
]
|
2024-11-02 21:13:41 +00:00
|
|
|
const external = [
|
|
|
|
'#internal/nitro',
|
|
|
|
'#shared',
|
|
|
|
resolve(ctx.nuxt.options.rootDir, ctx.nuxt.options.dir.shared),
|
|
|
|
]
|
2024-03-21 11:57:11 +00:00
|
|
|
if (!ctx.nuxt.options.dev) {
|
|
|
|
external.push('#internal/nuxt/paths')
|
|
|
|
}
|
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'],
|
2024-04-05 18:08:32 +00:00
|
|
|
URLSearchParams: [ctx.userConfig.serverURLPolyfill, 'URLSearchParams'],
|
2020-09-02 12:27:27 +00:00
|
|
|
}))
|
|
|
|
}
|
2022-04-15 15:28:42 +00:00
|
|
|
|
|
|
|
// Add type-checking
|
2023-12-05 17:09:46 +00:00
|
|
|
if (!ctx.nuxt.options.test && (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({
|
2024-04-05 18:08:32 +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
|
|
|
}
|