Skip to content
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
7 changes: 7 additions & 0 deletions .chronus/changes/feature-babel-preset-types-2026-7-21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@alloy-js/babel-preset"
---

`@alloy-js/babel-preset` now ships with TypeScript type declarations, so consumers can import the preset without `@ts-expect-error`.
7 changes: 7 additions & 0 deletions .chronus/changes/feature-rollup-plugin-api-2026-7-21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@alloy-js/rollup-plugin"
---

The Rollup/Vite plugin now accepts `addSourceInfo` and `sourceMaps` options and automatically configures esbuild for Alloy (`jsx: "preserve"` plus the `source` resolve conditions). Consumers no longer need to set these by hand in their Vite/Vitest config.
17 changes: 17 additions & 0 deletions .chronus/changes/internal-share-babel-config-2026-7-21.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
changeKind: internal
packages:
- "@alloy-js/cli"
- "@alloy-js/core"
- "@alloy-js/csharp"
- "@alloy-js/go"
- "@alloy-js/java"
- "@alloy-js/json"
- "@alloy-js/markdown"
- "@alloy-js/msbuild"
- "@alloy-js/python"
- "@alloy-js/typescript"
- "@alloy-js/typespec"
---

Rely on the Rollup/Vite plugin to configure esbuild, removing the duplicated esbuild/resolve boilerplate from each package's `vitest.config.ts`.
38 changes: 0 additions & 38 deletions packages/babel-preset-alloy/index.js

This file was deleted.

45 changes: 38 additions & 7 deletions packages/babel-preset-alloy/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
{
"name": "@alloy-js/babel-preset",
"version": "0.3.0",
"description": "",
"type": "module",
"description": "Babel preset for Alloy's JSX/TSX transformation",
"homepage": "https://github.com/alloy-framework/alloy",
"license": "MIT",
"author": "Microsoft",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"default": "./dist/index.js"
}
},
"publishConfig": {
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/alloy-framework/alloy.git"
},
"main": "index.js",
"engines": {
"node": ">=22.0.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"watch": "tsc -p ./tsconfig.build.json --watch",
"build": "tsc -p ./tsconfig.build.json",
"clean": "rimraf dist/ .temp/"
},
"dependencies": {
"@alloy-js/babel-plugin": "workspace:~",
"@alloy-js/babel-plugin-jsx-dom-expressions": "workspace:~"
},
"keywords": [],
"author": "",
"license": "MIT",
"type": "module"
"devDependencies": {
"@babel/core": "catalog:",
"@types/node": "catalog:",
"rimraf": "catalog:",
"typescript": "catalog:"
},
"bugs": "https://github.com/alloy-framework/alloy/issues"
}
73 changes: 73 additions & 0 deletions packages/babel-preset-alloy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import alloyTransform from "@alloy-js/babel-plugin";
import jsxTransform from "@alloy-js/babel-plugin-jsx-dom-expressions";
import type { PluginItem } from "@babel/core";

/** Options accepted by the Alloy Babel preset. */
export interface AlloyPresetOptions {
/** Module name providing the Alloy runtime. @default "@alloy-js/core" */
alloyModuleName?: string;
/** Module name providing the JSX runtime. @default "@alloy-js/core/jsx-runtime" */
moduleName?: string;
/** Code generation mode passed to the JSX transform. @default "universal" */
generate?: string;
/** Whether to wrap conditional expressions. @default true */
wrapConditionals?: boolean;
/** Preserve JSX whitespace. @default true */
preserveWhitespace?: boolean;
/** Opt into the legacy whitespace handling (not recommended). */
legacyWhitespace?: boolean;
/**
* Emit source location information for components. When omitted, this is
* inferred from `dev` or from `BABEL_ENV`/`NODE_ENV`.
*/
addSourceInfo?: boolean;
/** Whether this is a development build. Used to infer `addSourceInfo`. */
dev?: boolean;
}

