diff --git a/packages/shared-internals/src/colocation.ts b/packages/shared-internals/src/colocation.ts index 84c04167f..7ab261827 100644 --- a/packages/shared-internals/src/colocation.ts +++ b/packages/shared-internals/src/colocation.ts @@ -14,18 +14,13 @@ export function syntheticJStoHBS(source: string): string | null { return null; } -export function needsSyntheticComponentJS( - requestedSpecifier: string, - foundFile: string, - packageCache?: Pick -): string | null { +export function needsSyntheticComponentJS(requestedSpecifier: string, foundFile: string): string | null { requestedSpecifier = cleanUrl(requestedSpecifier); foundFile = cleanUrl(foundFile); if ( discoveredImplicitHBS(requestedSpecifier, foundFile) && !foundFile.split(sep).join('/').endsWith('/template.hbs') && - !correspondingJSExists(foundFile) && - isInComponents(foundFile, packageCache) + !correspondingJSExists(foundFile) ) { return foundFile.slice(0, -3) + 'js'; } @@ -40,8 +35,8 @@ function correspondingJSExists(id: string): boolean { return ['js', 'ts'].some(ext => existsSync(id.slice(0, -3) + ext)); } -function isInComponents(id: string, packageCache?: Pick) { - if (!packageCache) return true; +export function isInComponents(url: string, packageCache: Pick) { + const id = cleanUrl(url); const pkg = packageCache.ownerOfFile(id); return pkg?.isV2App() && id.slice(pkg?.root.length).split(sep).join('/').startsWith('/components'); } diff --git a/packages/vite/src/esbuild-resolver.ts b/packages/vite/src/esbuild-resolver.ts index 9255500fc..1d35c3407 100644 --- a/packages/vite/src/esbuild-resolver.ts +++ b/packages/vite/src/esbuild-resolver.ts @@ -1,6 +1,6 @@ import type { Plugin as EsBuildPlugin, OnLoadResult } from 'esbuild'; import { transform } from '@babel/core'; -import { ResolverLoader, virtualContent, needsSyntheticComponentJS } from '@embroider/core'; +import { ResolverLoader, virtualContent, needsSyntheticComponentJS, isInComponents } from '@embroider/core'; import { readFileSync } from 'fs-extra'; import { EsBuildModuleRequest } from './esbuild-request'; import assertNever from 'assert-never'; @@ -80,8 +80,8 @@ export function esBuildResolver(): EsBuildPlugin { }); if (result.errors.length === 0 && !result.external) { - let syntheticPath = needsSyntheticComponentJS(path, result.path, resolverLoader.resolver.packageCache); - if (syntheticPath) { + let syntheticPath = needsSyntheticComponentJS(path, result.path); + if (syntheticPath && isInComponents(path, resolverLoader.resolver.packageCache)) { return { path: syntheticPath, namespace: 'embroider-template-only-component' }; } } diff --git a/packages/vite/src/hbs.ts b/packages/vite/src/hbs.ts index 20bb9791c..2b3acf784 100644 --- a/packages/vite/src/hbs.ts +++ b/packages/vite/src/hbs.ts @@ -5,6 +5,7 @@ import { hbsToJS, ResolverLoader, needsSyntheticComponentJS, + isInComponents, templateOnlyComponentSource, syntheticJStoHBS, } from '@embroider/core'; @@ -58,8 +59,8 @@ export function hbs(): Plugin { } } - let syntheticId = needsSyntheticComponentJS(source, resolution.id, resolverLoader.resolver.packageCache); - if (syntheticId) { + let syntheticId = needsSyntheticComponentJS(source, resolution.id); + if (syntheticId && isInComponents(resolution.id, resolverLoader.resolver.packageCache)) { return { id: syntheticId, meta: {