Skip to content

Commit

Permalink
split out isInComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Aug 28, 2024
1 parent 6f99817 commit 1467bde
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
13 changes: 4 additions & 9 deletions packages/shared-internals/src/colocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ export function syntheticJStoHBS(source: string): string | null {
return null;
}

export function needsSyntheticComponentJS(
requestedSpecifier: string,
foundFile: string,
packageCache?: Pick<PackageCache, 'ownerOfFile'>
): 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';
}
Expand All @@ -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<PackageCache, 'ownerOfFile'>) {
if (!packageCache) return true;
export function isInComponents(url: string, packageCache: Pick<PackageCache, 'ownerOfFile'>) {
const id = cleanUrl(url);
const pkg = packageCache.ownerOfFile(id);
return pkg?.isV2App() && id.slice(pkg?.root.length).split(sep).join('/').startsWith('/components');
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/esbuild-resolver.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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' };
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/hbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
hbsToJS,
ResolverLoader,
needsSyntheticComponentJS,
isInComponents,
templateOnlyComponentSource,
syntheticJStoHBS,
} from '@embroider/core';
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 1467bde

Please sign in to comment.