|
| 1 | +import type { Plugin } from 'vite' |
| 2 | +import type { Nuxt } from '@nuxt/schema' |
| 3 | +import { withoutLeadingSlash } from 'ufo' |
| 4 | +import * as vite from 'vite' |
| 5 | +import { dirname, isAbsolute, join, relative, resolve } from 'pathe' |
| 6 | +import { useNitro } from '@nuxt/kit' |
| 7 | +import { resolveModulePath } from 'exsolve' |
| 8 | +import { defineEnv } from 'unenv' |
| 9 | + |
| 10 | +export function EnvironmentsPlugin (nuxt: Nuxt): Plugin { |
| 11 | + const fileNames = withoutLeadingSlash(join(nuxt.options.app.buildAssetsDir, '[hash].js')) |
| 12 | + const clientOutputDir = join(useNitro().options.output.publicDir, nuxt.options.app.buildAssetsDir) |
| 13 | + |
| 14 | + const clientAliases: Record<string, string> = { |
| 15 | + 'nitro/runtime': join(nuxt.options.buildDir, 'nitro.client.mjs'), |
| 16 | + // TODO: remove in v5 |
| 17 | + '#internal/nitro': join(nuxt.options.buildDir, 'nitro.client.mjs'), |
| 18 | + 'nitropack/runtime': join(nuxt.options.buildDir, 'nitro.client.mjs'), |
| 19 | + // work around vite optimizer bug |
| 20 | + '#app-manifest': resolveModulePath('mocked-exports/empty', { from: import.meta.url }), |
| 21 | + } |
| 22 | + |
| 23 | + return { |
| 24 | + name: 'nuxt:environments', |
| 25 | + config () { |
| 26 | + if (!nuxt.options.dev) { |
| 27 | + return { |
| 28 | + base: './', |
| 29 | + } |
| 30 | + } |
| 31 | + }, |
| 32 | + configEnvironment (name, config) { |
| 33 | + if (name === 'client') { |
| 34 | + const outputConfig = config.build?.rollupOptions?.output as vite.Rollup.OutputOptions |
| 35 | + return { |
| 36 | + build: { |
| 37 | + rollupOptions: { |
| 38 | + output: { |
| 39 | + chunkFileNames: outputConfig?.chunkFileNames ?? (nuxt.options.dev ? undefined : fileNames), |
| 40 | + entryFileNames: outputConfig?.entryFileNames ?? (nuxt.options.dev ? 'entry.js' : fileNames), |
| 41 | + sourcemapPathTransform: outputConfig?.sourcemapPathTransform ?? ((relativeSourcePath, sourcemapPath) => { |
| 42 | + // client build is running in a temporary build directory, like `.nuxt/dist/client` |
| 43 | + // so we need to transform the sourcemap path to be relative to the final build directory |
| 44 | + if (!isAbsolute(relativeSourcePath)) { |
| 45 | + const absoluteSourcePath = resolve(dirname(sourcemapPath), relativeSourcePath) |
| 46 | + return relative(clientOutputDir, absoluteSourcePath) |
| 47 | + } |
| 48 | + return relativeSourcePath |
| 49 | + }), |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (name === 'ssr') { |
| 57 | + if (config.build?.rollupOptions?.output && !Array.isArray(config.build.rollupOptions.output)) { |
| 58 | + config.build.rollupOptions.output.manualChunks = undefined |
| 59 | + |
| 60 | + // @ts-expect-error non-public property |
| 61 | + if (vite.rolldownVersion) { |
| 62 | + // @ts-expect-error rolldown-specific |
| 63 | + config.build.rollupOptions.output.advancedChunks = undefined |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + }, |
| 68 | + applyToEnvironment (environment) { |
| 69 | + if (environment.name === 'client') { |
| 70 | + return [ |
| 71 | + ...nuxt.options.experimental.clientNodeCompat ? [NodeCompatAliasPlugin()] : [], |
| 72 | + { |
| 73 | + name: 'nuxt:client:aliases', |
| 74 | + enforce: 'post', |
| 75 | + resolveId: source => clientAliases[source], |
| 76 | + }, |
| 77 | + ] |
| 78 | + } else if (environment.name === 'ssr') { |
| 79 | + // |
| 80 | + } |
| 81 | + return false |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function NodeCompatAliasPlugin (): Plugin { |
| 87 | + const nodeCompatAlias = defineEnv({ nodeCompat: true, resolve: true }).env.alias |
| 88 | + return { |
| 89 | + name: 'nuxt:client:node-compat-aliases', |
| 90 | + resolveId: { |
| 91 | + order: 'pre', |
| 92 | + handler (source) { |
| 93 | + if (source in nodeCompatAlias) { |
| 94 | + return nodeCompatAlias[source] |
| 95 | + } |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
0 commit comments