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
6 changes: 6 additions & 0 deletions packages/rolldown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ module graph. Without it, the bundled CSS is emitted but nothing loads it.

`ember()` returns an array of rolldown plugins:

- **`emberConfig()`** applies the library-build defaults (sourcemaps,
`clean`, `dts`, `.js` / `.d.ts` extensions). It also turns off rolldown's
constant inlining (`optimization.inlineConst`). The rolldown default puts an
imported constant's literal into `precompileTemplate` scope objects, and the
consuming app's template compiler accepts only direct references there.
Anything you set explicitly still wins.
- **`emberIsolatedDeclarations()`** errors when the tsconfig the build uses
(tsdown's `tsconfig` option, defaulting to `tsconfig.json`) is present without
`isolatedDeclarations: true`.
Expand Down
3 changes: 3 additions & 0 deletions packages/rolldown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ interface Config {
* - `emberConfig()`
* applies sensible tsdown defaults for a library build
* (sourcemaps, `clean`, `dts`, `.js` / `.d.ts` extensions, quiet logging).
* It also turns off rolldown's constant inlining.
* Inlined literals break `precompileTemplate` scope objects,
* which must hold direct references.
* Anything you set explicitly still wins.
*
* - `emberIsolatedDeclarations()`
Expand Down
78 changes: 78 additions & 0 deletions packages/rolldown/src/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { rolldown, type InputOptions } from "rolldown";
import { describe, expect, it } from "vitest";

import { emberConfig } from "./config.ts";

/**
* A module pair shaped like babel's template-compilation output: an imported
* constant referenced from a `precompileTemplate` scope object. The constant's
* value is shorter than its name — the case rolldown's default constant
* inlining (`optimization.inlineConst`, `mode: "smart"`) substitutes, turning
* `scope: () => ({ EMPTY })` into `scope: () => ({ EMPTY: "—" })`. Scope
* entries must be direct references to in-scope values, so the consuming app's
* template compiler rejects the substituted form.
*/
async function scopeFixture(): Promise<string> {
const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-config-"));

await writeFile(path.join(dir, "constants.js"), `export const EMPTY = "—";\n`);
await writeFile(
path.join(dir, "entry.js"),
`import { setComponentTemplate } from "@ember/component";\n` +
`import templateOnly from "@ember/component/template-only";\n` +
`import { precompileTemplate } from "@ember/template-compilation";\n` +
`import { EMPTY } from "./constants.js";\n` +
`export const X = setComponentTemplate(precompileTemplate("{{EMPTY}}", {\n` +
` strictMode: true,\n` +
` scope: () => ({\n` +
` EMPTY\n` +
` })\n` +
`}), templateOnly());\n`,
);

return dir;
}

async function buildScopeFixture(options: Partial<InputOptions> = {}): Promise<string> {
const dir = await scopeFixture();
const build = await rolldown({
input: path.join(dir, "entry.js"),
external: /^@ember\//,
...options,
});
const { output } = await build.generate({});
return output[0].code;
}

describe("emberConfig", () => {
it("documents the rolldown default this guards against: short constants inline into scope objects", async () => {
// If this starts preserving the reference, rolldown changed its default
// and the guard below may no longer be needed.
const code = await buildScopeFixture();
expect(code).toContain(`scope: () => ({ EMPTY: "—" })`);
});

it("keeps scope-object constants as references", async () => {
const code = await buildScopeFixture({ plugins: [emberConfig()] });
expect(code).toContain(`scope: () => ({ EMPTY })`);
});

it("an explicit optimization.inlineConst still wins", async () => {
const code = await buildScopeFixture({
plugins: [emberConfig()],
optimization: { inlineConst: { mode: "smart" } },
});
expect(code).toContain(`scope: () => ({ EMPTY: "—" })`);
});

it("leaves other explicit optimization options alone while defaulting inlineConst off", async () => {
const code = await buildScopeFixture({
plugins: [emberConfig()],
optimization: { pifeForModuleWrappers: false },
});
expect(code).toContain(`scope: () => ({ EMPTY })`);
});
});
19 changes: 19 additions & 0 deletions packages/rolldown/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ import type { TsdownPlugin, UserConfig } from "tsdown";
*
* - `output.sourcemap` — on
*
* One default applies through rolldown's `options` hook,
* so it holds under both tools:
*
* - `optimization.inlineConst` — off.
* Rolldown's default (`mode: "smart"`) puts an imported constant's literal
* into `precompileTemplate` scope objects:
* `scope: () => ({ EMPTY })` becomes `scope: () => ({ EMPTY: "—" })`.
* The consuming app's template compiler accepts only direct references there.
* The library still builds, type-checks, and lints clean,
* so the app boot is the first place the error shows.
* The compiler-side fix, which accepts literal scope values, is
* https://github.com/emberjs/babel-plugin-ember-template-compilation/pull/125
*
* Externals are handled by `emberExternals()` (a `resolveId` hook) in both cases.
*
* Bundle mode flips the dependency defaults.
Expand Down Expand Up @@ -66,6 +79,12 @@ export function emberConfig({ bundle = false }: { bundle?: boolean } = {}): Tsdo
config.deps.neverBundle ??= ["node:*", "@ember/*", "@glimmer/*"];
},

options(options) {
options.optimization ??= {};
options.optimization.inlineConst ??= false;
return options;
},

outputOptions(options) {
options.sourcemap ??= true;
return options;
Expand Down
Loading