Skip to content

Commit 0d5696d

Browse files
8ctaviodanielroe
authored andcommitted
fix(nuxt): ignore #components import mapping inside packages that use it internally (#33049)
1 parent 2cd3b09 commit 0d5696d

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

packages/nuxt/src/components/plugins/transform.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { isObject } from '@vue/shared'
12
import { isIgnored } from '@nuxt/kit'
23
import type { Import } from 'unimport'
34
import { createUnimport } from 'unimport'
45
import { createUnplugin } from 'unplugin'
56
import { parseURL } from 'ufo'
67
import { parseQuery } from 'vue-router'
7-
import { normalize } from 'pathe'
8+
import { isAbsolute, normalize } from 'pathe'
9+
import { readPackage } from 'pkg-types'
810
import { genImport } from 'knitwork'
911
import type { getComponentsT } from '../module'
1012
import type { Nuxt } from 'nuxt/schema'
@@ -112,6 +114,12 @@ export function TransformPlugin (nuxt: Nuxt, options: TransformPluginOptions) {
112114

113115
if (!code.includes('#components')) { return }
114116

117+
// If package defines a "#components" import mapping, assume is used internally by the package.
118+
const pkg = isAbsolute(id) && id.includes('node_modules') ? await readPackage(id, { try: true }) : undefined
119+
if (isObject(pkg) && isObject(pkg.imports) && Object.hasOwn(pkg.imports, '#components')) {
120+
return
121+
}
122+
115123
componentUnimport.modifyDynamicImports((imports) => {
116124
imports.length = 0
117125
imports.push(...getComponentsImports())

packages/nuxt/test/components-transform.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import { fileURLToPath } from 'node:url'
12
import { describe, expect, it } from 'vitest'
23
import type { Component, Nuxt } from '@nuxt/schema'
34
import { kebabCase } from 'scule'
4-
import { normalize } from 'pathe'
5+
import { join, normalize } from 'pathe'
56
import { findWorkspaceDir } from 'pkg-types'
67

78
import { TransformPlugin } from '../src/components/plugins/transform'
89

10+
const pkgPath = fileURLToPath(new URL('./node_modules/package-fixture', import.meta.url))
11+
const virtualFilePath = join(pkgPath, 'foo', 'bar', 'baz')
12+
913
describe('components:transform', () => {
1014
it('should transform #components imports', async () => {
1115
const transform = createTransformer([
@@ -22,6 +26,16 @@ describe('components:transform', () => {
2226
`)
2327
})
2428

29+
it('should ignore #components import mapping inside packages that use it internally', async () => {
30+
const transform = createTransformer([
31+
createComponent('Foo'),
32+
createComponent('Bar', { export: 'Bar' }),
33+
])
34+
35+
const code = await transform('import { Internal, Private } from \'#components\'', virtualFilePath)
36+
expect(code).toMatchInlineSnapshot(`undefined`)
37+
})
38+
2539
it('should correctly resolve server-only components', async () => {
2640
const transform = createTransformer([
2741
createComponent('Foo', { mode: 'server' }),

packages/nuxt/test/node_modules/package-fixture/package.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ packages:
33
- packages/**
44
- '!packages/nuxi'
55
- '!packages/test-utils'
6+
- '!packages/nuxt/test/package-fixture'
67
- playground
78
- test/fixtures/*
89

0 commit comments

Comments
 (0)