Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:",
Expand All @@ -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:",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 20 additions & 9 deletions scripts/lib/dependency-plugin-compat.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
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'],
])

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logged (18/100): The Vite hook allocates source-map editing state for modules it leaves unchanged.

Proof: Nuxt registers this as an unfiltered Vite transform. Each unrelated module now allocates a MagicString instance, a chunk, two maps, and a bit set before returning null. The base performed only the path checks for that case.

Optional follow-up: As an optional follow-up, return null for unmatched module IDs before constructing MagicString.

  • Ask an agent to verify and fix this finding

Harlan Agent Kit posted this automated review. It is not Harlan's personal review or approval. AI open source policy.

const transformed = new MagicString(code)
if (ogImageExport) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logged (50/100): An empty OG image plugin module now aborts the build transform.

Proof: For either recognized OG image module ID, empty source reaches overwrite(0, 0, wrapper). The pinned MagicString 1.2.3 implementation delegates overwrite to update, which throws when start equals end. The base returned the same valid wrapper regardless of the input source, so an empty input now aborts the transform.

Optional follow-up: As an optional follow-up, use an insertion for empty source and keep the existing overwrite for nonempty source.

  • Ask an agent to verify and fix this finding

Harlan Agent Kit posted this automated review. It is not Harlan's personal review or approval. AI open source policy.

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() {
Expand Down
56 changes: 39 additions & 17 deletions test/unit/dependency-plugin-compat.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
Loading