Repository navigation
fix(build): return maps from dependency compatibility transforms #713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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 | ||
|
|
||
| const transformed = new MagicString(code) | ||
| if (ogImageExport) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
|
||
| 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() { | ||
|
|
||
| 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() | ||
| }) | ||
| }) |
There was a problem hiding this comment.
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.