Skip to content

Commit 966c541

Browse files
authored
Merge pull request #1955 from embroider-build/no-template-only-compilation-stage-2
Remove template-only components compilation from Stage 2
2 parents b61ecde + e61adc4 commit 966c541

File tree

7 files changed

+286
-160
lines changed

7 files changed

+286
-160
lines changed

packages/compat/src/audit/babel-visitor.ts

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export function auditJS(rawSource: string, filename: string, babelConfig: Transf
7373
codeFrameIndex: saveCodeFrame(arg),
7474
specifiers: [],
7575
});
76+
} else if (arg.type === 'BinaryExpression') {
77+
// ignore binary expressions. Vite uses these (somehow) in the `@vite/client` import
7678
} else {
7779
problems.push({
7880
message: `audit tool is unable to understand this usage of ${

packages/compat/src/compat-app.ts

-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { isEmbroiderMacrosPlugin, MacrosConfig } from '@embroider/macros/src/nod
2222
import resolvePackagePath from 'resolve-package-path';
2323
import Concat from 'broccoli-concat';
2424
import mapKeys from 'lodash/mapKeys';
25-
import SynthesizeTemplateOnlyComponents from './synthesize-template-only-components';
2625
import { isEmberAutoImportDynamic, isInlinePrecompilePlugin } from './detect-babel-plugins';
2726
import loadAstPlugins from './prepare-htmlbars-ast-plugins';
2827
import { readFileSync } from 'fs';
@@ -693,9 +692,6 @@ export default class CompatApp {
693692

694693
let trees: BroccoliNode[] = [];
695694
trees.push(appTree);
696-
trees.push(
697-
new SynthesizeTemplateOnlyComponents(appTree, { allowedPaths: ['components'], templateExtensions: ['.hbs'] })
698-
);
699695

700696
if (testsTree) {
701697
trees.push(testsTree);

packages/core/src/app-files.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,23 @@ export class AppFiles {
8383
}
8484

8585
if (relativePath.startsWith('components/')) {
86-
// hbs files are resolvable, but not when they're used via co-location.
86+
// hbs files are not resolvable when they're used via co-location with
87+
// an associated js file because it's the js that is resolvable.
8788
// An hbs file is used via colocation when it's inside the components
8889
// directory, and also not named "template.hbs" (because that is an
8990
// older pattern used with pods-like layouts).
90-
if (!relativePath.endsWith('.hbs') || relativePath.endsWith('/template.hbs')) {
91+
let isHbs = relativePath.endsWith('.hbs');
92+
if (!isHbs || relativePath.endsWith('/template.hbs')) {
9193
components.push(relativePath);
94+
} else if (isHbs) {
95+
// template-only components will be compiled as js files during the
96+
// build process, so we push a virtual path to js in the list of files
97+
// because the resolver will be able to recognize a template-only and
98+
// give a correct answer in the end.
99+
let jsPath = relativePath.replace(/\.hbs$/, '.js');
100+
if (!combinedFiles.has(jsPath)) {
101+
components.push(jsPath);
102+
}
92103
}
93104
continue;
94105
}

packages/vite/src/hbs.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import { createFilter } from '@rollup/pluginutils';
44
import type { PluginContext, ResolvedId } from 'rollup';
55
import type { Plugin } from 'vite';
6-
import { hbsToJS, ResolverLoader } from '@embroider/core';
6+
import { hbsToJS, ResolverLoader, cleanUrl } from '@embroider/core';
77
import assertNever from 'assert-never';
8+
import { readFileSync } from 'fs-extra';
89
import { parse as pathParse } from 'path';
910
import makeDebug from 'debug';
1011

@@ -30,15 +31,15 @@ export function hbs(): Plugin {
3031
}
3132
},
3233

33-
transform(source: string, id: string) {
34+
load(id: string) {
3435
const meta = getMeta(this, id);
3536
if (!meta) {
3637
return;
3738
}
3839

3940
switch (meta.type) {
4041
case 'template':
41-
let code = hbsToJS(source);
42+
let code = hbsToJS(`${readFileSync(cleanUrl(id))}`);
4243
return {
4344
code,
4445
};

test-packages/support/audit-assertions.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import type { AuditBuildOptions, Finding, Module } from '../../packages/compat/s
22
import { httpAudit, type HTTPAuditOptions } from '../../packages/compat/src/http-audit';
33
import type { Import } from '../../packages/compat/src/module-visitor';
44
import { Audit } from '../../packages/compat/src/audit';
5-
import { explicitRelative } from '../../packages/shared-internals';
5+
import { cleanUrl, explicitRelative } from '../../packages/shared-internals';
66
import { install as installCodeEqualityAssertions } from 'code-equality-assertions/qunit';
77
import { posix } from 'path';
88
import { distance } from 'fastest-levenshtein';
99
import { sortBy } from 'lodash';
1010
import { getRewrittenLocation } from './rewritten-path';
11+
import { Memoize } from 'typescript-memoize';
1112

1213
export { Import };
1314

@@ -113,8 +114,14 @@ export class ExpectAuditResults {
113114
export class ExpectModule {
114115
constructor(private expectAudit: ExpectAuditResults, private inputName: string) {}
115116

117+
@Memoize()
116118
private get module() {
117119
let outputName = this.expectAudit.toRewrittenPath(this.inputName);
120+
for (let [key, value] of Object.entries(this.expectAudit.result.modules)) {
121+
if (cleanUrl(key) === outputName) {
122+
return value;
123+
}
124+
}
118125
return this.expectAudit.result.modules[outputName];
119126
}
120127

@@ -264,7 +271,15 @@ export class ExpectModule {
264271
this.emitMissingModule();
265272
return;
266273
}
267-
this.expectAudit.assert.deepEqual(this.module.consumedFrom, paths.map(this.expectAudit.toRewrittenPath));
274+
275+
let consumedFrom = this.module.consumedFrom.map(m => {
276+
if (typeof m === 'string') {
277+
return cleanUrl(m);
278+
}
279+
return m;
280+
});
281+
282+
this.expectAudit.assert.deepEqual(consumedFrom, paths.map(this.expectAudit.toRewrittenPath));
268283
}
269284
}
270285

0 commit comments

Comments
 (0)