Nuxt/packages/webpack/src/configs/server.ts
Daniel Roe 7458dd1aa6
fix(nuxt3): use shared module for dynamic paths (#3757)
* test: add tests for dynamic paths + relative assets

Co-authored-by: Anthony Fu <hi@antfu.me>

* fix: rework client config and use shared module for dynamic paths

* resolves webpack public path regression

* refactor: use more similar names to nitro equivalent

* fix: align config exports

* refactor: remove `__` prefix within dynamic paths module

* refactor: use '#_config' to indicate internal alias

* Update packages/nuxt3/src/core/templates.ts

Co-authored-by: pooya parsa <pyapar@gmail.com>

* refactor: use `#_config` alias and rename to `paths.mjs`

Co-authored-by: Anthony Fu <hi@antfu.me>
Co-authored-by: pooya parsa <pyapar@gmail.com>
2022-03-22 16:51:26 +01:00

81 lines
1.9 KiB
TypeScript

import { isAbsolute } from 'pathe'
import webpack from 'webpack'
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
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'
config.devtool = 'cheap-module-source-map'
config.optimization = {
splitChunks: false,
minimize: false
}
}
function serverStandalone (ctx: WebpackConfigContext) {
// TODO: Refactor this out of webpack
const inline = [
'src/',
'#app',
'nuxt3',
'!',
'-!',
'~',
'@/',
'#',
...ctx.options.build.transpile
]
const external = ['#_config']
if (!Array.isArray(ctx.config.externals)) { return }
ctx.config.externals.push(({ request }, cb) => {
if (external.includes(request)) {
return cb(null, true)
}
if (
request[0] === '.' ||
isAbsolute(request) ||
inline.find(prefix => typeof prefix === 'string' && request.startsWith(prefix)) ||
assetPattern.test(request)
) {
// console.log('Inline', request)
return cb(null, false)
}
// console.log('Ext', request)
return cb(null, true)
})
}
function serverPlugins (ctx: WebpackConfigContext) {
const { config, options } = ctx
// Server polyfills
if (options.webpack.serverURLPolyfill) {
config.plugins.push(new webpack.ProvidePlugin({
URL: [options.webpack.serverURLPolyfill, 'URL'],
URLSearchParams: [options.webpack.serverURLPolyfill, 'URLSearchParams']
}))
}
}