@nullvoxpopuli/ember-rolldown 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -81,7 +81,7 @@ own and is only reachable through the entrypoints that re-export it.
81
81
 
82
82
  Declarations are emitted with isolated declarations — the only declaration
83
83
  pipeline that can see `<template>` (`.gts`/`.gjs`) modules, which exist only
84
- inside the bundler's module graph. Your `tsconfig.json` must enable it
84
+ inside the bundler's module graph. The tsconfig your build uses must enable it
85
85
  (`ember()` errors otherwise):
86
86
 
87
87
  ```jsonc
@@ -101,6 +101,33 @@ import type { TOC } from "@ember/component/template-only";
101
101
  export const Badge: TOC<BadgeSignature> = <template>...</template>;
102
102
  ```
103
103
 
104
+ #### Keeping the constraint off dev-only code
105
+
106
+ `isolatedDeclarations` is a constraint on how _published_ code is written, so it
107
+ should cover only the code you emit declarations for. If your package also holds
108
+ dev-only code — a demo app, in-package tests — putting the flag on the single
109
+ `tsconfig.json` that covers everything would force explicit annotations on demo
110
+ components and test helpers that never get a `.d.ts` emitted for them.
111
+
112
+ The tsconfig `ember()` checks is the one
113
+ [tsdown's `tsconfig` option](https://tsdown.dev/options/tsconfig) points at, so
114
+ point the build at a publish-only config and leave `tsconfig.json` alone for
115
+ editors and `tsc --noEmit`:
116
+
117
+ ```js
118
+ // tsdown.config.js
119
+ export default defineConfig({
120
+ entry: ["./src/index.ts"],
121
+ // include: ["src/**/*"], isolatedDeclarations: true
122
+ tsconfig: "./tsconfig.publish.json",
123
+ plugins: [ember()],
124
+ });
125
+ ```
126
+
127
+ The tradeoff: isolated-declaration errors in `src` then surface when you build
128
+ rather than in your editor, since the editor uses `tsconfig.json`. Run the build
129
+ (or `tsc --noEmit -p tsconfig.publish.json`) in CI so nothing lands unchecked.
130
+
104
131
  ## CSS
105
132
 
106
133
  Components that import co-located CSS (`import './popup.css'`) need
@@ -159,8 +186,9 @@ it.
159
186
 
160
187
  `ember()` returns an array of rolldown plugins:
161
188
 
162
- - **`emberIsolatedDeclarations()`** — errors when a `tsconfig.json` is present
163
- without `isolatedDeclarations: true`.
189
+ - **`emberIsolatedDeclarations()`** — errors when the tsconfig the build uses
190
+ (tsdown's `tsconfig` option, defaulting to `tsconfig.json`) is present without
191
+ `isolatedDeclarations: true`.
164
192
  - **`emberExternals()`** — keeps your `dependencies`, `peerDependencies`, and
165
193
  the ember virtual packages (e.g. `@ember/component`, `@glimmer/tracking`, the
166
194
  template compiler) external, so the consuming app resolves them.
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import transformTypeScript from "@babel/plugin-transform-typescript";
3
3
  import templateCompilation from "babel-plugin-ember-template-compilation";
4
4
  import decoratorTransforms from "decorator-transforms";
5
5
  import { loadPartialConfigSync } from "@babel/core";
6
- import { existsSync, readFileSync, realpathSync } from "node:fs";
6
+ import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
7
7
  import { createRequire } from "node:module";
8
8
  import path, { dirname, resolve } from "node:path";
9
9
  import pkg from "@embroider/core";
@@ -220,7 +220,28 @@ function emberExternals() {
220
220
  //#endregion
221
221
  //#region src/isolated-declarations.ts
222
222
  /**
223
- * Errors the build when a `tsconfig.json` is present without
223
+ * Resolve the tsconfig the build will actually use, mirroring tsdown's own
224
+ * `tsconfig` option (`string | boolean`):
225
+ *
226
+ * - `undefined` / `true`: `tsconfig.json` in the cwd.
227
+ * - a path to a file: that file.
228
+ * - a path to a directory: `tsconfig.json` inside it.
229
+ * - `false`: no tsconfig at all.
230
+ *
231
+ * Under plain rolldown there is no such option, so the cwd's `tsconfig.json`
232
+ * is all there is.
233
+ */
234
+ function resolveTsconfigPath(option) {
235
+ if (option === false) return void 0;
236
+ if (typeof option === "string") {
237
+ const asPath = resolve(option);
238
+ if (existsSync(asPath) && statSync(asPath).isDirectory()) return resolve(asPath, "tsconfig.json");
239
+ return asPath;
240
+ }
241
+ return resolve("tsconfig.json");
242
+ }
243
+ /**
244
+ * Errors the build when the tsconfig it builds with does not set
224
245
  * `isolatedDeclarations: true`.
225
246
  *
226
247
  * This is required: declarations for `.gts`/`.gjs` (template tag) modules can
@@ -234,16 +255,28 @@ function emberExternals() {
234
255
  * same rules the build enforces (exported values need explicit type
235
256
  * annotations, e.g. `export const X: TOC<Sig> = <template>...`).
236
257
  *
237
- * Projects without a `tsconfig.json` (JavaScript libraries) or without the
238
- * `typescript` package have no declarations to emit, so there is nothing to
239
- * check.
258
+ * The tsconfig checked is the one tsdown's `tsconfig` option points at, not
259
+ * necessarily `tsconfig.json`. A library whose package also holds dev-only code
260
+ * (a demo app, in-package tests) can therefore keep a permissive
261
+ * `tsconfig.json` covering everything for editors and `tsc --noEmit`, and point
262
+ * the build at a `tsconfig.publish.json` that covers only the publishable
263
+ * sources -- isolated declarations then constrain exactly the code that gets
264
+ * declarations emitted for it, and nothing else.
265
+ *
266
+ * Projects with no tsconfig (JavaScript libraries, or `tsconfig: false`) or
267
+ * without the `typescript` package have no declarations to emit, so there is
268
+ * nothing to check.
240
269
  */
241
270
  function emberIsolatedDeclarations() {
271
+ let tsconfigOption;
242
272
  return {
243
273
  name: "ember:isolated-declarations",
274
+ tsdownConfig(config) {
275
+ tsconfigOption = config.tsconfig;
276
+ },
244
277
  async buildStart() {
245
- const tsconfigPath = resolve("tsconfig.json");
246
- if (!existsSync(tsconfigPath)) return;
278
+ const tsconfigPath = resolveTsconfigPath(tsconfigOption);
279
+ if (!tsconfigPath || !existsSync(tsconfigPath)) return;
247
280
  let ts;
248
281
  try {
249
282
  ts = (await import("typescript")).default;
@@ -252,7 +285,7 @@ function emberIsolatedDeclarations() {
252
285
  }
253
286
  const { config, error } = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
254
287
  if (error) return;
255
- if (ts.parseJsonConfigFileContent(config, ts.sys, dirname(tsconfigPath)).options.isolatedDeclarations !== true) this.error(`${tsconfigPath} must set "compilerOptions.isolatedDeclarations": true.\n\nDeclarations for .gts/.gjs (template tag) modules can only be emitted by the isolated-declarations pipeline, which reads compiled modules from the bundler's module graph -- the tsc-based pipeline reads from disk and cannot see them.\n\nIsolated declarations require exported values to have explicit type annotations, e.g. \`export const X: TOC<Sig> = <template>...\`.`);
288
+ if (ts.parseJsonConfigFileContent(config, ts.sys, dirname(tsconfigPath)).options.isolatedDeclarations !== true) this.error(`${tsconfigPath} must set "compilerOptions.isolatedDeclarations": true.\n\nDeclarations for .gts/.gjs (template tag) modules can only be emitted by the isolated-declarations pipeline, which reads compiled modules from the bundler's module graph -- the tsc-based pipeline reads from disk and cannot see them.\n\nIsolated declarations require exported values to have explicit type annotations, e.g. \`export const X: TOC<Sig> = <template>...\`.\n\nIf this tsconfig also covers dev-only code (a demo app, in-package tests) that should not be constrained this way, point tsdown's \`tsconfig\` option at a publish-only tsconfig that sets the flag and covers just the published sources.`);
256
289
  }
257
290
  };
258
291
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nullvoxpopuli/ember-rolldown",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "Meta plugin for building ember v2 libraries/addons with rolldown & tsdown.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,122 @@
1
+ import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import path from "node:path";
4
+ import { build } from "tsdown";
5
+ import { afterEach, describe, expect, it } from "vitest";
6
+
7
+ import { ember } from "../index.ts";
8
+
9
+ let restoreCwd: (() => void) | undefined;
10
+
11
+ afterEach(() => {
12
+ restoreCwd?.();
13
+ restoreCwd = undefined;
14
+ });
15
+
16
+ /**
17
+ * Runs a real tsdown build of a single trivial entry in a temp dir, so the
18
+ * isolated-declarations guard reads the tsconfig files written by the test the
19
+ * same way it does under the tsdown CLI. Returns the build's rejection, or
20
+ * `undefined` when it succeeded.
21
+ */
22
+ async function buildFixture(
23
+ files: Record<string, string>,
24
+ tsconfig?: string | boolean,
25
+ ): Promise<Error | undefined> {
26
+ const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-isolated-declarations-"));
27
+
28
+ const withDefaults: Record<string, string> = {
29
+ "package.json": JSON.stringify({ name: "fixture", version: "0.0.0", type: "module" }),
30
+ "src/index.ts": `export const greeting: string = 'hello';`,
31
+ ...files,
32
+ };
33
+
34
+ for (const [relative, source] of Object.entries(withDefaults)) {
35
+ const full = path.join(dir, relative);
36
+ await mkdir(path.dirname(full), { recursive: true });
37
+ await writeFile(full, source, "utf8");
38
+ }
39
+
40
+ const previousCwd = process.cwd();
41
+ process.chdir(dir);
42
+ restoreCwd = () => process.chdir(previousCwd);
43
+
44
+ try {
45
+ await build({
46
+ entry: ["./src/index.ts"],
47
+ config: false,
48
+ logLevel: "silent",
49
+ tsconfig,
50
+ plugins: [ember()],
51
+ });
52
+
53
+ return undefined;
54
+ } catch (error) {
55
+ return error as Error;
56
+ }
57
+ }
58
+
59
+ const withFlag = JSON.stringify({
60
+ compilerOptions: { isolatedDeclarations: true, declaration: true },
61
+ });
62
+ const withoutFlag = JSON.stringify({ compilerOptions: { declaration: true } });
63
+
64
+ describe("isolated declarations guard", () => {
65
+ it("errors when the cwd tsconfig.json does not set the flag", async () => {
66
+ const error = await buildFixture({ "tsconfig.json": withoutFlag });
67
+
68
+ expect(error?.message).toContain(`must set "compilerOptions.isolatedDeclarations": true`);
69
+ });
70
+
71
+ it("passes when the cwd tsconfig.json sets the flag", async () => {
72
+ expect(await buildFixture({ "tsconfig.json": withFlag })).toBeUndefined();
73
+ });
74
+
75
+ it("checks the tsconfig the `tsconfig` option points at, not tsconfig.json", async () => {
76
+ // The shape a library with in-package dev code wants: a permissive
77
+ // tsconfig.json for editors and `tsc --noEmit` over everything, and a
78
+ // publish-only tsconfig -- the one the build uses -- carrying the flag.
79
+ const error = await buildFixture(
80
+ { "tsconfig.json": withoutFlag, "tsconfig.publish.json": withFlag },
81
+ "./tsconfig.publish.json",
82
+ );
83
+
84
+ expect(error).toBeUndefined();
85
+ });
86
+
87
+ it("errors when the tsconfig the `tsconfig` option points at lacks the flag", async () => {
88
+ const error = await buildFixture(
89
+ { "tsconfig.json": withFlag, "tsconfig.publish.json": withoutFlag },
90
+ "./tsconfig.publish.json",
91
+ );
92
+
93
+ expect(error?.message).toContain("tsconfig.publish.json");
94
+ expect(error?.message).toContain(`must set "compilerOptions.isolatedDeclarations": true`);
95
+ });
96
+
97
+ it("resolves a directory `tsconfig` option to the tsconfig.json inside it", async () => {
98
+ const error = await buildFixture(
99
+ { "tsconfig.json": withFlag, "config/tsconfig.json": withoutFlag },
100
+ "./config",
101
+ );
102
+
103
+ expect(error?.message).toContain(`must set "compilerOptions.isolatedDeclarations": true`);
104
+ });
105
+
106
+ it("skips the check when there is no tsconfig at all", async () => {
107
+ expect(await buildFixture({})).toBeUndefined();
108
+ });
109
+
110
+ it("skips the check when tsconfig is disabled", async () => {
111
+ expect(await buildFixture({ "tsconfig.json": withoutFlag }, false)).toBeUndefined();
112
+ });
113
+
114
+ it("inherits the flag through an extends chain", async () => {
115
+ const error = await buildFixture({
116
+ "tsconfig.base.json": withFlag,
117
+ "tsconfig.json": JSON.stringify({ extends: "./tsconfig.base.json" }),
118
+ });
119
+
120
+ expect(error).toBeUndefined();
121
+ });
122
+ });
@@ -1,10 +1,39 @@
1
- import { existsSync } from "node:fs";
1
+ import { existsSync, statSync } from "node:fs";
2
2
  import { dirname, resolve } from "node:path";
3
3
 
4
4
  import type { Plugin } from "rolldown";
5
+ import type { UserConfig } from "tsdown";
5
6
 
6
7
  /**
7
- * Errors the build when a `tsconfig.json` is present without
8
+ * Resolve the tsconfig the build will actually use, mirroring tsdown's own
9
+ * `tsconfig` option (`string | boolean`):
10
+ *
11
+ * - `undefined` / `true`: `tsconfig.json` in the cwd.
12
+ * - a path to a file: that file.
13
+ * - a path to a directory: `tsconfig.json` inside it.
14
+ * - `false`: no tsconfig at all.
15
+ *
16
+ * Under plain rolldown there is no such option, so the cwd's `tsconfig.json`
17
+ * is all there is.
18
+ */
19
+ function resolveTsconfigPath(option: string | boolean | undefined): string | undefined {
20
+ if (option === false) return undefined;
21
+
22
+ if (typeof option === "string") {
23
+ const asPath = resolve(option);
24
+
25
+ if (existsSync(asPath) && statSync(asPath).isDirectory()) {
26
+ return resolve(asPath, "tsconfig.json");
27
+ }
28
+
29
+ return asPath;
30
+ }
31
+
32
+ return resolve("tsconfig.json");
33
+ }
34
+
35
+ /**
36
+ * Errors the build when the tsconfig it builds with does not set
8
37
  * `isolatedDeclarations: true`.
9
38
  *
10
39
  * This is required: declarations for `.gts`/`.gjs` (template tag) modules can
@@ -18,18 +47,34 @@ import type { Plugin } from "rolldown";
18
47
  * same rules the build enforces (exported values need explicit type
19
48
  * annotations, e.g. `export const X: TOC<Sig> = <template>...`).
20
49
  *
21
- * Projects without a `tsconfig.json` (JavaScript libraries) or without the
22
- * `typescript` package have no declarations to emit, so there is nothing to
23
- * check.
50
+ * The tsconfig checked is the one tsdown's `tsconfig` option points at, not
51
+ * necessarily `tsconfig.json`. A library whose package also holds dev-only code
52
+ * (a demo app, in-package tests) can therefore keep a permissive
53
+ * `tsconfig.json` covering everything for editors and `tsc --noEmit`, and point
54
+ * the build at a `tsconfig.publish.json` that covers only the publishable
55
+ * sources -- isolated declarations then constrain exactly the code that gets
56
+ * declarations emitted for it, and nothing else.
57
+ *
58
+ * Projects with no tsconfig (JavaScript libraries, or `tsconfig: false`) or
59
+ * without the `typescript` package have no declarations to emit, so there is
60
+ * nothing to check.
24
61
  */
25
62
  export function emberIsolatedDeclarations(): Plugin {
63
+ let tsconfigOption: string | boolean | undefined;
64
+
26
65
  return {
27
66
  name: "ember:isolated-declarations",
28
67
 
68
+ // Not called under plain rolldown, which leaves `tsconfigOption` undefined
69
+ // -- i.e. the cwd's tsconfig.json, the only thing it could mean there.
70
+ tsdownConfig(config: UserConfig) {
71
+ tsconfigOption = config.tsconfig;
72
+ },
73
+
29
74
  async buildStart() {
30
- const tsconfigPath = resolve("tsconfig.json");
75
+ const tsconfigPath = resolveTsconfigPath(tsconfigOption);
31
76
 
32
- if (!existsSync(tsconfigPath)) return;
77
+ if (!tsconfigPath || !existsSync(tsconfigPath)) return;
33
78
 
34
79
  let ts: typeof import("typescript");
35
80
 
@@ -57,7 +102,10 @@ export function emberIsolatedDeclarations(): Plugin {
57
102
  `isolated-declarations pipeline, which reads compiled modules from the bundler's ` +
58
103
  `module graph -- the tsc-based pipeline reads from disk and cannot see them.\n\n` +
59
104
  `Isolated declarations require exported values to have explicit type annotations, ` +
60
- `e.g. \`export const X: TOC<Sig> = <template>...\`.`,
105
+ `e.g. \`export const X: TOC<Sig> = <template>...\`.\n\n` +
106
+ `If this tsconfig also covers dev-only code (a demo app, in-package tests) that ` +
107
+ `should not be constrained this way, point tsdown's \`tsconfig\` option at a ` +
108
+ `publish-only tsconfig that sets the flag and covers just the published sources.`,
61
109
  );
62
110
  }
63
111
  },