Skip to content
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

Hit the file system 3 less times per template-using file (hbs, gjs, gts) #243

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 7 additions & 34 deletions ember-scoped-css/src/lib/path/template-transform-paths.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import path from 'node:path';

import { existsSync } from 'fs';

import { findWorkspacePath, withoutExtension } from './utils';
import { findWorkspacePath } from './utils';

/**
* template plugins do not hand us the correct file path.
Expand Down Expand Up @@ -36,7 +34,10 @@ export function fixFilename(filename) {
* - the filename looks like an absolute path, but swapped out the 'app' part of the path
* with the module name, so the file paths never exist on disk
*/
if (!fileName.includes('/app/')) {
if (
!fileName.includes('/app/') &&
!fileName.includes('/node_modules/.embroider/')
) {
let maybeModule = fileName.replace(workspace, '');
let [maybeScope, ...rest] = maybeModule.split('/').filter(Boolean);
let parts = rest;
Expand All @@ -57,11 +58,7 @@ export function fixFilename(filename) {
*/
let candidatePath = path.join(workspace, 'app', relative);

let resolved = findCandidate(candidatePath);

if (resolved) {
return resolved;
}
return candidatePath;
}

/**
Expand All @@ -76,11 +73,7 @@ export function fixFilename(filename) {
'/app/',
);

let resolved = findCandidate(candidatePath);

if (resolved) {
return resolved;
}
return candidatePath;
}

// TODO: why are we passed files to other projects?
Expand All @@ -93,23 +86,3 @@ export function fixFilename(filename) {
// unhandled scenarios with the file names in the plugin infra
return fileName;
}

const COMPILES_TO_JS = ['.hbs', '.gjs', '.gts'];

function findCandidate(filePath) {
if (existsSync(filePath)) {
return filePath;
}

let withoutExt = withoutExtension(filePath);

for (let ext of COMPILES_TO_JS) {
let candidatePath = withoutExt + ext;

if (existsSync(candidatePath)) {
return candidatePath;
}
}

return null;
}
16 changes: 16 additions & 0 deletions ember-scoped-css/src/lib/path/template-transform-paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ describe('fixFilename()', () => {
);
});

it('is not confused with "app" in the embroider rewritten location', () => {
let file = path.join(
paths.embroiderApp,
paths.rewritten,
'components/app/page/template-only.hbs',
);
let corrected = fixFilename(file);

expect(corrected).to.equal(
path.join(
paths.embroiderApp,
'app/components/app/page/template-only.hbs',
),
);
});

it('works with classic paths (w/ module name)', () => {
let file = path.join(
paths.classicApp,
Expand Down
Loading