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 @parcel/macros package with macro context TS definitions #9544

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/transformers/js/src/JSTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ type MacroAsset = {|
content: string,
|};

// NOTE: Make sure this is in sync with the TypeScript definition in the @parcel/macros package.
type MacroContext = {|
addAsset(asset: MacroAsset): void,
invalidateOnFileChange(FilePath): void,
Expand Down
42 changes: 42 additions & 0 deletions packages/utils/macros/macros.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export interface MacroContext {
/** Adds an asset as a dependency of the JS module that called this macro. */
addAsset(asset: MacroAsset): void;
/** Invalidate the macro call whenever the given file changes. */
invalidateOnFileChange(filePath: string): void;
/** Invalidate the macro call when a file matching the given pattern is created. */
invalidateOnFileCreate(options: FileCreateInvalidation): void;
/** Invalidate the macro whenever the given environment variable changes. */
invalidateOnEnvChange(env: string): void;
/** Invalidate the macro whenever Parcel restarts. */
invalidateOnStartup(): void;
/** Invalidate the macro on every build. */
invalidateOnBuild(): void;
}

export interface MacroAsset {
/** The type of the asset (e.g. `'css'`). */
type: string;
/** The content of the asset. */
content: string;
}

export type FileCreateInvalidation =
| FileInvalidation
| GlobInvalidation
| FileAboveInvalidation;

/** Invalidate when a file matching a glob is created. */
export interface GlobInvalidation {
glob: string;
}

/** Invalidate when a specific file is created. */
export interface FileInvalidation {
filePath: string;
}

/** Invalidate when a file of a specific name is created above a certain directory in the hierarchy. */
export interface FileAboveInvalidation {
fileName: string;
aboveFilePath: string;
}
22 changes: 22 additions & 0 deletions packages/utils/macros/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@parcel/macros",
"version": "2.11.0",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/parcel"
},
"repository": {
"type": "git",
"url": "https://github.com/parcel-bundler/parcel.git"
},
"types": "macros.d.ts",
"sideEffects": false,
"engines": {
"node": ">= 12.0.0",
"parcel": "^2.11.0"
}
}
Loading