From 50f9fcdce13328730cf6f03d763704d9cb8f033a Mon Sep 17 00:00:00 2001 From: Harlan GitHub Agent Date: Fri, 20 Nov 2026 08:22:54 +1100 Subject: [PATCH] fix(build): return maps from dependency compatibility transforms --- package.json | 2 + pnpm-lock.yaml | 12 +++++ pnpm-workspace.yaml | 2 + scripts/lib/dependency-plugin-compat.ts | 29 +++++++---- test/unit/dependency-plugin-compat.test.ts | 56 +++++++++++++++------- 5 files changed, 75 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index cb661dc8..ac1ee3f8 100644 --- a/package.json +++ b/package.json @@ -106,6 +106,7 @@ "@harlan-zw/nuxt-cloudflare": "catalog:", "@harlan-zw/nuxt-dx": "catalog:", "@harlan-zw/nuxt-sentry": "catalog:", + "@jridgewell/trace-mapping": "catalog:", "@nuxt/a11y": "catalog:", "@nuxt/eslint": "catalog:", "@nuxt/kit": "catalog:", @@ -121,6 +122,7 @@ "eslint-plugin-harlanzw": "catalog:", "happy-dom": "catalog:", "lint-staged": "catalog:", + "magic-string": "catalog:", "nuxtseo-devtools-host": "catalog:", "site-typecheck": "workspace:*", "tsx": "catalog:", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 04dd9c71..feee829f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -212,6 +212,9 @@ catalogs: '@iconify-json/vscode-icons': specifier: ^1.2.76 version: 1.2.76 + '@jridgewell/trace-mapping': + specifier: 0.3.31 + version: 0.3.31 '@modelcontextprotocol/client': specifier: 2.0.0 version: 2.0.0 @@ -323,6 +326,9 @@ catalogs: lint-staged: specifier: ^17.4.1 version: 17.4.1 + magic-string: + specifier: 1.2.3 + version: 1.2.3 marked: specifier: ^18.0.11 version: 18.0.11 @@ -555,6 +561,9 @@ importers: '@harlan-zw/nuxt-sentry': specifier: 'catalog:' version: 0.3.0(b93fac6eb0b0b351150aa811e6a25c50) + '@jridgewell/trace-mapping': + specifier: 'catalog:' + version: 0.3.31 '@nuxt/a11y': specifier: 'catalog:' version: 1.0.0-alpha.1(@nuxt/schema@4.6.4)(confbox@0.3.1)(dotenv@17.4.2)(giget@3.3.1)(jiti@2.7.0)(magic-string@1.2.3)(mlly@1.8.2)(oxc-parser@0.143.0)(rolldown@1.2.18)(unplugin@3.3.0(esbuild@0.28.2)(rolldown@1.2.18)(rollup@4.62.4)(vite@8.3.7(@types/node@26.4.0)(esbuild@0.28.2)(jiti@2.7.0)(terser@5.50.0)(tsx@4.23.13)(yaml@2.9.1)))(vite@8.3.7(@types/node@26.4.0)(esbuild@0.28.2)(jiti@2.7.0)(terser@5.50.0)(tsx@4.23.13)(yaml@2.9.1)) @@ -600,6 +609,9 @@ importers: lint-staged: specifier: 'catalog:' version: 17.4.1 + magic-string: + specifier: 'catalog:' + version: 1.2.3 nuxtseo-devtools-host: specifier: 'catalog:' version: 1.0.2(425548db53b25e6d3b784d445012cc81) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 286cf99b..190491ef 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -290,6 +290,7 @@ catalog: '@iconify-json/lucide': ^1.2.128 '@iconify-json/simple-icons': ^1.2.94 '@iconify-json/vscode-icons': ^1.2.76 + '@jridgewell/trace-mapping': 0.3.31 '@modelcontextprotocol/client': 2.0.0 '@modelcontextprotocol/sdk': 1.32.0 '@modelcontextprotocol/server': 2.3.0 @@ -327,6 +328,7 @@ catalog: import-in-the-middle: ^3.4.0 jsonc-parser: 3.3.1 lint-staged: ^17.4.1 + magic-string: 1.2.3 marked: ^18.0.11 motion-v: ^2.5.1 nuxt: ^4.6.3 diff --git a/scripts/lib/dependency-plugin-compat.ts b/scripts/lib/dependency-plugin-compat.ts index aee406e9..e2cd1f5a 100644 --- a/scripts/lib/dependency-plugin-compat.ts +++ b/scripts/lib/dependency-plugin-compat.ts @@ -1,3 +1,5 @@ +import MagicString from 'magic-string' + const OG_IMAGE_PLUGINS = new Map([ ['og-image-canonical-urls.server.js', 'ogImageCanonicalUrls'], ['route-rule-og-image.server.js', 'routeRuleOgImage'], @@ -5,29 +7,38 @@ const OG_IMAGE_PLUGINS = new Map([ const NUXT_PAGE_PLUGIN = '/nuxt/dist/pages/runtime/plugins/check-if-page-unused.js' -export function transformDependencyPlugin(id: string, code: string): string | null { +export function transformDependencyPlugin(id: string, code: string) { const sourcePath = id.split('?')[0]!.replaceAll('\\', '/') const fileName = sourcePath.split('/').at(-1) const ogImageExport = fileName && sourcePath.includes('/nuxt-og-image/dist/runtime/app/plugins/') ? OG_IMAGE_PLUGINS.get(fileName) : undefined + const transformed = new MagicString(code) if (ogImageExport) { - return [ + transformed.overwrite(0, code.length, [ `import { defineNuxtPlugin } from '#app'`, `import { ${ogImageExport} } from '../utils/plugins.js'`, `export default defineNuxtPlugin(${ogImageExport})`, - ].join('\n') + ].join('\n')) + } + else if (sourcePath.endsWith(NUXT_PAGE_PLUGIN)) { + transformed.replace( + /export\s*\{\s*NESTED_PAGE_CONFIRMATION_DELAY,\s*plugin as default,\s*findUnrenderedNestedPage\s*\};?/, + 'export default plugin;\nexport { NESTED_PAGE_CONFIRMATION_DELAY, findUnrenderedNestedPage };', + ) + } + else { + return null } - if (!sourcePath.endsWith(NUXT_PAGE_PLUGIN)) + if (transformed.toString() === code) return null - const transformed = code.replace( - /export\s*\{\s*NESTED_PAGE_CONFIRMATION_DELAY,\s*plugin as default,\s*findUnrenderedNestedPage\s*\};?/, - 'export default plugin;\nexport { NESTED_PAGE_CONFIRMATION_DELAY, findUnrenderedNestedPage };', - ) - return transformed === code ? null : transformed + return { + code: transformed.toString(), + map: transformed.generateMap({ source: id, includeContent: true, hires: true }), + } } export function dependencyPluginCompat() { diff --git a/test/unit/dependency-plugin-compat.test.ts b/test/unit/dependency-plugin-compat.test.ts index eae90392..5a3ae984 100644 --- a/test/unit/dependency-plugin-compat.test.ts +++ b/test/unit/dependency-plugin-compat.test.ts @@ -1,31 +1,53 @@ +import { originalPositionFor, TraceMap } from '@jridgewell/trace-mapping' import { describe, expect, it } from 'vitest' -import { transformDependencyPlugin } from '../../scripts/lib/dependency-plugin-compat' +import { dependencyPluginCompat } from '../../scripts/lib/dependency-plugin-compat' + +function transform(id: string, code: string) { + const result = dependencyPluginCompat().transform(code, id) + if (!result || typeof result === 'string') + throw new Error('Expected transformed code with a source map') + return result +} describe('dependency plugin compatibility transform', () => { it.each([ ['og-image-canonical-urls.server.js', 'ogImageCanonicalUrls'], ['route-rule-og-image.server.js', 'routeRuleOgImage'], - ])('wraps the %s re-export for Nuxt analysis', (file, exportName) => { - const result = transformDependencyPlugin( - `/node_modules/nuxt-og-image/dist/runtime/app/plugins/${file}`, - `export { ${exportName} as default } from "../utils/plugins.js";`, - ) + ])('maps the %s wrapper to its original module', (file, exportName) => { + const id = `/node_modules/nuxt-og-image/dist/runtime/app/plugins/${file}?v=123` + const source = `export { ${exportName} as default } from "../utils/plugins.js";` + const result = transform(id, source) + const map = new TraceMap(result.map.toString()) - expect(result).toContain(`import { ${exportName} } from '../utils/plugins.js'`) - expect(result).toContain(`export default defineNuxtPlugin(${exportName})`) + expect(result.code).toContain(`import { ${exportName} } from '../utils/plugins.js'`) + expect(result.code).toContain(`export default defineNuxtPlugin(${exportName})`) + expect(originalPositionFor(map, { line: 3, column: 0 })).toMatchObject({ source: id, line: 1, column: 0 }) + expect(map.sourcesContent).toEqual([source]) }) - it('makes Nuxt page plugin default export explicit', () => { - const result = transformDependencyPlugin( - '/node_modules/nuxt/dist/pages/runtime/plugins/check-if-page-unused.js', - 'const plugin = defineNuxtPlugin({});\nexport { NESTED_PAGE_CONFIRMATION_DELAY, plugin as default, findUnrenderedNestedPage };', - ) + it('keeps original positions before and after the page export rewrite', () => { + const id = 'C:\\node_modules\\nuxt\\dist\\pages\\runtime\\plugins\\check-if-page-unused.js?vue' + const source = [ + 'const plugin = defineNuxtPlugin({});', + 'export { NESTED_PAGE_CONFIRMATION_DELAY, plugin as default, findUnrenderedNestedPage };', + 'console.log(plugin);', + ].join('\n') + const result = transform(id, source) + const map = new TraceMap(result.map.toString()) + const sourceId = id.replaceAll('\\', '/') - expect(result).toContain('export default plugin') - expect(result).not.toContain('plugin as default') + expect(result.code).toContain('export default plugin;') + expect(result.code).not.toContain('plugin as default') + expect(originalPositionFor(map, { line: 1, column: 6 })).toMatchObject({ source: sourceId, line: 1, column: 6 }) + expect(originalPositionFor(map, { line: 2, column: 0 })).toMatchObject({ source: sourceId, line: 2, column: 0 }) + expect(originalPositionFor(map, { line: 4, column: 12 })).toMatchObject({ source: sourceId, line: 3, column: 12 }) + expect(map.sourcesContent).toEqual([source]) }) - it('ignores unrelated modules', () => { - expect(transformDependencyPlugin('/app/plugins/analytics.ts', 'export default {}')).toBeNull() + it.each([ + ['/app/plugins/analytics.ts', 'export default {}'], + ['/node_modules/nuxt/dist/pages/runtime/plugins/check-if-page-unused.js', 'export default plugin;'], + ])('leaves unchanged modules alone: %s', (id, source) => { + expect(dependencyPluginCompat().transform(source, id)).toBeNull() }) })