Skip to content

Commit

Permalink
Merge pull request #243 from soxhub/hit-the-fs-3-less-time-per-template
Browse files Browse the repository at this point in the history
Hit the file system 3 less times per template-using file (hbs, gjs, gts)
  • Loading branch information
NullVoxPopuli authored Jun 14, 2024
2 parents 7a5f4c9 + 360067f commit 9d12eff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
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

0 comments on commit 9d12eff

Please sign in to comment.