Skip to content

Commit

Permalink
Hit the file system 3 less times per template-using file (hbs, gjs, gts)
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jun 14, 2024
1 parent f13d3a8 commit d8eaddf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
34 changes: 3 additions & 31 deletions ember-scoped-css/src/lib/path/template-transform-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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 +57,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 +72,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 +85,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;
}
13 changes: 13 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,19 @@ 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

0 comments on commit d8eaddf

Please sign in to comment.