diff --git a/packages/nuxt/src/core/app.ts b/packages/nuxt/src/core/app.ts index 4bc7da1e91..342096a0e3 100644 --- a/packages/nuxt/src/core/app.ts +++ b/packages/nuxt/src/core/app.ts @@ -258,7 +258,7 @@ export async function annotatePlugins (nuxt: Nuxt, plugins: NuxtPlugin[]) { try { const code = plugin.src in nuxt.vfs ? nuxt.vfs[plugin.src] : await fsp.readFile(plugin.src!, 'utf-8') _plugins.push({ - ...await extractMetadata(code, IS_TSX.test(plugin.src) ? 'tsx' : 'ts'), + ...await extractMetadata(code, IS_TSX.test(plugin.src) ? 'tsx' : 'ts', nuxt.options.esbuild.options), ...plugin, }) } catch (e) { diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 8c45a05e10..971b63ac18 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -87,7 +87,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { esbuild: { options: { exclude: excludePattern, - ...nuxt.options.esbuild + ...nuxt.options.esbuild.options }, }, analyze: !nuxt.options.test && nuxt.options.build.analyze && (nuxt.options.build.analyze === true || nuxt.options.build.analyze.enabled) diff --git a/packages/nuxt/src/core/plugins/plugin-metadata.ts b/packages/nuxt/src/core/plugins/plugin-metadata.ts index 331c5b8d9d..9dc606351c 100644 --- a/packages/nuxt/src/core/plugins/plugin-metadata.ts +++ b/packages/nuxt/src/core/plugins/plugin-metadata.ts @@ -2,6 +2,7 @@ import type { CallExpression, Literal, Property, SpreadElement } from 'estree' import type { Node } from 'estree-walker' import { walk } from 'estree-walker' import { transform } from 'esbuild' +import type { TransformOptions } from 'esbuild' import { parse } from 'acorn' import { defu } from 'defu' import { findExports } from 'mlly' @@ -41,12 +42,12 @@ export const orderMap: Record, number> = { } const metaCache: Record> = {} -export async function extractMetadata (code: string, loader = 'ts' as 'ts' | 'tsx') { +export async function extractMetadata (code: string, loader = 'ts' as 'ts' | 'tsx', esbuildOptions?: TransformOptions) { let meta: PluginMeta = {} if (metaCache[code]) { return metaCache[code] } - const js = await transform(code, { loader }) + const js = await transform(code, { loader, ...esbuildOptions }) walk(parse(js.code, { sourceType: 'module', ecmaVersion: 'latest', diff --git a/packages/nuxt/src/core/plugins/prehydrate.ts b/packages/nuxt/src/core/plugins/prehydrate.ts index e47fa3883e..15c21b8851 100644 --- a/packages/nuxt/src/core/plugins/prehydrate.ts +++ b/packages/nuxt/src/core/plugins/prehydrate.ts @@ -37,7 +37,7 @@ export function prehydrateTransformPlugin (nuxt: Nuxt) { const needsAttr = node.arguments[0].params.length > 0 const { start, end } = node.arguments[0] as Node & { start: number, end: number } - const p = transform(`forEach(${code.slice(start, end)})`, { loader: 'ts', minify: true }) + const p = transform(`forEach(${code.slice(start, end)})`, { loader: 'ts', minify: true, ...nuxt.options.esbuild.options }) promises.push(p.then(({ code: result }) => { const cleaned = result.slice('forEach'.length).replace(/;\s+$/, '') const args = [JSON.stringify(cleaned)] diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index 3e5982ea3b..ed590e89f4 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -303,7 +303,7 @@ export default defineNuxtModule({ const glob = pageToGlobMap[path] const code = path in nuxt.vfs ? nuxt.vfs[path] : await readFile(path!, 'utf-8') try { - const extractedRule = await extractRouteRules(code) + const extractedRule = await extractRouteRules(code, nuxt.options.esbuild.options) if (extractedRule) { if (!glob) { const relativePath = relative(nuxt.options.srcDir, path) diff --git a/packages/nuxt/src/pages/route-rules.ts b/packages/nuxt/src/pages/route-rules.ts index 24acf217b1..f2f0532177 100644 --- a/packages/nuxt/src/pages/route-rules.ts +++ b/packages/nuxt/src/pages/route-rules.ts @@ -3,6 +3,7 @@ import type { Node } from 'estree-walker' import type { CallExpression } from 'estree' import { walk } from 'estree-walker' import { transform } from 'esbuild' +import type { TransformOptions } from 'esbuild' import { parse } from 'acorn' import type { NuxtPage } from '@nuxt/schema' import type { NitroRouteConfig } from 'nitropack' @@ -12,7 +13,7 @@ import { extractScriptContent, pathToNitroGlob } from './utils' const ROUTE_RULE_RE = /\bdefineRouteRules\(/ const ruleCache: Record = {} -export async function extractRouteRules (code: string): Promise { +export async function extractRouteRules (code: string, esbuildOptions?: TransformOptions): Promise { if (code in ruleCache) { return ruleCache[code] } @@ -23,7 +24,7 @@ export async function extractRouteRules (code: string): Promise { const shouldAugment = nuxt.options.experimental.scanPageMeta || nuxt.options.experimental.typedPages if (shouldAugment) { - const augmentedPages = await augmentPages(pages, nuxt.vfs) + const augmentedPages = await augmentPages(pages, nuxt.vfs, undefined, nuxt.options.esbuild.options) await nuxt.callHook('pages:extend', pages) - await augmentPages(pages, nuxt.vfs, augmentedPages) + await augmentPages(pages, nuxt.vfs, augmentedPages, nuxt.options.esbuild.options) augmentedPages.clear() } else { await nuxt.callHook('pages:extend', pages) @@ -140,16 +141,16 @@ export function generateRoutesFromFiles (files: ScannedFile[], options: Generate return prepareRoutes(routes) } -export async function augmentPages (routes: NuxtPage[], vfs: Record, augmentedPages = new Set()) { +export async function augmentPages (routes: NuxtPage[], vfs: Record, augmentedPages = new Set(), esbuildOptions?: TransformOptions) { for (const route of routes) { if (route.file && !augmentedPages.has(route.file)) { const fileContent = route.file in vfs ? vfs[route.file] : fs.readFileSync(await resolvePath(route.file), 'utf-8') - Object.assign(route, await getRouteMeta(fileContent, route.file)) + Object.assign(route, await getRouteMeta(fileContent, route.file, esbuildOptions)) augmentedPages.add(route.file) } if (route.children && route.children.length > 0) { - await augmentPages(route.children, vfs) + await augmentPages(route.children, vfs, augmentedPages, esbuildOptions) } } return augmentedPages @@ -174,7 +175,7 @@ const DYNAMIC_META_KEY = '__nuxt_dynamic_meta_key' as const const pageContentsCache: Record = {} const metaCache: Record>> = {} -export async function getRouteMeta (contents: string, absolutePath: string): Promise>> { +export async function getRouteMeta (contents: string, absolutePath: string, esbuildOptions?: TransformOptions): Promise>> { // set/update pageContentsCache, invalidate metaCache on cache mismatch if (!(absolutePath in pageContentsCache) || pageContentsCache[absolutePath] !== contents) { pageContentsCache[absolutePath] = contents @@ -194,7 +195,7 @@ export async function getRouteMeta (contents: string, absolutePath: string): Pro return {} } - const js = await transform(script.code, { loader: script.loader }) + const js = await transform(script.code, { loader: script.loader, ...esbuildOptions }) const ast = parse(js.code, { sourceType: 'module', ecmaVersion: 'latest',