From 1467bde3b4551aaab59bb4fe3ea0adc8ef33fb9f Mon Sep 17 00:00:00 2001 From: patrick Date: Wed, 28 Aug 2024 13:37:13 +0200 Subject: [PATCH] split out isInComponents --- packages/shared-internals/src/colocation.ts | 13 ++++--------- packages/vite/src/esbuild-resolver.ts | 6 +++--- packages/vite/src/hbs.ts | 5 +++-- 3 files changed, 10 insertions(+), 14 deletions(-) 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..008cd8fe6 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(result.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: {