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

Add glint helper types for more macros #1354

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions packages/macros/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ if (macroCondition(isDevelopingApp()) {

Note that these can be used in combination - e.g. if you run tests in the production environment, `isTesting()` will be true, but `isDevelopingApp()` will be false.

## Glint usage
If you are using [Glint](https://typed-ember.gitbook.io/glint/) and `environment-ember-loose`, you can add all the macros to your app at once by adding

```ts
import type { EmbroiderMacrosRegistry } from "@embroider/macros/src/template-registry";
```
to your app's e.g. `types/glint.d.ts` file, and making sure your registry extends from EmbroiderMacrosRegistry:

```ts
declare module '@glint/environment-ember-loose/registry' {
export default interface Registry
extends EmbroiderMacrosRegistry {
// ...
}
}
```

## Real world examples

Below are a list of addons that have started using `@embroider/macros` so that you can get a feel for common use cases that can be solved via the macro system.
Expand Down
4 changes: 4 additions & 0 deletions packages/macros/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export function failBuild(message: string): void {
throw new Oops(message);
}

export function maybeAttrs<T>(predicate: boolean, ...bareAttrs: unknown[]): T {
vlascik marked this conversation as resolved.
Show resolved Hide resolved
throw new Oops(predicate, bareAttrs);
}

export function moduleExists(packageName: string): boolean {
throw new Oops(packageName);
}
Expand Down
43 changes: 43 additions & 0 deletions packages/macros/src/template-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Helper from '@ember/component/helper';
import { dependencySatisfies, failBuild, getConfig, getOwnConfig, maybeAttrs } from './index';
import { EnsureSafeComponentHelper } from '@embroider/util';

export class MacroGetConfigHelper extends Helper<{
Args: { Positional: [packageName: string, ...keys: string[]] };
Return: ReturnType<typeof getConfig>;
}> {}

export class MacroGetOwnConfigHelper extends Helper<{
Args: { Positional: [...keys: string[]] };
Return: ReturnType<typeof getOwnConfig>;
}> {}

export class MacroConditionHelper extends Helper<{
Args: { Positional: [predicate: unknown] };
Return: boolean;
}> {}

export class MacroDependencySatisfiesHelper extends Helper<{
Args: { Positional: Parameters<typeof dependencySatisfies> };
Return: ReturnType<typeof dependencySatisfies>;
}> {}

export class MacroMaybeAttrsHelper extends Helper<{
Args: { Positional: Parameters<typeof maybeAttrs> };
Return: ReturnType<typeof maybeAttrs>;
}> {}

export class MacroFailBuildHelper extends Helper<{
Args: { Positional: Parameters<typeof failBuild> };
Return: ReturnType<typeof failBuild>;
}> {}

export interface EmbroiderMacrosRegistry {
'ensure-safe-component': typeof EnsureSafeComponentHelper;
macroGetOwnConfig: typeof MacroGetOwnConfigHelper;
macroGetConfig: typeof MacroGetConfigHelper;
macroCondition: typeof MacroConditionHelper;
macroDependencySatisfies: typeof MacroDependencySatisfiesHelper;
macroMaybeAttrs: typeof MacroMaybeAttrsHelper;
macroFailBuild: typeof MacroFailBuildHelper;
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16130,11 +16130,6 @@ webpack-sources@^3.2.3:
resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-3.2.3.tgz#2d4daab8451fd4b240cc27055ff6a0c2ccea0cde"
integrity sha512-/DyMEOrDgLKKIG0fmvtz+4dUX/3Ghozwgm6iPp8KRhvn+eQf9+Q7GWxVNMk3+uCPWfdXYC4ExGBckIXdFEfH1w==

webpack-virtual-modules@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/webpack-virtual-modules/-/webpack-virtual-modules-0.5.0.tgz#362f14738a56dae107937ab98ea7062e8bdd3b6c"
integrity sha512-kyDivFZ7ZM0BVOUteVbDFhlRt7Ah/CSPwJdi8hBpkK7QLumUqdLtVfm/PX/hkcnrvr0i77fO5+TjZ94Pe+C9iw==

webpack@^5, webpack@^5.38.1, webpack@^5.72.1, webpack@^5.74.0:
version "5.75.0"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.75.0.tgz#1e440468647b2505860e94c9ff3e44d5b582c152"
Expand Down