Skip to content

Commit

Permalink
Add @parcel/macros package with macro context TS definitions (#9544)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Feb 27, 2024
1 parent ac43c39 commit 78fe2ce
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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"
}
}

0 comments on commit 78fe2ce

Please sign in to comment.