/**
* Alloy Babel preset. Transforms Alloy's JSX syntax into calls against the
* Alloy runtime.
*/
export default function alloyPreset(
_context: unknown,
options: AlloyPresetOptions = {},
): { plugins: PluginItem[] } {
const envMode = process.env.BABEL_ENV ?? process.env.NODE_ENV;
const inferredDev = envMode === undefined ? true : envMode !== "production";
const defaultOptions = {
alloyModuleName: "@alloy-js/core",
moduleName: "@alloy-js/core/jsx-runtime",
generate: "universal",
wrapConditionals: true,
preserveWhitespace: true,
};

const jsxOptions = {
...defaultOptions,
...options,
} as Record<string, unknown>;
if (options.addSourceInfo === undefined) {
if (options.dev !== undefined) {
jsxOptions.addSourceInfo = options.dev;
} else {
jsxOptions.addSourceInfo = inferredDev;
}
}

// `@babel/core`'s `PluginItem` type does not model the Alloy plugins' custom
// option objects, so assert comparability here.
const plugins = [
[
alloyTransform,
{
alloyModuleName:
options.alloyModuleName ?? defaultOptions.alloyModuleName,
legacyWhitespace: options.legacyWhitespace,
},
],
[jsxTransform, jsxOptions],
] as PluginItem[];

return { plugins };
}
7 changes: 7 additions & 0 deletions packages/babel-preset-alloy/src/vendored-plugins.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// `@alloy-js/babel-plugin-jsx-dom-expressions` ships as plain JS without type
// declarations, so provide a minimal ambient declaration for it here.
declare module "@alloy-js/babel-plugin-jsx-dom-expressions" {
import type { PluginObj } from "@babel/core";
const plugin: (...args: unknown[]) => PluginObj;
export default plugin;
}
10 changes: 10 additions & 0 deletions packages/babel-preset-alloy/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist"
},
"references": [],
"include": ["src"],
"exclude": ["**/*.test.*", "test/**/*"]
}
9 changes: 9 additions & 0 deletions packages/babel-preset-alloy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"declaration": true,
"outDir": "dist"
},
"include": ["src/**/*.ts", "src/**/*.d.ts"],
"exclude": ["node_modules", "dist"]
}
2 changes: 0 additions & 2 deletions packages/cli/src/babel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { mkdir, writeFile } from "node:fs/promises";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
import alloyPreset from "@alloy-js/babel-preset";
import * as babel from "@babel/core";
import typescriptPreset from "@babel/preset-typescript";
Expand Down
1 change: 0 additions & 1 deletion packages/core/test/babel-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* intrinsics returning AlloyNodes eagerly).
*/

// @ts-expect-error — preset has no types
import alloyPreset from "@alloy-js/babel-preset";
import { transformSync } from "@babel/core";
import typescriptPreset from "@babel/preset-typescript";
Expand Down
1 change: 0 additions & 1 deletion packages/core/test/output-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* - `For` works under the runtime emitting multiple files.
*/

// @ts-expect-error — preset has no types
import alloyPreset from "@alloy-js/babel-preset";
import { transformSync } from "@babel/core";
import typescriptPreset from "@babel/preset-typescript";
Expand Down
12 changes: 0 additions & 12 deletions packages/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import alloyPlugin from "@alloy-js/rollup-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
conditions: ["source"],
},
ssr: {
resolve: {
conditions: ["source"],
},
},
esbuild: {
jsx: "preserve",
sourcemap: "both",
},
test: {
exclude: ["**/dist/**", "**/node_modules/**"],
setupFiles: ["./test/vitest.setup.ts"],
Expand Down
12 changes: 0 additions & 12 deletions packages/csharp/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import alloyPlugin from "@alloy-js/rollup-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
conditions: ["source"],
},
ssr: {
resolve: {
conditions: ["source"],
},
},
esbuild: {
jsx: "preserve",
sourcemap: "both",
},
test: {
exclude: ["**/dist/**", "**/node_modules/**"],
setupFiles: ["./test/vitest.setup.ts"],
Expand Down
12 changes: 0 additions & 12 deletions packages/go/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import alloyPlugin from "@alloy-js/rollup-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
conditions: ["source"],
},
ssr: {
resolve: {
conditions: ["source"],
},
},
esbuild: {
jsx: "preserve",
sourcemap: "both",
},
test: {
exclude: ["**/dist/**", "**/node_modules/**"],
setupFiles: ["./test/vitest.setup.ts"],
Expand Down
12 changes: 0 additions & 12 deletions packages/java/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import alloyPlugin from "@alloy-js/rollup-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
conditions: ["source"],
},
ssr: {
resolve: {
conditions: ["source"],
},
},
esbuild: {
jsx: "preserve",
sourcemap: "both",
},
test: {
exclude: ["**/dist/**", "**/node_modules/**"],
setupFiles: ["./test/vitest.setup.ts"],
Expand Down
12 changes: 0 additions & 12 deletions packages/json/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import alloyPlugin from "@alloy-js/rollup-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
conditions: ["source"],
},
ssr: {
resolve: {
conditions: ["source"],
},
},
esbuild: {
jsx: "preserve",
sourcemap: "both",
},
test: {
exclude: ["**/dist/**", "**/node_modules/**"],
setupFiles: ["./test/vitest.setup.ts"],
Expand Down
12 changes: 0 additions & 12 deletions packages/markdown/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import alloyPlugin from "@alloy-js/rollup-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
conditions: ["source"],
},
ssr: {
resolve: {
conditions: ["source"],
},
},
esbuild: {
jsx: "preserve",
sourcemap: "both",
},
test: {
exclude: ["**/dist/**", "**/node_modules/**"],
setupFiles: ["./test/vitest.setup.ts"],
Expand Down
12 changes: 0 additions & 12 deletions packages/msbuild/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ import alloyPlugin from "@alloy-js/rollup-plugin";
import { defineConfig } from "vitest/config";

export default defineConfig({
resolve: {
conditions: ["source"],
},
ssr: {
resolve: {
conditions: ["source"],
},
},
esbuild: {
jsx: "preserve",
sourcemap: "both",
},
test: {
exclude: ["**/dist/**", "**/node_modules/**"],
setupFiles: ["./test/vitest.setup.ts"],
Expand Down
Loading
Loading