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'
|
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'
|
|
|
|
|
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) {
|
|
|
|
const { config } = ctx
|
|
|
|
|
2021-09-05 20:33:24 +00:00
|
|
|
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/',
|
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',
|
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-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) ||
|
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)
|
2021-04-03 12:42:02 +00:00
|
|
|
return cb(null, false)
|
|
|
|
}
|
2021-04-20 13:22:24 +00:00
|
|
|
// 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
|
2022-02-25 19:11:01 +00:00
|
|
|
if (options.webpack.serverURLPolyfill) {
|
2021-10-02 16:01:17 +00:00
|
|
|
config.plugins.push(new webpack.ProvidePlugin({
|
2022-02-25 19:11:01 +00:00
|
|
|
URL: [options.webpack.serverURLPolyfill, 'URL'],
|
|
|
|
URLSearchParams: [options.webpack.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)) {
|
|
|
|
ctx.config.plugins.push(new ForkTSCheckerWebpackPlugin({ logger }))
|
|
|
|
}
|
2020-09-02 12:27:27 +00:00
|
|
|
}
|