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

vite: fix hbs loading for virtual pair components #1813

Merged
merged 8 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
27 changes: 21 additions & 6 deletions packages/vite/src/hbs.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// TODO: I copied this from @embroider/addon-dev, it needs to be its own package
// (or be in shared-internals or core)
import { createFilter } from '@rollup/pluginutils';
import type { PluginContext, ResolvedId } from 'rollup';
import type { Plugin } from 'vite';
import { hbsToJS } from '@embroider/core';
import { hbsToJS, ResolverLoader } from '@embroider/core';
import assertNever from 'assert-never';
import { parse as pathParse } from 'path';
import makeDebug from 'debug';

const debug = makeDebug('embroider:hbs-plugin');
const resolverLoader = new ResolverLoader(process.cwd());

export function hbs(): Plugin {
return {
Expand Down Expand Up @@ -99,6 +98,24 @@ async function maybeSynthesizeComponentJS(context: PluginContext, source: string
if (!templateResolution) {
return null;
}

const resolvedId = templateResolution.id.split('?')[0];
templateResolution.id = resolvedId;

const pkg = resolverLoader.resolver.packageCache.ownerOfFile(resolvedId);
const isInComponents = pkg?.isV2App() && resolvedId.slice(pkg?.root.length).startsWith('/components');
Copy link
Contributor Author

@patricklx patricklx Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will always be false for addons, which is good, because they should not do template colocation / template-only.
It's already done in previous stage


if (resolvedId.endsWith('/template.hbs' || !isInComponents)) {
return {
...templateResolution,
meta: {
'rollup-hbs-plugin': {
type: 'template',
},
},
};
}

debug(`emitting template only component: %s`, templateResolution.id);

// we're trying to resolve a JS module but only the corresponding HBS
Expand All @@ -113,10 +130,8 @@ async function maybeSynthesizeComponentJS(context: PluginContext, source: string
};
}

const hbsFilter = createFilter('**/*.hbs?(\\?)*');

function maybeRewriteHBS(resolution: ResolvedId) {
if (!hbsFilter(resolution.id)) {
if (!resolution.id.split('?')[0].endsWith('.hbs')) {
return null;
}
debug('emitting hbs rewrite: %s', resolution.id);
Expand Down
5 changes: 5 additions & 0 deletions tests/vite-app/app/components/old/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Component from '@glimmer/component';

export default class extends Component {
message = 'hi';
}
1 change: 1 addition & 0 deletions tests/vite-app/app/components/old/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>hey {{@message}} <Fancy /></div>
1 change: 1 addition & 0 deletions tests/vite-app/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{{page-title "ViteApp"}}

<Example @message={{this.model.message}} />
<Old @message={{this.model.message}} />

<WelcomePage />
{{! Feel free to remove this! }}
Expand Down
Loading