@nullvoxpopuli/ember-rolldown 2.4.0 → 2.6.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 +93 -6
- package/dist/index.js +95 -14
- package/package.json +1 -1
- package/src/babel.test.ts +125 -2
- package/src/babel.ts +52 -7
- package/src/isolated-declarations.ts +81 -8
package/README.md
CHANGED
|
@@ -53,10 +53,12 @@ export default defineConfig({
|
|
|
53
53
|
});
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
A
|
|
56
|
+
A babel config is optional. Without one, `ember()` compiles templates
|
|
57
57
|
(to `precompileTemplate`), decorators (via
|
|
58
58
|
[decorator-transforms](https://github.com/ef4/decorator-transforms)), and
|
|
59
|
-
TypeScript; with one, your config runs instead.
|
|
59
|
+
TypeScript; with one, your config runs instead. A `babel.publish.config.*` is
|
|
60
|
+
preferred over a `babel.config.*` — see
|
|
61
|
+
[Publish vs. development babel config](#publish-vs-development-babel-config).
|
|
60
62
|
|
|
61
63
|
### Entrypoints
|
|
62
64
|
|
|
@@ -81,7 +83,7 @@ own and is only reachable through the entrypoints that re-export it.
|
|
|
81
83
|
|
|
82
84
|
Declarations are emitted with isolated declarations — the only declaration
|
|
83
85
|
pipeline that can see `<template>` (`.gts`/`.gjs`) modules, which exist only
|
|
84
|
-
inside the bundler's module graph.
|
|
86
|
+
inside the bundler's module graph. The tsconfig your build uses must enable it
|
|
85
87
|
(`ember()` errors otherwise):
|
|
86
88
|
|
|
87
89
|
```jsonc
|
|
@@ -101,6 +103,48 @@ import type { TOC } from "@ember/component/template-only";
|
|
|
101
103
|
export const Badge: TOC<BadgeSignature> = <template>...</template>;
|
|
102
104
|
```
|
|
103
105
|
|
|
106
|
+
#### Keeping the constraint off dev-only code
|
|
107
|
+
|
|
108
|
+
`isolatedDeclarations` is a constraint on how _published_ code is written, so it
|
|
109
|
+
should cover only the code you emit declarations for. If your package also holds
|
|
110
|
+
dev-only code — a demo app, in-package tests — putting the flag on the single
|
|
111
|
+
`tsconfig.json` that covers everything would force explicit annotations on demo
|
|
112
|
+
components and test helpers that never get a `.d.ts` emitted for them.
|
|
113
|
+
|
|
114
|
+
The tsconfig `ember()` checks is the one
|
|
115
|
+
[tsdown's `tsconfig` option](https://tsdown.dev/options/tsconfig) points at, so
|
|
116
|
+
point the build at a publish-only config and leave `tsconfig.json` alone for
|
|
117
|
+
editors and `tsc --noEmit`:
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
// tsdown.config.js
|
|
121
|
+
export default defineConfig({
|
|
122
|
+
entry: ["./src/index.ts"],
|
|
123
|
+
// include: ["src/**/*"], isolatedDeclarations: true
|
|
124
|
+
tsconfig: "./tsconfig.publish.json",
|
|
125
|
+
plugins: [ember()],
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Any path works, so the publish config can live wherever you keep build
|
|
130
|
+
configuration — `tsconfig: "./config/tsconfig.publish.json"`. (A directory works
|
|
131
|
+
too: `tsconfig: "./config"` picks up `config/tsconfig.json`.)
|
|
132
|
+
|
|
133
|
+
`tsconfig: false` is rejected while declarations are on: with no tsconfig there
|
|
134
|
+
is no `isolatedDeclarations`, and the fallback pipeline can't see compiled
|
|
135
|
+
`.gts`, so it would fail later with "Source file not found". Set `dts: false`
|
|
136
|
+
alongside it if the library ships no types.
|
|
137
|
+
|
|
138
|
+
A tsconfig's relative paths resolve against the file itself, so a config kept in
|
|
139
|
+
`config/` wants `"include": ["../src/**/*"]` and `"rootDir": "../src"`. The build
|
|
140
|
+
won't tell you if you get that wrong — tsdown drives declaration emit from
|
|
141
|
+
`entry`, not from the tsconfig's `include` — but `tsc`/`ember-tsc` and your
|
|
142
|
+
editor will, if you ever point them at that config.
|
|
143
|
+
|
|
144
|
+
The tradeoff: isolated-declaration errors in `src` then surface when you build
|
|
145
|
+
rather than in your editor, since the editor uses `tsconfig.json`. Run the build
|
|
146
|
+
(or `tsc --noEmit -p tsconfig.publish.json`) in CI so nothing lands unchecked.
|
|
147
|
+
|
|
104
148
|
## CSS
|
|
105
149
|
|
|
106
150
|
Components that import co-located CSS (`import './popup.css'`) need
|
|
@@ -159,8 +203,9 @@ it.
|
|
|
159
203
|
|
|
160
204
|
`ember()` returns an array of rolldown plugins:
|
|
161
205
|
|
|
162
|
-
- **`emberIsolatedDeclarations()`** — errors when
|
|
163
|
-
|
|
206
|
+
- **`emberIsolatedDeclarations()`** — errors when the tsconfig the build uses
|
|
207
|
+
(tsdown's `tsconfig` option, defaulting to `tsconfig.json`) is present without
|
|
208
|
+
`isolatedDeclarations: true`.
|
|
164
209
|
- **`emberExternals()`** — keeps your `dependencies`, `peerDependencies`, and
|
|
165
210
|
the ember virtual packages (e.g. `@ember/component`, `@glimmer/tracking`, the
|
|
166
211
|
template compiler) external, so the consuming app resolves them.
|
|
@@ -170,7 +215,9 @@ it.
|
|
|
170
215
|
`.gts` specifiers in emitted `.d.ts` files.
|
|
171
216
|
- **`emberBabel()`** — runs babel with `babelHelpers: "bundled"`, but only on
|
|
172
217
|
the files that actually need it (template-tag, decorators, template imports);
|
|
173
|
-
everything else stays on rolldown's fast native (oxc) transform.
|
|
218
|
+
everything else stays on rolldown's fast native (oxc) transform. Uses your
|
|
219
|
+
`babel.publish.config.*` (root or `config/`) in preference to your
|
|
220
|
+
`babel.config.*`.
|
|
174
221
|
|
|
175
222
|
## Configuration
|
|
176
223
|
|
|
@@ -192,6 +239,46 @@ combined with a babel config file — a config lists
|
|
|
192
239
|
`babel-plugin-ember-template-compilation` itself, so its transforms belong
|
|
193
240
|
there.
|
|
194
241
|
|
|
242
|
+
### Publish vs. development babel config
|
|
243
|
+
|
|
244
|
+
A library's plain `babel.config.*` is usually its _development_ config: it
|
|
245
|
+
compiles `@embroider/macros` away, targets the wire format, and wires up
|
|
246
|
+
whatever the in-package demo app or test suite needs. None of that belongs in a
|
|
247
|
+
published artifact — macros must survive for the consuming app to evaluate, and
|
|
248
|
+
the wire format is private between one template compiler and one glimmer
|
|
249
|
+
runtime of the same version.
|
|
250
|
+
|
|
251
|
+
Babel's own resolution can't tell those apart, so `ember()` looks for a config
|
|
252
|
+
named for publishing first. With no explicit `configFile`, detection is:
|
|
253
|
+
|
|
254
|
+
1. `babel.publish.config.{mjs,cjs,js,mts,cts,ts,json}` in the package root
|
|
255
|
+
2. the same names in `config/`
|
|
256
|
+
3. otherwise babel's own resolution (`babel.config.*`, honoring `rootMode`)
|
|
257
|
+
4. otherwise no config file — templates, decorators and TypeScript are still
|
|
258
|
+
handled by the built-in defaults
|
|
259
|
+
|
|
260
|
+
So a library that keeps both configs needs no `babel` option at all:
|
|
261
|
+
|
|
262
|
+
```
|
|
263
|
+
my-addon/
|
|
264
|
+
babel.config.mjs # dev: macros compiled, wire format, test-app wiring
|
|
265
|
+
babel.publish.config.mjs # what ember() uses
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
...and so does one that keeps build configuration out of its root:
|
|
269
|
+
|
|
270
|
+
```
|
|
271
|
+
my-addon/
|
|
272
|
+
babel.config.mjs
|
|
273
|
+
config/
|
|
274
|
+
babel.publish.config.mjs # what ember() uses
|
|
275
|
+
tsconfig.publish.json # see below
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Set `configFile` explicitly to override that (`configFile: false` ignores config
|
|
279
|
+
files entirely, which is what you want when your publish config would just
|
|
280
|
+
restate the built-in defaults).
|
|
281
|
+
|
|
195
282
|
## App re-exports
|
|
196
283
|
|
|
197
284
|
For libraries whose modules must appear in the consuming app's namespace
|
package/dist/index.js
CHANGED
|
@@ -3,21 +3,60 @@ 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";
|
|
7
|
-
import { createRequire } from "node:module";
|
|
6
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
8
7
|
import path, { dirname, resolve } from "node:path";
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
9
|
import pkg from "@embroider/core";
|
|
10
10
|
import { Preprocessor } from "content-tag";
|
|
11
11
|
import { readFile, readdir, writeFile } from "node:fs/promises";
|
|
12
12
|
//#region src/babel.ts
|
|
13
|
+
/** Tried in order, so an ESM config wins over a JSON one of the same name. */
|
|
14
|
+
const PUBLISH_CONFIG_EXTENSIONS = [
|
|
15
|
+
"mjs",
|
|
16
|
+
"cjs",
|
|
17
|
+
"js",
|
|
18
|
+
"mts",
|
|
19
|
+
"cts",
|
|
20
|
+
"ts",
|
|
21
|
+
"json"
|
|
22
|
+
];
|
|
23
|
+
/**
|
|
24
|
+
* Tried in order: next to package.json first, then `config/` for libraries that
|
|
25
|
+
* keep their build configuration out of the package root.
|
|
26
|
+
*/
|
|
27
|
+
const PUBLISH_CONFIG_DIRECTORIES = [".", "config"];
|
|
28
|
+
/**
|
|
29
|
+
* A `babel.publish.config.*` (in the package root or `config/`), if there is
|
|
30
|
+
* one.
|
|
31
|
+
*
|
|
32
|
+
* A library's plain `babel.config.*` is its *development* config: it typically
|
|
33
|
+
* compiles `@embroider/macros` away, targets the wire format, and wires up
|
|
34
|
+
* whatever the in-package demo app or test suite needs. None of that belongs in
|
|
35
|
+
* a published artifact -- macros must stay for the consuming app to evaluate,
|
|
36
|
+
* and the wire format is private between one template compiler and one glimmer
|
|
37
|
+
* runtime. Babel's own resolution can't tell the two apart (it finds whatever
|
|
38
|
+
* `babel.config.*` exists), so a publish build has to prefer the config named
|
|
39
|
+
* for publishing.
|
|
40
|
+
*/
|
|
41
|
+
function detectPublishConfigFile() {
|
|
42
|
+
for (const directory of PUBLISH_CONFIG_DIRECTORIES) for (const extension of PUBLISH_CONFIG_EXTENSIONS) {
|
|
43
|
+
const candidate = resolve(directory, `babel.publish.config.${extension}`);
|
|
44
|
+
if (existsSync(candidate)) return candidate;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
13
47
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
48
|
+
* The config this publish build should use: a `babel.publish.config.*` when the
|
|
49
|
+
* library has one, else the project's root babel config resolved the way babel
|
|
50
|
+
* itself does (`babel.config.{js,cjs,mjs,cts,json}`, honoring `rootMode`
|
|
51
|
+
* semantics) -- which for a library whose only config *is* its publish config
|
|
52
|
+
* is the same file either way.
|
|
16
53
|
*
|
|
17
54
|
* The sync API evaluates the config file; on node 24+ that works for native
|
|
18
55
|
* ESM configs too (require(esm)).
|
|
19
56
|
*/
|
|
20
57
|
function detectConfigFile() {
|
|
58
|
+
const publishConfig = detectPublishConfigFile();
|
|
59
|
+
if (publishConfig) return publishConfig;
|
|
21
60
|
return loadPartialConfigSync({ cwd: process.cwd() })?.config ?? void 0;
|
|
22
61
|
}
|
|
23
62
|
/**
|
|
@@ -65,9 +104,10 @@ function defaultPlugins(templateTransforms) {
|
|
|
65
104
|
* to only the files that actually need it (template-tag, decorators, template
|
|
66
105
|
* imports). Everything else stays on the native transform.
|
|
67
106
|
*
|
|
68
|
-
* The library's own babel config is used when it exists
|
|
69
|
-
* `
|
|
70
|
-
* config
|
|
107
|
+
* The library's own babel config is used when it exists -- its
|
|
108
|
+
* `babel.publish.config.*` (root or `config/`) in preference to its
|
|
109
|
+
* `babel.config.*`, since this is a publish build. Without one, `defaultPlugins` covers templates, decorators,
|
|
110
|
+
* and TypeScript, so no config file is required.
|
|
71
111
|
*
|
|
72
112
|
* Libraries default to `babelHelpers: "bundled"` so the emitted output is
|
|
73
113
|
* self-contained.
|
|
@@ -220,7 +260,28 @@ function emberExternals() {
|
|
|
220
260
|
//#endregion
|
|
221
261
|
//#region src/isolated-declarations.ts
|
|
222
262
|
/**
|
|
223
|
-
*
|
|
263
|
+
* Resolve the tsconfig the build will actually use, mirroring tsdown's own
|
|
264
|
+
* `tsconfig` option (`string | boolean`):
|
|
265
|
+
*
|
|
266
|
+
* - `undefined` / `true`: `tsconfig.json` in the cwd.
|
|
267
|
+
* - a path to a file: that file.
|
|
268
|
+
* - a path to a directory: `tsconfig.json` inside it.
|
|
269
|
+
* - `false`: no tsconfig at all.
|
|
270
|
+
*
|
|
271
|
+
* Under plain rolldown there is no such option, so the cwd's `tsconfig.json`
|
|
272
|
+
* is all there is.
|
|
273
|
+
*/
|
|
274
|
+
function resolveTsconfigPath(option) {
|
|
275
|
+
if (option === false) return void 0;
|
|
276
|
+
if (typeof option === "string") {
|
|
277
|
+
const asPath = resolve(option);
|
|
278
|
+
if (existsSync(asPath) && statSync(asPath).isDirectory()) return resolve(asPath, "tsconfig.json");
|
|
279
|
+
return asPath;
|
|
280
|
+
}
|
|
281
|
+
return resolve("tsconfig.json");
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Errors the build when the tsconfig it builds with does not set
|
|
224
285
|
* `isolatedDeclarations: true`.
|
|
225
286
|
*
|
|
226
287
|
* This is required: declarations for `.gts`/`.gjs` (template tag) modules can
|
|
@@ -234,16 +295,36 @@ function emberExternals() {
|
|
|
234
295
|
* same rules the build enforces (exported values need explicit type
|
|
235
296
|
* annotations, e.g. `export const X: TOC<Sig> = <template>...`).
|
|
236
297
|
*
|
|
237
|
-
*
|
|
238
|
-
* `
|
|
239
|
-
*
|
|
298
|
+
* The tsconfig checked is the one tsdown's `tsconfig` option points at, not
|
|
299
|
+
* necessarily `tsconfig.json`. A library whose package also holds dev-only code
|
|
300
|
+
* (a demo app, in-package tests) can therefore keep a permissive
|
|
301
|
+
* `tsconfig.json` covering everything for editors and `tsc --noEmit`, and point
|
|
302
|
+
* the build at a `tsconfig.publish.json` that covers only the publishable
|
|
303
|
+
* sources -- isolated declarations then constrain exactly the code that gets
|
|
304
|
+
* declarations emitted for it, and nothing else.
|
|
305
|
+
*
|
|
306
|
+
* Projects with no tsconfig (JavaScript libraries) or without the `typescript`
|
|
307
|
+
* package have no declarations to emit, so there is nothing to check.
|
|
308
|
+
* `tsconfig: false` is different: it is an explicit opt-out, and combined with
|
|
309
|
+
* declaration emit it produces a confusing "Source file not found" from the
|
|
310
|
+
* tsc-based pipeline, so it errors here instead.
|
|
240
311
|
*/
|
|
241
312
|
function emberIsolatedDeclarations() {
|
|
313
|
+
let tsconfigOption;
|
|
314
|
+
let emitsDeclarations = true;
|
|
242
315
|
return {
|
|
243
316
|
name: "ember:isolated-declarations",
|
|
317
|
+
tsdownConfig(config) {
|
|
318
|
+
tsconfigOption = config.tsconfig;
|
|
319
|
+
emitsDeclarations = config.dts !== false;
|
|
320
|
+
},
|
|
244
321
|
async buildStart() {
|
|
245
|
-
|
|
246
|
-
|
|
322
|
+
if (tsconfigOption === false) {
|
|
323
|
+
if (!emitsDeclarations) return;
|
|
324
|
+
this.error("`tsconfig: false` cannot be combined with declaration emit.\n\nDeclarations are emitted by the isolated-declarations pipeline, which needs a tsconfig setting \"compilerOptions.isolatedDeclarations\": true -- it is the only pipeline that can see compiled .gts/.gjs modules.\n\nEither point tsdown's `tsconfig` option at such a config, or set `dts: false` if this library ships no types.");
|
|
325
|
+
}
|
|
326
|
+
const tsconfigPath = resolveTsconfigPath(tsconfigOption);
|
|
327
|
+
if (!tsconfigPath || !existsSync(tsconfigPath)) return;
|
|
247
328
|
let ts;
|
|
248
329
|
try {
|
|
249
330
|
ts = (await import("typescript")).default;
|
|
@@ -252,7 +333,7 @@ function emberIsolatedDeclarations() {
|
|
|
252
333
|
}
|
|
253
334
|
const { config, error } = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
|
|
254
335
|
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
|
|
336
|
+
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
337
|
}
|
|
257
338
|
};
|
|
258
339
|
}
|
package/package.json
CHANGED
package/src/babel.test.ts
CHANGED
|
@@ -1,6 +1,85 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mkdtemp, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { rolldown } from "rolldown";
|
|
5
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
6
|
|
|
3
|
-
import { emberBabel } from "./babel.ts";
|
|
7
|
+
import { emberBabel, type BabelOptions } from "./babel.ts";
|
|
8
|
+
import { emberTransform } from "./transform.ts";
|
|
9
|
+
|
|
10
|
+
let restoreCwd: (() => void) | undefined;
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
restoreCwd?.();
|
|
14
|
+
restoreCwd = undefined;
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A babel config whose only plugin rewrites the string `"MARKER"` to `name`, so
|
|
19
|
+
* the emitted code says which config file babel actually loaded.
|
|
20
|
+
*/
|
|
21
|
+
function markerConfig(name: string): string {
|
|
22
|
+
return [
|
|
23
|
+
`export default {`,
|
|
24
|
+
` plugins: [`,
|
|
25
|
+
` function marker() {`,
|
|
26
|
+
` return {`,
|
|
27
|
+
` visitor: {`,
|
|
28
|
+
` StringLiteral(path) {`,
|
|
29
|
+
` if (path.node.value === 'MARKER') path.node.value = '${name}';`,
|
|
30
|
+
` },`,
|
|
31
|
+
` },`,
|
|
32
|
+
` };`,
|
|
33
|
+
` },`,
|
|
34
|
+
` ],`,
|
|
35
|
+
`};`,
|
|
36
|
+
].join("\n");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Builds a `.gts` entry (so babel actually runs -- `maybeBabel` skips files that
|
|
41
|
+
* need no transform) in a temp dir that becomes the cwd, the way config
|
|
42
|
+
* detection sees it under the tsdown CLI. Returns the emitted code.
|
|
43
|
+
*/
|
|
44
|
+
async function bundleWithBabel(
|
|
45
|
+
files: Record<string, string>,
|
|
46
|
+
options?: BabelOptions,
|
|
47
|
+
): Promise<string> {
|
|
48
|
+
const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-babel-"));
|
|
49
|
+
|
|
50
|
+
const withDefaults: Record<string, string> = {
|
|
51
|
+
"package.json": JSON.stringify({ name: "fixture", version: "0.0.0", type: "module" }),
|
|
52
|
+
"index.gts": [
|
|
53
|
+
`export const marker = 'MARKER';`,
|
|
54
|
+
`export default <template>hi</template>;`,
|
|
55
|
+
].join("\n"),
|
|
56
|
+
...files,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
for (const [relative, source] of Object.entries(withDefaults)) {
|
|
60
|
+
const full = path.join(dir, relative);
|
|
61
|
+
await mkdir(path.dirname(full), { recursive: true });
|
|
62
|
+
await writeFile(full, source, "utf8");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const previousCwd = process.cwd();
|
|
66
|
+
process.chdir(dir);
|
|
67
|
+
restoreCwd = () => process.chdir(previousCwd);
|
|
68
|
+
|
|
69
|
+
const build = await rolldown({
|
|
70
|
+
input: [path.join(dir, "index.gts")],
|
|
71
|
+
plugins: [emberTransform(), emberBabel(options)],
|
|
72
|
+
external: (id) => !(id.startsWith(".") || path.isAbsolute(id)),
|
|
73
|
+
onwarn() {},
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const { output } = await build.generate({ format: "es" });
|
|
77
|
+
|
|
78
|
+
return output
|
|
79
|
+
.filter((chunk): chunk is typeof chunk & { code: string } => "code" in chunk)
|
|
80
|
+
.map((chunk) => chunk.code)
|
|
81
|
+
.join("\n");
|
|
82
|
+
}
|
|
4
83
|
|
|
5
84
|
describe("emberBabel", () => {
|
|
6
85
|
it("rejects templateTransforms combined with a config file", () => {
|
|
@@ -14,4 +93,48 @@ describe("emberBabel", () => {
|
|
|
14
93
|
emberBabel({ configFile: false, templateTransforms: ["some-transform"] }),
|
|
15
94
|
).not.toThrow();
|
|
16
95
|
});
|
|
96
|
+
|
|
97
|
+
describe("config detection", () => {
|
|
98
|
+
it("prefers babel.publish.config over babel.config", async () => {
|
|
99
|
+
const code = await bundleWithBabel({
|
|
100
|
+
"babel.config.mjs": markerConfig("dev"),
|
|
101
|
+
"babel.publish.config.mjs": markerConfig("publish"),
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
expect(code).toContain(`"publish"`);
|
|
105
|
+
expect(code).not.toContain(`"dev"`);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("falls back to babel.config when there is no publish config", async () => {
|
|
109
|
+
const code = await bundleWithBabel({ "babel.config.mjs": markerConfig("dev") });
|
|
110
|
+
|
|
111
|
+
expect(code).toContain(`"dev"`);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("uses an explicit configFile over either", async () => {
|
|
115
|
+
const code = await bundleWithBabel(
|
|
116
|
+
{
|
|
117
|
+
"babel.config.mjs": markerConfig("dev"),
|
|
118
|
+
"babel.publish.config.mjs": markerConfig("publish"),
|
|
119
|
+
"babel.explicit.config.mjs": markerConfig("explicit"),
|
|
120
|
+
},
|
|
121
|
+
{ configFile: "./babel.explicit.config.mjs" },
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(code).toContain(`"explicit"`);
|
|
125
|
+
expect(code).not.toContain(`"publish"`);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("ignores every config file when configFile is false", async () => {
|
|
129
|
+
const code = await bundleWithBabel(
|
|
130
|
+
{
|
|
131
|
+
"babel.config.mjs": markerConfig("dev"),
|
|
132
|
+
"babel.publish.config.mjs": markerConfig("publish"),
|
|
133
|
+
},
|
|
134
|
+
{ configFile: false },
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
expect(code).toContain(`"MARKER"`);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
17
140
|
});
|
package/src/babel.ts
CHANGED
|
@@ -3,6 +3,8 @@ 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 } from "node:fs";
|
|
7
|
+
import { resolve } from "node:path";
|
|
6
8
|
import type { PluginItem } from "@babel/core";
|
|
7
9
|
import type { Transform } from "babel-plugin-ember-template-compilation";
|
|
8
10
|
import type { Plugin } from "rolldown";
|
|
@@ -12,8 +14,9 @@ export interface BabelOptions {
|
|
|
12
14
|
/**
|
|
13
15
|
* The babel config file to use.
|
|
14
16
|
*
|
|
15
|
-
* - `undefined`: auto-detect
|
|
16
|
-
* config
|
|
17
|
+
* - `undefined`: auto-detect. A `babel.publish.config.*` (in the package root
|
|
18
|
+
* or `config/`) wins, because this is a publish build; otherwise babel's own
|
|
19
|
+
* config resolution applies (see `detectConfigFile`).
|
|
17
20
|
* - a string: use that config file.
|
|
18
21
|
* - `false`: ignore config files entirely.
|
|
19
22
|
*
|
|
@@ -46,14 +49,55 @@ export interface BabelOptions {
|
|
|
46
49
|
};
|
|
47
50
|
}
|
|
48
51
|
|
|
52
|
+
/** Tried in order, so an ESM config wins over a JSON one of the same name. */
|
|
53
|
+
const PUBLISH_CONFIG_EXTENSIONS = ["mjs", "cjs", "js", "mts", "cts", "ts", "json"];
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Tried in order: next to package.json first, then `config/` for libraries that
|
|
57
|
+
* keep their build configuration out of the package root.
|
|
58
|
+
*/
|
|
59
|
+
const PUBLISH_CONFIG_DIRECTORIES = [".", "config"];
|
|
60
|
+
|
|
49
61
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
62
|
+
* A `babel.publish.config.*` (in the package root or `config/`), if there is
|
|
63
|
+
* one.
|
|
64
|
+
*
|
|
65
|
+
* A library's plain `babel.config.*` is its *development* config: it typically
|
|
66
|
+
* compiles `@embroider/macros` away, targets the wire format, and wires up
|
|
67
|
+
* whatever the in-package demo app or test suite needs. None of that belongs in
|
|
68
|
+
* a published artifact -- macros must stay for the consuming app to evaluate,
|
|
69
|
+
* and the wire format is private between one template compiler and one glimmer
|
|
70
|
+
* runtime. Babel's own resolution can't tell the two apart (it finds whatever
|
|
71
|
+
* `babel.config.*` exists), so a publish build has to prefer the config named
|
|
72
|
+
* for publishing.
|
|
73
|
+
*/
|
|
74
|
+
function detectPublishConfigFile(): string | undefined {
|
|
75
|
+
for (const directory of PUBLISH_CONFIG_DIRECTORIES) {
|
|
76
|
+
for (const extension of PUBLISH_CONFIG_EXTENSIONS) {
|
|
77
|
+
const candidate = resolve(directory, `babel.publish.config.${extension}`);
|
|
78
|
+
|
|
79
|
+
if (existsSync(candidate)) return candidate;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The config this publish build should use: a `babel.publish.config.*` when the
|
|
88
|
+
* library has one, else the project's root babel config resolved the way babel
|
|
89
|
+
* itself does (`babel.config.{js,cjs,mjs,cts,json}`, honoring `rootMode`
|
|
90
|
+
* semantics) -- which for a library whose only config *is* its publish config
|
|
91
|
+
* is the same file either way.
|
|
52
92
|
*
|
|
53
93
|
* The sync API evaluates the config file; on node 24+ that works for native
|
|
54
94
|
* ESM configs too (require(esm)).
|
|
55
95
|
*/
|
|
56
96
|
function detectConfigFile(): string | undefined {
|
|
97
|
+
const publishConfig = detectPublishConfigFile();
|
|
98
|
+
|
|
99
|
+
if (publishConfig) return publishConfig;
|
|
100
|
+
|
|
57
101
|
const partial = loadPartialConfigSync({ cwd: process.cwd() });
|
|
58
102
|
|
|
59
103
|
return partial?.config ?? undefined;
|
|
@@ -112,9 +156,10 @@ function defaultPlugins(templateTransforms?: Transform[]): PluginItem[] {
|
|
|
112
156
|
* to only the files that actually need it (template-tag, decorators, template
|
|
113
157
|
* imports). Everything else stays on the native transform.
|
|
114
158
|
*
|
|
115
|
-
* The library's own babel config is used when it exists
|
|
116
|
-
* `
|
|
117
|
-
* config
|
|
159
|
+
* The library's own babel config is used when it exists -- its
|
|
160
|
+
* `babel.publish.config.*` (root or `config/`) in preference to its
|
|
161
|
+
* `babel.config.*`, since this is a publish build. Without one, `defaultPlugins` covers templates, decorators,
|
|
162
|
+
* and TypeScript, so no config file is required.
|
|
118
163
|
*
|
|
119
164
|
* Libraries default to `babelHelpers: "bundled"` so the emitted output is
|
|
120
165
|
* self-contained.
|
|
@@ -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
|
-
*
|
|
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,59 @@ 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
|
-
*
|
|
22
|
-
* `
|
|
23
|
-
*
|
|
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 without the `typescript`
|
|
59
|
+
* package have no declarations to emit, so there is nothing to check.
|
|
60
|
+
* `tsconfig: false` is different: it is an explicit opt-out, and combined with
|
|
61
|
+
* declaration emit it produces a confusing "Source file not found" from the
|
|
62
|
+
* tsc-based pipeline, so it errors here instead.
|
|
24
63
|
*/
|
|
25
64
|
export function emberIsolatedDeclarations(): Plugin {
|
|
65
|
+
let tsconfigOption: string | boolean | undefined;
|
|
66
|
+
let emitsDeclarations = true;
|
|
67
|
+
|
|
26
68
|
return {
|
|
27
69
|
name: "ember:isolated-declarations",
|
|
28
70
|
|
|
71
|
+
// Not called under plain rolldown, which leaves `tsconfigOption` undefined
|
|
72
|
+
// -- i.e. the cwd's tsconfig.json, the only thing it could mean there.
|
|
73
|
+
tsdownConfig(config: UserConfig) {
|
|
74
|
+
tsconfigOption = config.tsconfig;
|
|
75
|
+
// `emberConfig()` turns dts on unless the library turned it off, so an
|
|
76
|
+
// explicit `false` is the only "no declarations" signal. Hook order
|
|
77
|
+
// between plugins isn't guaranteed, so don't rely on it having run.
|
|
78
|
+
emitsDeclarations = config.dts !== false;
|
|
79
|
+
},
|
|
80
|
+
|
|
29
81
|
async buildStart() {
|
|
30
|
-
|
|
82
|
+
if (tsconfigOption === false) {
|
|
83
|
+
if (!emitsDeclarations) return;
|
|
84
|
+
|
|
85
|
+
// Without a tsconfig there is no `isolatedDeclarations`, so tsdown
|
|
86
|
+
// falls back to the tsc-based declaration pipeline -- which reads
|
|
87
|
+
// source files from disk and dies with "Source file not found" on
|
|
88
|
+
// every `.gts`/`.gjs`, since those only exist compiled in the module
|
|
89
|
+
// graph. Say so here instead of leaving that to be decoded.
|
|
90
|
+
this.error(
|
|
91
|
+
`\`tsconfig: false\` cannot be combined with declaration emit.\n\n` +
|
|
92
|
+
`Declarations are emitted by the isolated-declarations pipeline, which needs a ` +
|
|
93
|
+
`tsconfig setting "compilerOptions.isolatedDeclarations": true -- it is the only ` +
|
|
94
|
+
`pipeline that can see compiled .gts/.gjs modules.\n\n` +
|
|
95
|
+
`Either point tsdown's \`tsconfig\` option at such a config, or set \`dts: false\` ` +
|
|
96
|
+
`if this library ships no types.`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const tsconfigPath = resolveTsconfigPath(tsconfigOption);
|
|
31
101
|
|
|
32
|
-
if (!existsSync(tsconfigPath)) return;
|
|
102
|
+
if (!tsconfigPath || !existsSync(tsconfigPath)) return;
|
|
33
103
|
|
|
34
104
|
let ts: typeof import("typescript");
|
|
35
105
|
|
|
@@ -57,7 +127,10 @@ export function emberIsolatedDeclarations(): Plugin {
|
|
|
57
127
|
`isolated-declarations pipeline, which reads compiled modules from the bundler's ` +
|
|
58
128
|
`module graph -- the tsc-based pipeline reads from disk and cannot see them.\n\n` +
|
|
59
129
|
`Isolated declarations require exported values to have explicit type annotations, ` +
|
|
60
|
-
`e.g. \`export const X: TOC<Sig> = <template
|
|
130
|
+
`e.g. \`export const X: TOC<Sig> = <template>...\`.\n\n` +
|
|
131
|
+
`If this tsconfig also covers dev-only code (a demo app, in-package tests) that ` +
|
|
132
|
+
`should not be constrained this way, point tsdown's \`tsconfig\` option at a ` +
|
|
133
|
+
`publish-only tsconfig that sets the flag and covers just the published sources.`,
|
|
61
134
|
);
|
|
62
135
|
}
|
|
63
136
|
},
|