@nullvoxpopuli/ember-rolldown 1.1.0 → 2.1.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 +68 -1
- package/dist/app-reexports.js +97 -0
- package/dist/index.js +62 -39
- package/index.ts +8 -4
- package/package.json +8 -3
- package/rolldown.config.js +5 -3
- package/src/app-reexports.test.ts +220 -0
- package/src/app-reexports.ts +152 -0
- package/src/config.ts +50 -0
- package/src/dts.test.ts +166 -0
- package/src/transform.test.ts +173 -0
- package/src/define-config.ts +0 -32
package/README.md
CHANGED
|
@@ -20,10 +20,15 @@ modern TypeScript when type-checking: 6+ with `lib` covering `es2025` (e.g.
|
|
|
20
20
|
|
|
21
21
|
## Usage
|
|
22
22
|
|
|
23
|
+
Import `defineConfig` from the bundler you're using — `tsdown` or `rolldown` —
|
|
24
|
+
so it carries that tool's own config types; `@nullvoxpopuli/ember-rolldown`
|
|
25
|
+
provides `ember()` (the defaults travel with the plugin, not `defineConfig`).
|
|
26
|
+
|
|
23
27
|
In your `tsdown.config.js` (recommended — emits declarations):
|
|
24
28
|
|
|
25
29
|
```js
|
|
26
|
-
import { defineConfig
|
|
30
|
+
import { defineConfig } from "tsdown";
|
|
31
|
+
import { ember } from "@nullvoxpopuli/ember-rolldown";
|
|
27
32
|
|
|
28
33
|
export default defineConfig({
|
|
29
34
|
entry: ["./src/index.ts"],
|
|
@@ -77,6 +82,27 @@ import type { TOC } from "@ember/component/template-only";
|
|
|
77
82
|
export const Badge: TOC<BadgeSignature> = <template>...</template>;
|
|
78
83
|
```
|
|
79
84
|
|
|
85
|
+
## CSS
|
|
86
|
+
|
|
87
|
+
Components that import co-located CSS (`import './popup.css'`) need
|
|
88
|
+
[`@tsdown/css`](https://www.npmjs.com/package/@tsdown/css) installed in your
|
|
89
|
+
library — tsdown auto-detects it and bundles every imported stylesheet into a
|
|
90
|
+
single CSS file in `dist/`. Install the version matching your `tsdown`
|
|
91
|
+
version (they're released in lockstep):
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
npm add --save-dev @tsdown/css
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Without it, tsdown's css-guard fails the build on the first CSS import — and
|
|
98
|
+
because the importing component module never loads, any declaration that
|
|
99
|
+
references that component dangles, surfacing as misleading
|
|
100
|
+
`UNLOADABLE_DEPENDENCY` errors on `<component>.d.ts` files.
|
|
101
|
+
|
|
102
|
+
Scoping/transforming the CSS itself (e.g.
|
|
103
|
+
[ember-scoped-css](https://github.com/soxhub/ember-scoped-css)) is separate
|
|
104
|
+
from bundling and runs via its own plugins.
|
|
105
|
+
|
|
80
106
|
## What `ember()` does
|
|
81
107
|
|
|
82
108
|
`ember()` returns an array of rolldown plugins:
|
|
@@ -109,6 +135,47 @@ ember({
|
|
|
109
135
|
|
|
110
136
|
Each option is documented on `BabelOptions`.
|
|
111
137
|
|
|
138
|
+
## App re-exports
|
|
139
|
+
|
|
140
|
+
For libraries whose modules must appear in the consuming app's namespace
|
|
141
|
+
(classic resolution: `{{a-component}}`, services and helpers looked up by
|
|
142
|
+
name), `appReexports()` does the same job as `@embroider/addon-dev`'s
|
|
143
|
+
`appReexports` rollup plugin. It's a separate import because most libraries
|
|
144
|
+
don't need it:
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import { defineConfig } from "tsdown";
|
|
148
|
+
import { ember } from "@nullvoxpopuli/ember-rolldown";
|
|
149
|
+
import { appReexports } from "@nullvoxpopuli/ember-rolldown/app-reexports";
|
|
150
|
+
|
|
151
|
+
export default defineConfig({
|
|
152
|
+
entry: ["./src/index.ts", "./src/services/session.ts"],
|
|
153
|
+
plugins: [ember(), appReexports()],
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
With no arguments, top-level services (`services/*`) are re-exported — under
|
|
158
|
+
strict mode, components and helpers are imported, but services are still
|
|
159
|
+
injected by name. A string argument is an include glob, and an object gives
|
|
160
|
+
full control:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
appReexports(); // services/*
|
|
164
|
+
appReexports("components/**"); // one include glob
|
|
165
|
+
appReexports({ include: ["services/*", "helpers/*"], exclude: [...] });
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
For every built file matching `include` (minus `exclude` and `.d.ts` files),
|
|
169
|
+
it writes a module under `dist/_app_/` re-exporting from the library's own
|
|
170
|
+
name, and records the set in `package.json` under `ember-addon.app-js`.
|
|
171
|
+
`mapFilename` renames a re-export; `exports` picks which bindings it forwards
|
|
172
|
+
(default `["default"]`).
|
|
173
|
+
|
|
174
|
+
Unlike the embroider plugin, nothing is written unless its content actually
|
|
175
|
+
differs from what is on disk — `_app_/` modules are compared byte-for-byte
|
|
176
|
+
and `package.json` is only rewritten when the `app-js` map itself changed —
|
|
177
|
+
so rebuilds don't re-trigger file watchers.
|
|
178
|
+
|
|
112
179
|
## Credit
|
|
113
180
|
|
|
114
181
|
The `emberExternals` and `emberTransform` plugins are derived from
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, extname, join, matchesGlob, relative, resolve, sep } from "node:path";
|
|
3
|
+
//#region src/app-reexports.ts
|
|
4
|
+
/**
|
|
5
|
+
* Merges built modules into the consuming app's namespace, so classic
|
|
6
|
+
* resolution (`{{a-component}}`, service/helper lookup by name) finds them --
|
|
7
|
+
* the same job as `@embroider/addon-dev`'s `appReexports` rollup plugin.
|
|
8
|
+
*
|
|
9
|
+
* For every built file matching `include` (minus `exclude` and `.d.ts`), it
|
|
10
|
+
* writes a tiny module under `<outDir>/_app_/` re-exporting from the
|
|
11
|
+
* library's own name, and records the full set in `package.json` under
|
|
12
|
+
* `ember-addon.app-js`.
|
|
13
|
+
*
|
|
14
|
+
* Unlike the embroider plugin, nothing is written unless its content actually
|
|
15
|
+
* differs from what is on disk: the `_app_/` modules are compared
|
|
16
|
+
* byte-for-byte, and `package.json` is only rewritten when the `app-js` map
|
|
17
|
+
* itself changed (so an equivalent map in different formatting is left
|
|
18
|
+
* alone). This keeps rebuilds from re-triggering file watchers.
|
|
19
|
+
*
|
|
20
|
+
* This is a separate import, because most libraries don't need it:
|
|
21
|
+
*
|
|
22
|
+
* ```js
|
|
23
|
+
* import { defineConfig } from "tsdown";
|
|
24
|
+
* import { ember } from "@nullvoxpopuli/ember-rolldown";
|
|
25
|
+
* import { appReexports } from "@nullvoxpopuli/ember-rolldown/app-reexports";
|
|
26
|
+
*
|
|
27
|
+
* export default defineConfig({
|
|
28
|
+
* entry: ["./src/index.ts", "./src/services/session.ts"],
|
|
29
|
+
* plugins: [ember(), appReexports()],
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* With no arguments, top-level services (`services/*`) are re-exported. A
|
|
34
|
+
* string argument is an include glob; an object gives full control:
|
|
35
|
+
*
|
|
36
|
+
* ```js
|
|
37
|
+
* appReexports(); // services/*
|
|
38
|
+
* appReexports("components/**"); // one include glob
|
|
39
|
+
* appReexports({ include: ["services/*", "helpers/*"], exclude: [...] });
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
function appReexports(options = {}) {
|
|
43
|
+
const resolved = typeof options === "string" ? { include: [options] } : options;
|
|
44
|
+
const include = resolved.include ?? ["services/*"];
|
|
45
|
+
return {
|
|
46
|
+
name: "ember:app-reexports",
|
|
47
|
+
writeBundle(outputOptions, bundle) {
|
|
48
|
+
const manifest = readJsonSync("package.json");
|
|
49
|
+
const outDir = outputOptions.dir ?? dirname(outputOptions.file ?? "dist");
|
|
50
|
+
const appJs = {};
|
|
51
|
+
for (const builtFilename of Object.keys(bundle).sort()) {
|
|
52
|
+
if (!isIncluded(builtFilename, include, resolved.exclude)) continue;
|
|
53
|
+
const appFilename = resolved.mapFilename?.(builtFilename) ?? builtFilename;
|
|
54
|
+
const names = resolved.exports?.(builtFilename) || ["default"];
|
|
55
|
+
const clause = typeof names === "string" ? names : `{ ${names.join(", ")} }`;
|
|
56
|
+
const withoutExtension = builtFilename.slice(0, -extname(builtFilename).length);
|
|
57
|
+
const reexportPath = join(outDir, "_app_", appFilename);
|
|
58
|
+
appJs[`./${appFilename}`] = `./${toPosix(relative(process.cwd(), resolve(reexportPath)))}`;
|
|
59
|
+
writeFileIfChanged(reexportPath, `export ${clause} from "${manifest.name}/${withoutExtension}";\n`);
|
|
60
|
+
}
|
|
61
|
+
if (!sameStringRecord(manifest["ember-addon"]?.["app-js"] ?? {}, appJs)) {
|
|
62
|
+
manifest["ember-addon"] = {
|
|
63
|
+
...manifest["ember-addon"],
|
|
64
|
+
"app-js": appJs
|
|
65
|
+
};
|
|
66
|
+
writeFileSync("package.json", JSON.stringify(manifest, null, 2) + "\n");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function readJsonSync(path) {
|
|
72
|
+
return JSON.parse(readFileSync(path, { encoding: "utf8" }));
|
|
73
|
+
}
|
|
74
|
+
function isIncluded(filename, include, exclude) {
|
|
75
|
+
if (matchesGlob(filename, "**/*.d.ts")) return false;
|
|
76
|
+
if (!include.some((glob) => matchesGlob(filename, glob))) return false;
|
|
77
|
+
if (exclude?.some((glob) => matchesGlob(filename, glob))) return false;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
function writeFileIfChanged(path, content) {
|
|
81
|
+
let existing;
|
|
82
|
+
try {
|
|
83
|
+
existing = readFileSync(path, { encoding: "utf8" });
|
|
84
|
+
} catch {}
|
|
85
|
+
if (existing === content) return;
|
|
86
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
87
|
+
writeFileSync(path, content);
|
|
88
|
+
}
|
|
89
|
+
function sameStringRecord(a, b) {
|
|
90
|
+
const aKeys = Object.keys(a);
|
|
91
|
+
return aKeys.length === Object.keys(b).length && aKeys.every((key) => a[key] === b[key]);
|
|
92
|
+
}
|
|
93
|
+
function toPosix(path) {
|
|
94
|
+
return path.split(sep).join("/");
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
export { appReexports };
|
package/dist/index.js
CHANGED
|
@@ -87,6 +87,60 @@ function emberBabel(options = {}) {
|
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
//#endregion
|
|
90
|
+
//#region src/config.ts
|
|
91
|
+
/**
|
|
92
|
+
* Sensible defaults for an Ember v2 library, applied through plugin hooks so
|
|
93
|
+
* they travel with `ember()` — you get them whether you use this package's
|
|
94
|
+
* `defineConfig`, tsdown's, or a plain `tsdown.config.js` / `rolldown.config.js`.
|
|
95
|
+
*
|
|
96
|
+
* Every value is applied with `??=`, so anything you set explicitly wins.
|
|
97
|
+
*
|
|
98
|
+
* Under tsdown, the full set applies via the `tsdownConfig` hook (analogous to
|
|
99
|
+
* Vite's `config` hook):
|
|
100
|
+
*
|
|
101
|
+
* - `sourcemap` — emit sourcemaps alongside the output.
|
|
102
|
+
* - `clean` — wipe `dist/` between builds.
|
|
103
|
+
* - `dts` — emit `.d.ts` declarations (via isolated declarations).
|
|
104
|
+
* - `outExtensions` — `.js`/`.d.ts` rather than tsdown's default `.mjs`/`.d.mts`,
|
|
105
|
+
* since exports maps conventionally point at `.js`/`.d.ts`.
|
|
106
|
+
* - `report` — off; the size report is noise for a library build.
|
|
107
|
+
* - `deps.neverBundle` — leave node builtins and the ember virtual packages to
|
|
108
|
+
* the consuming app (`emberExternals()` handles the rest).
|
|
109
|
+
*
|
|
110
|
+
* A plain rolldown build has no notion of `clean`/`dts`/`outExtensions`/`report`
|
|
111
|
+
* (those are tsdown-level concepts), so only the options with rolldown
|
|
112
|
+
* equivalents are applied there, via rolldown's own `outputOptions` hook:
|
|
113
|
+
*
|
|
114
|
+
* - `output.sourcemap` — on (output option).
|
|
115
|
+
*
|
|
116
|
+
* Externals are handled by `emberExternals()` (a `resolveId` hook) in both cases.
|
|
117
|
+
*/
|
|
118
|
+
function emberConfig() {
|
|
119
|
+
return {
|
|
120
|
+
name: "ember:config",
|
|
121
|
+
tsdownConfig(config) {
|
|
122
|
+
config.sourcemap ??= true;
|
|
123
|
+
config.clean ??= true;
|
|
124
|
+
config.dts ??= { sourcemap: true };
|
|
125
|
+
config.outExtensions ??= () => ({
|
|
126
|
+
js: ".js",
|
|
127
|
+
dts: ".d.ts"
|
|
128
|
+
});
|
|
129
|
+
config.report ??= false;
|
|
130
|
+
config.deps ??= {};
|
|
131
|
+
config.deps.neverBundle ??= [
|
|
132
|
+
"node:*",
|
|
133
|
+
"@ember/*",
|
|
134
|
+
"@glimmer/*"
|
|
135
|
+
];
|
|
136
|
+
},
|
|
137
|
+
outputOptions(options) {
|
|
138
|
+
options.sourcemap ??= true;
|
|
139
|
+
return options;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
//#endregion
|
|
90
144
|
//#region src/externals.ts
|
|
91
145
|
const { emberVirtualPackages, emberVirtualPeerDeps, packageName, templateCompilationModules } = pkg;
|
|
92
146
|
const compilationModules = new Set(templateCompilationModules.map((m) => m.module));
|
|
@@ -287,42 +341,6 @@ function emberTransform() {
|
|
|
287
341
|
};
|
|
288
342
|
}
|
|
289
343
|
//#endregion
|
|
290
|
-
//#region src/define-config.ts
|
|
291
|
-
/**
|
|
292
|
-
* tsdown's config, set up for an Ember v2 library: builds your entry to
|
|
293
|
-
* `dist/*.js` and `dist/*.d.ts` with sourcemaps, cleaning `dist/` between
|
|
294
|
-
* builds and leaving the ember virtual packages to the consuming app.
|
|
295
|
-
*
|
|
296
|
-
* You choose the `entry` and the `plugins` (e.g. `ember()`); any tsdown
|
|
297
|
-
* option you pass wins.
|
|
298
|
-
*
|
|
299
|
-
* ```js
|
|
300
|
-
* import { defineConfig, ember } from "@nullvoxpopuli/ember-rolldown";
|
|
301
|
-
*
|
|
302
|
-
* export default defineConfig({
|
|
303
|
-
* entry: ["./src/index.ts"],
|
|
304
|
-
* plugins: [ember()],
|
|
305
|
-
* });
|
|
306
|
-
* ```
|
|
307
|
-
*/
|
|
308
|
-
function defineConfig(config) {
|
|
309
|
-
return {
|
|
310
|
-
sourcemap: true,
|
|
311
|
-
clean: true,
|
|
312
|
-
dts: true,
|
|
313
|
-
outExtensions: () => ({
|
|
314
|
-
js: ".js",
|
|
315
|
-
dts: ".d.ts"
|
|
316
|
-
}),
|
|
317
|
-
neverBundle: [
|
|
318
|
-
"node:*",
|
|
319
|
-
"@ember/*",
|
|
320
|
-
"@glimmer/*"
|
|
321
|
-
],
|
|
322
|
-
...config
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
//#endregion
|
|
326
344
|
//#region index.ts
|
|
327
345
|
/**
|
|
328
346
|
* A batteries-included plugin for building Ember v2 libraries (addons) with
|
|
@@ -331,6 +349,9 @@ function defineConfig(config) {
|
|
|
331
349
|
* It bundles everything needed to compile `.gts`/`.gjs` and template-tag
|
|
332
350
|
* (`<template>`) source into publishable output:
|
|
333
351
|
*
|
|
352
|
+
* - `emberConfig()` — applies sensible tsdown defaults for a library build
|
|
353
|
+
* (sourcemaps, `clean`, `dts`, `.js`/`.d.ts` extensions, quiet logging);
|
|
354
|
+
* anything you set explicitly still wins.
|
|
334
355
|
* - `emberIsolatedDeclarations()` — errors when a tsconfig.json is present
|
|
335
356
|
* without `isolatedDeclarations: true` (required: it is the only
|
|
336
357
|
* declaration pipeline that can see compiled template-tag modules).
|
|
@@ -344,20 +365,22 @@ function defineConfig(config) {
|
|
|
344
365
|
* native transform. The library's own `babel.config.js` is used when it
|
|
345
366
|
* exists; no config file is required.
|
|
346
367
|
*
|
|
347
|
-
* Usage in `tsdown.config.js`
|
|
368
|
+
* Usage in `tsdown.config.js` (import `defineConfig` from `tsdown` — or from
|
|
369
|
+
* `rolldown` for a `rolldown.config.js` — so it carries the correct types):
|
|
348
370
|
*
|
|
349
371
|
* ```js
|
|
372
|
+
* import { defineConfig } from "tsdown";
|
|
350
373
|
* import { ember } from "@nullvoxpopuli/ember-rolldown";
|
|
351
374
|
*
|
|
352
375
|
* export default defineConfig({
|
|
353
376
|
* entry: ["./src/index.ts"],
|
|
354
|
-
* dts: true,
|
|
355
377
|
* plugins: [ember()],
|
|
356
378
|
* });
|
|
357
379
|
* ```
|
|
358
380
|
*/
|
|
359
381
|
function ember(config = {}) {
|
|
360
382
|
return [
|
|
383
|
+
emberConfig(),
|
|
361
384
|
emberIsolatedDeclarations(),
|
|
362
385
|
emberExternals(),
|
|
363
386
|
emberTransform(),
|
|
@@ -365,4 +388,4 @@ function ember(config = {}) {
|
|
|
365
388
|
];
|
|
366
389
|
}
|
|
367
390
|
//#endregion
|
|
368
|
-
export {
|
|
391
|
+
export { ember };
|
package/index.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type { Plugin } from "rolldown";
|
|
2
2
|
|
|
3
3
|
import { emberBabel, type BabelOptions } from "./src/babel.ts";
|
|
4
|
+
import { emberConfig } from "./src/config.ts";
|
|
4
5
|
import { emberExternals } from "./src/externals.ts";
|
|
5
6
|
import { emberIsolatedDeclarations } from "./src/isolated-declarations.ts";
|
|
6
7
|
import { emberTransform } from "./src/transform.ts";
|
|
7
8
|
|
|
8
|
-
export { defineConfig } from "./src/define-config.ts";
|
|
9
|
-
|
|
10
9
|
interface Config {
|
|
11
10
|
/**
|
|
12
11
|
* Options for the babel step; see `BabelOptions`.
|
|
@@ -21,6 +20,9 @@ interface Config {
|
|
|
21
20
|
* It bundles everything needed to compile `.gts`/`.gjs` and template-tag
|
|
22
21
|
* (`<template>`) source into publishable output:
|
|
23
22
|
*
|
|
23
|
+
* - `emberConfig()` — applies sensible tsdown defaults for a library build
|
|
24
|
+
* (sourcemaps, `clean`, `dts`, `.js`/`.d.ts` extensions, quiet logging);
|
|
25
|
+
* anything you set explicitly still wins.
|
|
24
26
|
* - `emberIsolatedDeclarations()` — errors when a tsconfig.json is present
|
|
25
27
|
* without `isolatedDeclarations: true` (required: it is the only
|
|
26
28
|
* declaration pipeline that can see compiled template-tag modules).
|
|
@@ -34,20 +36,22 @@ interface Config {
|
|
|
34
36
|
* native transform. The library's own `babel.config.js` is used when it
|
|
35
37
|
* exists; no config file is required.
|
|
36
38
|
*
|
|
37
|
-
* Usage in `tsdown.config.js`
|
|
39
|
+
* Usage in `tsdown.config.js` (import `defineConfig` from `tsdown` — or from
|
|
40
|
+
* `rolldown` for a `rolldown.config.js` — so it carries the correct types):
|
|
38
41
|
*
|
|
39
42
|
* ```js
|
|
43
|
+
* import { defineConfig } from "tsdown";
|
|
40
44
|
* import { ember } from "@nullvoxpopuli/ember-rolldown";
|
|
41
45
|
*
|
|
42
46
|
* export default defineConfig({
|
|
43
47
|
* entry: ["./src/index.ts"],
|
|
44
|
-
* dts: true,
|
|
45
48
|
* plugins: [ember()],
|
|
46
49
|
* });
|
|
47
50
|
* ```
|
|
48
51
|
*/
|
|
49
52
|
export function ember(config: Config = {}): Plugin[] {
|
|
50
53
|
return [
|
|
54
|
+
emberConfig(),
|
|
51
55
|
emberIsolatedDeclarations(),
|
|
52
56
|
emberExternals(),
|
|
53
57
|
emberTransform(),
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nullvoxpopuli/ember-rolldown",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.1.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",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/index.js",
|
|
9
|
+
"./app-reexports": "./dist/app-reexports.js"
|
|
10
|
+
},
|
|
7
11
|
"keywords": [
|
|
8
12
|
"rolldown",
|
|
9
13
|
"tsdown",
|
|
@@ -44,8 +48,9 @@
|
|
|
44
48
|
}
|
|
45
49
|
},
|
|
46
50
|
"devDependencies": {
|
|
47
|
-
"
|
|
48
|
-
"
|
|
51
|
+
"@tsdown/css": "^0.22.13",
|
|
52
|
+
"rolldown": "^1.2.0",
|
|
53
|
+
"tsdown": "^0.22.9",
|
|
49
54
|
"typescript": "^6.0.3"
|
|
50
55
|
},
|
|
51
56
|
"scripts": {
|
package/rolldown.config.js
CHANGED
|
@@ -8,11 +8,13 @@ const deps = [
|
|
|
8
8
|
];
|
|
9
9
|
|
|
10
10
|
export default defineConfig({
|
|
11
|
-
input:
|
|
11
|
+
input: {
|
|
12
|
+
index: "index.ts",
|
|
13
|
+
"app-reexports": "src/app-reexports.ts",
|
|
14
|
+
},
|
|
12
15
|
output: {
|
|
13
16
|
format: "esm",
|
|
14
|
-
|
|
15
|
-
codeSplitting: false,
|
|
17
|
+
dir: "dist",
|
|
16
18
|
},
|
|
17
19
|
external(id) {
|
|
18
20
|
if (id.startsWith("node:")) return true;
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, readFile, rm, stat, utimes, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { rolldown, type Plugin } from "rolldown";
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
7
|
+
|
|
8
|
+
import { appReexports, type AppReexportsOptions } from "./app-reexports.ts";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The plugin reads and writes package.json relative to the current working
|
|
12
|
+
* directory (matching the other plugins in this package), so each test runs
|
|
13
|
+
* inside its own temp project.
|
|
14
|
+
*/
|
|
15
|
+
describe("appReexports", () => {
|
|
16
|
+
let projectDir: string;
|
|
17
|
+
const originalCwd = process.cwd();
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
projectDir = await mkdtemp(join(tmpdir(), "app-reexports-"));
|
|
21
|
+
|
|
22
|
+
await writeFile(
|
|
23
|
+
join(projectDir, "package.json"),
|
|
24
|
+
JSON.stringify(
|
|
25
|
+
{
|
|
26
|
+
name: "my-lib",
|
|
27
|
+
version: "1.0.0",
|
|
28
|
+
"ember-addon": { version: 2, type: "addon" },
|
|
29
|
+
},
|
|
30
|
+
null,
|
|
31
|
+
2,
|
|
32
|
+
) + "\n",
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
await mkdir(join(projectDir, "src/components"), { recursive: true });
|
|
36
|
+
await mkdir(join(projectDir, "src/utils"), { recursive: true });
|
|
37
|
+
await writeFile(
|
|
38
|
+
join(projectDir, "src/components/foo.js"),
|
|
39
|
+
"export default function foo() {}\n",
|
|
40
|
+
);
|
|
41
|
+
await writeFile(
|
|
42
|
+
join(projectDir, "src/utils/math.js"),
|
|
43
|
+
"export function add(a, b) { return a + b; }\n",
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
process.chdir(projectDir);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
process.chdir(originalCwd);
|
|
51
|
+
await rm(projectDir, { recursive: true, force: true });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
async function build(options?: string | AppReexportsOptions) {
|
|
55
|
+
const bundle = await rolldown({
|
|
56
|
+
input: {
|
|
57
|
+
"components/foo": "./src/components/foo.js",
|
|
58
|
+
"utils/math": "./src/utils/math.js",
|
|
59
|
+
},
|
|
60
|
+
plugins: [appReexports(options)],
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await bundle.write({ dir: "dist" });
|
|
64
|
+
await bundle.close();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function readJson(path: string) {
|
|
68
|
+
return JSON.parse(await readFile(path, "utf8"));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
it("writes _app_ re-exports and records them in package.json", async () => {
|
|
72
|
+
await build({ include: ["components/**"] });
|
|
73
|
+
|
|
74
|
+
expect(await readFile("dist/_app_/components/foo.js", "utf8")).toMatchInlineSnapshot(`
|
|
75
|
+
"export { default } from "my-lib/components/foo";
|
|
76
|
+
"
|
|
77
|
+
`);
|
|
78
|
+
|
|
79
|
+
const manifest = await readJson("package.json");
|
|
80
|
+
|
|
81
|
+
expect(manifest["ember-addon"]).toMatchInlineSnapshot(`
|
|
82
|
+
{
|
|
83
|
+
"app-js": {
|
|
84
|
+
"./components/foo.js": "./dist/_app_/components/foo.js",
|
|
85
|
+
},
|
|
86
|
+
"type": "addon",
|
|
87
|
+
"version": 2,
|
|
88
|
+
}
|
|
89
|
+
`);
|
|
90
|
+
|
|
91
|
+
// utils/math.js didn't match include
|
|
92
|
+
await expect(stat("dist/_app_/utils/math.js")).rejects.toThrow();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("accepts a string argument as the include glob", async () => {
|
|
96
|
+
await build("components/**");
|
|
97
|
+
|
|
98
|
+
const manifest = await readJson("package.json");
|
|
99
|
+
|
|
100
|
+
expect(Object.keys(manifest["ember-addon"]["app-js"])).toEqual(["./components/foo.js"]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("defaults to top-level services", async () => {
|
|
104
|
+
await mkdir(join(projectDir, "src/services/nested"), { recursive: true });
|
|
105
|
+
await writeFile(join(projectDir, "src/services/session.js"), "export default class {}\n");
|
|
106
|
+
await writeFile(join(projectDir, "src/services/nested/deep.js"), "export default class {}\n");
|
|
107
|
+
|
|
108
|
+
const bundle = await rolldown({
|
|
109
|
+
input: {
|
|
110
|
+
"components/foo": "./src/components/foo.js",
|
|
111
|
+
"services/session": "./src/services/session.js",
|
|
112
|
+
"services/nested/deep": "./src/services/nested/deep.js",
|
|
113
|
+
},
|
|
114
|
+
plugins: [appReexports()],
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
await bundle.write({ dir: "dist" });
|
|
118
|
+
await bundle.close();
|
|
119
|
+
|
|
120
|
+
const manifest = await readJson("package.json");
|
|
121
|
+
|
|
122
|
+
// services/* is not deep: nested services (and everything else) stay out
|
|
123
|
+
expect(manifest["ember-addon"]["app-js"]).toEqual({
|
|
124
|
+
"./services/session.js": "./dist/_app_/services/session.js",
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("respects exclude", async () => {
|
|
129
|
+
await build({ include: ["**/*.js"], exclude: ["utils/**"] });
|
|
130
|
+
|
|
131
|
+
const manifest = await readJson("package.json");
|
|
132
|
+
|
|
133
|
+
expect(Object.keys(manifest["ember-addon"]["app-js"])).toEqual(["./components/foo.js"]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("supports mapFilename and custom exports", async () => {
|
|
137
|
+
await build({
|
|
138
|
+
include: ["utils/**"],
|
|
139
|
+
mapFilename: (name) => name.replace("math", "arithmetic"),
|
|
140
|
+
exports: () => ["add"],
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
expect(await readFile("dist/_app_/utils/arithmetic.js", "utf8")).toMatchInlineSnapshot(`
|
|
144
|
+
"export { add } from "my-lib/utils/math";
|
|
145
|
+
"
|
|
146
|
+
`);
|
|
147
|
+
|
|
148
|
+
const manifest = await readJson("package.json");
|
|
149
|
+
|
|
150
|
+
expect(manifest["ember-addon"]["app-js"]).toMatchInlineSnapshot(`
|
|
151
|
+
{
|
|
152
|
+
"./utils/arithmetic.js": "./dist/_app_/utils/arithmetic.js",
|
|
153
|
+
}
|
|
154
|
+
`);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("never re-exports .d.ts files", async () => {
|
|
158
|
+
const emitDeclaration: Plugin = {
|
|
159
|
+
name: "test:emit-declaration",
|
|
160
|
+
generateBundle() {
|
|
161
|
+
this.emitFile({
|
|
162
|
+
type: "asset",
|
|
163
|
+
fileName: "components/foo.d.ts",
|
|
164
|
+
source: "declare const foo: () => void;\nexport default foo;\n",
|
|
165
|
+
});
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const bundle = await rolldown({
|
|
170
|
+
input: { "components/foo": "./src/components/foo.js" },
|
|
171
|
+
plugins: [emitDeclaration, appReexports({ include: ["**/*"] })],
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
await bundle.write({ dir: "dist" });
|
|
175
|
+
await bundle.close();
|
|
176
|
+
|
|
177
|
+
const manifest = await readJson("package.json");
|
|
178
|
+
|
|
179
|
+
expect(Object.keys(manifest["ember-addon"]["app-js"])).toEqual(["./components/foo.js"]);
|
|
180
|
+
await expect(stat("dist/_app_/components/foo.d.ts")).rejects.toThrow();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("does not touch files whose content would be unchanged", async () => {
|
|
184
|
+
await build({ include: ["components/**"] });
|
|
185
|
+
|
|
186
|
+
const epoch = new Date(0);
|
|
187
|
+
|
|
188
|
+
await utimes("package.json", epoch, epoch);
|
|
189
|
+
await utimes("dist/_app_/components/foo.js", epoch, epoch);
|
|
190
|
+
|
|
191
|
+
await build({ include: ["components/**"] });
|
|
192
|
+
|
|
193
|
+
expect((await stat("package.json")).mtimeMs).toBe(0);
|
|
194
|
+
expect((await stat("dist/_app_/components/foo.js")).mtimeMs).toBe(0);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("leaves an equivalent package.json alone, regardless of formatting", async () => {
|
|
198
|
+
await build({ include: ["components/**"] });
|
|
199
|
+
|
|
200
|
+
// Same app-js map, different formatting: the plugin must not reformat.
|
|
201
|
+
const reformatted =
|
|
202
|
+
JSON.stringify(await readJson("package.json"), null, 4).replace(/\n/g, "\r\n") + "\r\n";
|
|
203
|
+
|
|
204
|
+
await writeFile("package.json", reformatted);
|
|
205
|
+
await build({ include: ["components/**"] });
|
|
206
|
+
|
|
207
|
+
expect(await readFile("package.json", "utf8")).toBe(reformatted);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("rewrites package.json when the app-js map changes", async () => {
|
|
211
|
+
await build({ include: ["components/**"] });
|
|
212
|
+
await build({ include: ["utils/**"] });
|
|
213
|
+
|
|
214
|
+
const manifest = await readJson("package.json");
|
|
215
|
+
|
|
216
|
+
expect(manifest["ember-addon"]["app-js"]).toEqual({
|
|
217
|
+
"./utils/math.js": "./dist/_app_/utils/math.js",
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, extname, join, matchesGlob, relative, resolve, sep } from "node:path";
|
|
3
|
+
|
|
4
|
+
import type { Plugin } from "rolldown";
|
|
5
|
+
|
|
6
|
+
export interface AppReexportsOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Globs, matched against each built filename (relative to the output
|
|
9
|
+
* directory), selecting which modules get an app re-export.
|
|
10
|
+
*
|
|
11
|
+
* Defaults to `["services/*"]` -- top-level services only. Under strict
|
|
12
|
+
* mode, components and helpers are imported, but services are still
|
|
13
|
+
* injected by name, so they are the one thing an app must always be able
|
|
14
|
+
* to resolve.
|
|
15
|
+
*
|
|
16
|
+
* `.d.ts` files never match.
|
|
17
|
+
*/
|
|
18
|
+
include?: string[];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Globs removing files that `include` matched.
|
|
22
|
+
*/
|
|
23
|
+
exclude?: string[];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Rename a re-export: receives the built filename, returns the filename the
|
|
27
|
+
* app sees. Defaults to the built filename unchanged.
|
|
28
|
+
*/
|
|
29
|
+
mapFilename?: (filename: string) => string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Which bindings a file's re-export module forwards: an array of names
|
|
33
|
+
* (default `["default"]`), or a raw export clause string such as `"*"`.
|
|
34
|
+
*/
|
|
35
|
+
exports?: (filename: string) => string[] | string | undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Merges built modules into the consuming app's namespace, so classic
|
|
40
|
+
* resolution (`{{a-component}}`, service/helper lookup by name) finds them --
|
|
41
|
+
* the same job as `@embroider/addon-dev`'s `appReexports` rollup plugin.
|
|
42
|
+
*
|
|
43
|
+
* For every built file matching `include` (minus `exclude` and `.d.ts`), it
|
|
44
|
+
* writes a tiny module under `<outDir>/_app_/` re-exporting from the
|
|
45
|
+
* library's own name, and records the full set in `package.json` under
|
|
46
|
+
* `ember-addon.app-js`.
|
|
47
|
+
*
|
|
48
|
+
* Unlike the embroider plugin, nothing is written unless its content actually
|
|
49
|
+
* differs from what is on disk: the `_app_/` modules are compared
|
|
50
|
+
* byte-for-byte, and `package.json` is only rewritten when the `app-js` map
|
|
51
|
+
* itself changed (so an equivalent map in different formatting is left
|
|
52
|
+
* alone). This keeps rebuilds from re-triggering file watchers.
|
|
53
|
+
*
|
|
54
|
+
* This is a separate import, because most libraries don't need it:
|
|
55
|
+
*
|
|
56
|
+
* ```js
|
|
57
|
+
* import { defineConfig } from "tsdown";
|
|
58
|
+
* import { ember } from "@nullvoxpopuli/ember-rolldown";
|
|
59
|
+
* import { appReexports } from "@nullvoxpopuli/ember-rolldown/app-reexports";
|
|
60
|
+
*
|
|
61
|
+
* export default defineConfig({
|
|
62
|
+
* entry: ["./src/index.ts", "./src/services/session.ts"],
|
|
63
|
+
* plugins: [ember(), appReexports()],
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* With no arguments, top-level services (`services/*`) are re-exported. A
|
|
68
|
+
* string argument is an include glob; an object gives full control:
|
|
69
|
+
*
|
|
70
|
+
* ```js
|
|
71
|
+
* appReexports(); // services/*
|
|
72
|
+
* appReexports("components/**"); // one include glob
|
|
73
|
+
* appReexports({ include: ["services/*", "helpers/*"], exclude: [...] });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function appReexports(options: string | AppReexportsOptions = {}): Plugin {
|
|
77
|
+
const resolved = typeof options === "string" ? { include: [options] } : options;
|
|
78
|
+
const include = resolved.include ?? ["services/*"];
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
name: "ember:app-reexports",
|
|
82
|
+
|
|
83
|
+
writeBundle(outputOptions, bundle) {
|
|
84
|
+
const manifest = readJsonSync("package.json");
|
|
85
|
+
const outDir = outputOptions.dir ?? dirname(outputOptions.file ?? "dist");
|
|
86
|
+
const appJs: Record<string, string> = {};
|
|
87
|
+
|
|
88
|
+
// Sorted so the app-js map (and therefore package.json) is stable
|
|
89
|
+
// across builds regardless of chunk emission order.
|
|
90
|
+
for (const builtFilename of Object.keys(bundle).sort()) {
|
|
91
|
+
if (!isIncluded(builtFilename, include, resolved.exclude)) continue;
|
|
92
|
+
|
|
93
|
+
const appFilename = resolved.mapFilename?.(builtFilename) ?? builtFilename;
|
|
94
|
+
const names = resolved.exports?.(builtFilename) || ["default"];
|
|
95
|
+
const clause = typeof names === "string" ? names : `{ ${names.join(", ")} }`;
|
|
96
|
+
const withoutExtension = builtFilename.slice(0, -extname(builtFilename).length);
|
|
97
|
+
const reexportPath = join(outDir, "_app_", appFilename);
|
|
98
|
+
|
|
99
|
+
appJs[`./${appFilename}`] = `./${toPosix(relative(process.cwd(), resolve(reexportPath)))}`;
|
|
100
|
+
|
|
101
|
+
writeFileIfChanged(
|
|
102
|
+
reexportPath,
|
|
103
|
+
`export ${clause} from "${manifest.name}/${withoutExtension}";\n`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const existing = manifest["ember-addon"]?.["app-js"] ?? {};
|
|
108
|
+
|
|
109
|
+
if (!sameStringRecord(existing, appJs)) {
|
|
110
|
+
manifest["ember-addon"] = { ...manifest["ember-addon"], "app-js": appJs };
|
|
111
|
+
writeFileSync("package.json", JSON.stringify(manifest, null, 2) + "\n");
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function readJsonSync(path: string) {
|
|
118
|
+
return JSON.parse(readFileSync(path, { encoding: "utf8" }));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function isIncluded(filename: string, include: string[], exclude?: string[]): boolean {
|
|
122
|
+
if (matchesGlob(filename, "**/*.d.ts")) return false;
|
|
123
|
+
if (!include.some((glob) => matchesGlob(filename, glob))) return false;
|
|
124
|
+
if (exclude?.some((glob) => matchesGlob(filename, glob))) return false;
|
|
125
|
+
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function writeFileIfChanged(path: string, content: string) {
|
|
130
|
+
let existing: string | undefined;
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
existing = readFileSync(path, { encoding: "utf8" });
|
|
134
|
+
} catch {
|
|
135
|
+
// Doesn't exist yet (or isn't readable) -- write it.
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (existing === content) return;
|
|
139
|
+
|
|
140
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
141
|
+
writeFileSync(path, content);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function sameStringRecord(a: Record<string, string>, b: Record<string, string>): boolean {
|
|
145
|
+
const aKeys = Object.keys(a);
|
|
146
|
+
|
|
147
|
+
return aKeys.length === Object.keys(b).length && aKeys.every((key) => a[key] === b[key]);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function toPosix(path: string): string {
|
|
151
|
+
return path.split(sep).join("/");
|
|
152
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { TsdownPlugin, UserConfig } from "tsdown";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sensible defaults for an Ember v2 library, applied through plugin hooks so
|
|
5
|
+
* they travel with `ember()` — you get them whether you use this package's
|
|
6
|
+
* `defineConfig`, tsdown's, or a plain `tsdown.config.js` / `rolldown.config.js`.
|
|
7
|
+
*
|
|
8
|
+
* Every value is applied with `??=`, so anything you set explicitly wins.
|
|
9
|
+
*
|
|
10
|
+
* Under tsdown, the full set applies via the `tsdownConfig` hook (analogous to
|
|
11
|
+
* Vite's `config` hook):
|
|
12
|
+
*
|
|
13
|
+
* - `sourcemap` — emit sourcemaps alongside the output.
|
|
14
|
+
* - `clean` — wipe `dist/` between builds.
|
|
15
|
+
* - `dts` — emit `.d.ts` declarations (via isolated declarations).
|
|
16
|
+
* - `outExtensions` — `.js`/`.d.ts` rather than tsdown's default `.mjs`/`.d.mts`,
|
|
17
|
+
* since exports maps conventionally point at `.js`/`.d.ts`.
|
|
18
|
+
* - `report` — off; the size report is noise for a library build.
|
|
19
|
+
* - `deps.neverBundle` — leave node builtins and the ember virtual packages to
|
|
20
|
+
* the consuming app (`emberExternals()` handles the rest).
|
|
21
|
+
*
|
|
22
|
+
* A plain rolldown build has no notion of `clean`/`dts`/`outExtensions`/`report`
|
|
23
|
+
* (those are tsdown-level concepts), so only the options with rolldown
|
|
24
|
+
* equivalents are applied there, via rolldown's own `outputOptions` hook:
|
|
25
|
+
*
|
|
26
|
+
* - `output.sourcemap` — on (output option).
|
|
27
|
+
*
|
|
28
|
+
* Externals are handled by `emberExternals()` (a `resolveId` hook) in both cases.
|
|
29
|
+
*/
|
|
30
|
+
export function emberConfig(): TsdownPlugin {
|
|
31
|
+
return {
|
|
32
|
+
name: "ember:config",
|
|
33
|
+
|
|
34
|
+
tsdownConfig(config: UserConfig) {
|
|
35
|
+
config.sourcemap ??= true;
|
|
36
|
+
config.clean ??= true;
|
|
37
|
+
config.dts ??= { sourcemap: true };
|
|
38
|
+
config.outExtensions ??= () => ({ js: ".js", dts: ".d.ts" });
|
|
39
|
+
config.report ??= false;
|
|
40
|
+
|
|
41
|
+
config.deps ??= {};
|
|
42
|
+
config.deps.neverBundle ??= ["node:*", "@ember/*", "@glimmer/*"];
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
outputOptions(options) {
|
|
46
|
+
options.sourcemap ??= true;
|
|
47
|
+
return options;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
package/src/dts.test.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { mkdtemp, mkdir, readdir, readFile, 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
|
+
/**
|
|
10
|
+
* Drives `ember()` through a real tsdown build (the dts pipeline included).
|
|
11
|
+
* `files` is a map of relative path -> source, written into a temp dir that
|
|
12
|
+
* becomes the cwd for the build (the externals and isolated-declarations
|
|
13
|
+
* plugins read package.json / tsconfig.json from cwd, like the tsdown CLI).
|
|
14
|
+
*/
|
|
15
|
+
async function buildFixture(files: Record<string, string>, entry: string[]): Promise<string> {
|
|
16
|
+
const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-dts-"));
|
|
17
|
+
|
|
18
|
+
const withDefaults: Record<string, string> = {
|
|
19
|
+
"package.json": JSON.stringify({
|
|
20
|
+
name: "fixture",
|
|
21
|
+
version: "0.0.0",
|
|
22
|
+
type: "module",
|
|
23
|
+
exports: { "./*": { types: "./dist/*.d.ts", default: "./dist/*.js" } },
|
|
24
|
+
}),
|
|
25
|
+
"tsconfig.json": JSON.stringify({
|
|
26
|
+
compilerOptions: { isolatedDeclarations: true, declaration: true },
|
|
27
|
+
}),
|
|
28
|
+
...files,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
for (const [relative, source] of Object.entries(withDefaults)) {
|
|
32
|
+
const full = path.join(dir, relative);
|
|
33
|
+
await mkdir(path.dirname(full), { recursive: true });
|
|
34
|
+
await writeFile(full, source, "utf8");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const previousCwd = process.cwd();
|
|
38
|
+
process.chdir(dir);
|
|
39
|
+
restoreCwd = () => process.chdir(previousCwd);
|
|
40
|
+
|
|
41
|
+
await build({
|
|
42
|
+
entry,
|
|
43
|
+
config: false,
|
|
44
|
+
logLevel: "silent",
|
|
45
|
+
plugins: [ember()],
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return dir;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let restoreCwd: (() => void) | undefined;
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
restoreCwd?.();
|
|
55
|
+
restoreCwd = undefined;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
async function listFiles(dir: string): Promise<string[]> {
|
|
59
|
+
const out: string[] = [];
|
|
60
|
+
for (const entry of await readdir(dir, { withFileTypes: true, recursive: true })) {
|
|
61
|
+
if (entry.isFile()) {
|
|
62
|
+
out.push(path.relative(dir, path.join(entry.parentPath, entry.name)));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return out.sort();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
describe("declarations (full ember() via tsdown)", () => {
|
|
69
|
+
it("emits .d.ts for an entry that type-imports from a non-entry .gts", async () => {
|
|
70
|
+
// Mirrors the luna-core shape: the component is in the runtime graph via
|
|
71
|
+
// the index entry, while a second entry (a service) only *type*-imports
|
|
72
|
+
// from it. The service's declaration then references the component's
|
|
73
|
+
// (virtual) .d.ts, which only exists in the bundler's module graph.
|
|
74
|
+
const dir = await buildFixture(
|
|
75
|
+
{
|
|
76
|
+
"src/index.ts": `export { default as Popup } from './popup.gts';`,
|
|
77
|
+
"src/service.ts": [
|
|
78
|
+
`import type { PopupBlock } from './popup.gts';`,
|
|
79
|
+
``,
|
|
80
|
+
`export class TooltipService {`,
|
|
81
|
+
` registerPopup(context: PopupBlock): void {`,
|
|
82
|
+
` void context;`,
|
|
83
|
+
` }`,
|
|
84
|
+
`}`,
|
|
85
|
+
].join("\n"),
|
|
86
|
+
"src/popup.gts": [
|
|
87
|
+
`import Component from '@glimmer/component';`,
|
|
88
|
+
``,
|
|
89
|
+
`export interface PopupBlock {`,
|
|
90
|
+
` open: () => void;`,
|
|
91
|
+
`}`,
|
|
92
|
+
``,
|
|
93
|
+
`export interface PopupSignature {`,
|
|
94
|
+
` Element: HTMLDivElement;`,
|
|
95
|
+
` Blocks: { default: [PopupBlock] };`,
|
|
96
|
+
`}`,
|
|
97
|
+
``,
|
|
98
|
+
`export default class Popup extends Component<PopupSignature> {`,
|
|
99
|
+
` <template>popup</template>`,
|
|
100
|
+
`}`,
|
|
101
|
+
].join("\n"),
|
|
102
|
+
},
|
|
103
|
+
["./src/index.ts", "./src/service.ts"],
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const dist = await listFiles(path.join(dir, "dist"));
|
|
107
|
+
|
|
108
|
+
expect(dist).toContain("index.d.ts");
|
|
109
|
+
expect(dist).toContain("service.d.ts");
|
|
110
|
+
|
|
111
|
+
const serviceDts = await readFile(path.join(dir, "dist/service.d.ts"), "utf8");
|
|
112
|
+
|
|
113
|
+
// The PopupBlock type must be reachable: either inlined into the bundled
|
|
114
|
+
// declaration or imported via a specifier that exists in dist.
|
|
115
|
+
expect(serviceDts).toContain("PopupBlock");
|
|
116
|
+
expect(serviceDts).not.toContain(".gts");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("bundles co-located CSS (via @tsdown/css) without breaking declarations", async () => {
|
|
120
|
+
// Ember components routinely import a co-located .css file. Without CSS
|
|
121
|
+
// handling the build dies on tsdown's css-guard — and because the failing
|
|
122
|
+
// component module never loads, every declaration that references it
|
|
123
|
+
// dangles (surfacing as misleading UNLOADABLE_DEPENDENCY errors on
|
|
124
|
+
// <component>.d.ts). @tsdown/css (this package's peer) makes both work.
|
|
125
|
+
const dir = await buildFixture(
|
|
126
|
+
{
|
|
127
|
+
"src/index.ts": `export { default as Popup } from './popup.gts';`,
|
|
128
|
+
"src/service.ts": [
|
|
129
|
+
`import type { PopupBlock } from './popup.gts';`,
|
|
130
|
+
``,
|
|
131
|
+
`export function register(context: PopupBlock): void {`,
|
|
132
|
+
` void context;`,
|
|
133
|
+
`}`,
|
|
134
|
+
].join("\n"),
|
|
135
|
+
"src/popup.css": `.popup { color: red; }`,
|
|
136
|
+
"src/popup.gts": [
|
|
137
|
+
`import './popup.css';`,
|
|
138
|
+
`import Component from '@glimmer/component';`,
|
|
139
|
+
``,
|
|
140
|
+
`export interface PopupBlock {`,
|
|
141
|
+
` open: () => void;`,
|
|
142
|
+
`}`,
|
|
143
|
+
``,
|
|
144
|
+
`export default class Popup extends Component {`,
|
|
145
|
+
` <template>popup</template>`,
|
|
146
|
+
`}`,
|
|
147
|
+
].join("\n"),
|
|
148
|
+
},
|
|
149
|
+
["./src/index.ts", "./src/service.ts"],
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const dist = await listFiles(path.join(dir, "dist"));
|
|
153
|
+
|
|
154
|
+
const cssFiles = dist.filter((file) => file.endsWith(".css"));
|
|
155
|
+
expect(cssFiles).toHaveLength(1);
|
|
156
|
+
|
|
157
|
+
const css = await readFile(path.join(dir, "dist", cssFiles[0]!), "utf8");
|
|
158
|
+
expect(css).toContain(".popup");
|
|
159
|
+
|
|
160
|
+
// The cascade regression: the css-importing component's declaration must
|
|
161
|
+
// still be generated and reachable from the service's declaration.
|
|
162
|
+
const serviceDts = await readFile(path.join(dir, "dist/service.d.ts"), "utf8");
|
|
163
|
+
expect(serviceDts).toContain("PopupBlock");
|
|
164
|
+
expect(serviceDts).not.toContain(".gts");
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -0,0 +1,173 @@
|
|
|
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 { describe, expect, it } from "vitest";
|
|
6
|
+
|
|
7
|
+
import { emberTransform } from "./transform.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Drives the plugin through a real rolldown build. `files` is a map of
|
|
11
|
+
* relative path -> source; the build entry is `index.ts`. Every bare (package)
|
|
12
|
+
* specifier is marked external so the build only exercises the local
|
|
13
|
+
* `.gts`/`.gjs`/`.ts` handling and doesn't need real dependencies installed.
|
|
14
|
+
* Returns the concatenated code of every emitted chunk.
|
|
15
|
+
*/
|
|
16
|
+
async function bundle(files: Record<string, string>): Promise<string> {
|
|
17
|
+
const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-build-"));
|
|
18
|
+
|
|
19
|
+
for (const [relative, source] of Object.entries(files)) {
|
|
20
|
+
const full = path.join(dir, relative);
|
|
21
|
+
await mkdir(path.dirname(full), { recursive: true });
|
|
22
|
+
await writeFile(full, source, "utf8");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const isLocal = (id: string) => id.startsWith(".") || id.startsWith("/") || path.isAbsolute(id);
|
|
26
|
+
|
|
27
|
+
const build = await rolldown({
|
|
28
|
+
input: path.join(dir, "index.ts"),
|
|
29
|
+
plugins: [emberTransform()],
|
|
30
|
+
external: (id) => !isLocal(id),
|
|
31
|
+
onwarn() {},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const { output } = await build.generate({ format: "es" });
|
|
35
|
+
|
|
36
|
+
const code = output
|
|
37
|
+
.filter((chunk): chunk is typeof chunk & { code: string } => "code" in chunk)
|
|
38
|
+
.map((chunk) => chunk.code)
|
|
39
|
+
.join("\n");
|
|
40
|
+
|
|
41
|
+
// rolldown's `//#region <path>` comments reference the random temp dir;
|
|
42
|
+
// collapse them to the bare filename so snapshots stay deterministic.
|
|
43
|
+
return code.replace(/(\/\/#region ).*\/([^/\n]+)$/gm, "$1$2");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("emberTransform (full plugin via rolldown)", () => {
|
|
47
|
+
it("compiles a .gts module's <template> and keeps bare imports external", async () => {
|
|
48
|
+
const code = await bundle({
|
|
49
|
+
"index.ts": `export { default as Foo } from './foo.gts';`,
|
|
50
|
+
"foo.gts": [
|
|
51
|
+
`import Component from '@glimmer/component';`,
|
|
52
|
+
`export default class Foo extends Component {`,
|
|
53
|
+
` <template>Hello</template>`,
|
|
54
|
+
`}`,
|
|
55
|
+
].join("\n"),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// <template> is gone, replaced by a content-tag `template(...)` call.
|
|
59
|
+
expect(code).not.toContain("<template>");
|
|
60
|
+
expect(code).toContain("template(");
|
|
61
|
+
// The bare import survived as an external dependency.
|
|
62
|
+
expect(code).toContain(`from "@glimmer/component"`);
|
|
63
|
+
// ...and it pulls in the template-compiler runtime import.
|
|
64
|
+
expect(code).toContain("@ember/template-compiler");
|
|
65
|
+
|
|
66
|
+
expect(code).toMatchInlineSnapshot(`
|
|
67
|
+
"import { template } from "@ember/template-compiler";
|
|
68
|
+
import Component from "@glimmer/component";
|
|
69
|
+
//#region foo.ts
|
|
70
|
+
var Foo = class extends Component {
|
|
71
|
+
static {
|
|
72
|
+
template(\`Hello\`, {
|
|
73
|
+
component: this,
|
|
74
|
+
eval() {
|
|
75
|
+
return eval(arguments[0]);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
//#endregion
|
|
81
|
+
export { Foo };
|
|
82
|
+
"
|
|
83
|
+
`);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("compiles .gjs alongside .gts and plain .ts in one graph", async () => {
|
|
87
|
+
const code = await bundle({
|
|
88
|
+
"index.ts": [
|
|
89
|
+
`export { default as Foo } from './foo.gts';`,
|
|
90
|
+
`export { greet } from './util.gjs';`,
|
|
91
|
+
`export { helper } from './plain.ts';`,
|
|
92
|
+
].join("\n"),
|
|
93
|
+
"foo.gts": `<template>Foo</template>`,
|
|
94
|
+
"util.gjs": [
|
|
95
|
+
`export function greet() { return 'hi'; }`,
|
|
96
|
+
`<template>{{greet}}</template>`,
|
|
97
|
+
].join("\n"),
|
|
98
|
+
"plain.ts": `export function helper() { return 1; }`,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(code).not.toContain("<template>");
|
|
102
|
+
expect(code).toContain("function greet()");
|
|
103
|
+
expect(code).toContain("function helper()");
|
|
104
|
+
expect(code).toContain("export {");
|
|
105
|
+
|
|
106
|
+
expect(code).toMatchInlineSnapshot(`
|
|
107
|
+
"import { template } from "@ember/template-compiler";
|
|
108
|
+
//#region foo.ts
|
|
109
|
+
var foo_default = template(\`Foo\`, { eval() {
|
|
110
|
+
return eval(arguments[0]);
|
|
111
|
+
} });
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region util.js
|
|
114
|
+
function greet() {
|
|
115
|
+
return "hi";
|
|
116
|
+
}
|
|
117
|
+
var util_default = template(\`{{greet}}\`, { eval() {
|
|
118
|
+
return eval(arguments[0]);
|
|
119
|
+
} });
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region plain.ts
|
|
122
|
+
function helper() {
|
|
123
|
+
return 1;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
export { foo_default as Foo, greet, helper };
|
|
127
|
+
"
|
|
128
|
+
`);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("resolves a .gts module that imports another .gts relatively", async () => {
|
|
132
|
+
const code = await bundle({
|
|
133
|
+
"index.ts": `export { default as Page } from './page.gts';`,
|
|
134
|
+
"page.gts": [
|
|
135
|
+
`import Widget from './widget.gts';`,
|
|
136
|
+
`export default class Page {`,
|
|
137
|
+
` Widget = Widget;`,
|
|
138
|
+
` <template><Widget /></template>`,
|
|
139
|
+
`}`,
|
|
140
|
+
].join("\n"),
|
|
141
|
+
"widget.gts": `<template>widget</template>`,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Both templates compiled, and the relative .gts import resolved (bundled).
|
|
145
|
+
expect(code).not.toContain("<template>");
|
|
146
|
+
expect(code).not.toContain(".gts");
|
|
147
|
+
expect(code).toContain("template(");
|
|
148
|
+
|
|
149
|
+
expect(code).toMatchInlineSnapshot(`
|
|
150
|
+
"import { template } from "@ember/template-compiler";
|
|
151
|
+
//#region widget.ts
|
|
152
|
+
var widget_default = template(\`widget\`, { eval() {
|
|
153
|
+
return eval(arguments[0]);
|
|
154
|
+
} });
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region page.ts
|
|
157
|
+
var Page = class {
|
|
158
|
+
Widget = widget_default;
|
|
159
|
+
static {
|
|
160
|
+
template(\`<Widget />\`, {
|
|
161
|
+
component: this,
|
|
162
|
+
eval() {
|
|
163
|
+
return eval(arguments[0]);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
//#endregion
|
|
169
|
+
export { Page };
|
|
170
|
+
"
|
|
171
|
+
`);
|
|
172
|
+
});
|
|
173
|
+
});
|
package/src/define-config.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { UserConfig } from "tsdown";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* tsdown's config, set up for an Ember v2 library: builds your entry to
|
|
5
|
-
* `dist/*.js` and `dist/*.d.ts` with sourcemaps, cleaning `dist/` between
|
|
6
|
-
* builds and leaving the ember virtual packages to the consuming app.
|
|
7
|
-
*
|
|
8
|
-
* You choose the `entry` and the `plugins` (e.g. `ember()`); any tsdown
|
|
9
|
-
* option you pass wins.
|
|
10
|
-
*
|
|
11
|
-
* ```js
|
|
12
|
-
* import { defineConfig, ember } from "@nullvoxpopuli/ember-rolldown";
|
|
13
|
-
*
|
|
14
|
-
* export default defineConfig({
|
|
15
|
-
* entry: ["./src/index.ts"],
|
|
16
|
-
* plugins: [ember()],
|
|
17
|
-
* });
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export function defineConfig(config: UserConfig): UserConfig {
|
|
21
|
-
return {
|
|
22
|
-
sourcemap: true,
|
|
23
|
-
clean: true,
|
|
24
|
-
dts: true,
|
|
25
|
-
// tsdown's extensions are .mjs/.d.mts otherwise, even in type-module
|
|
26
|
-
// packages; exports maps conventionally point at .js/.d.ts
|
|
27
|
-
outExtensions: () => ({ js: ".js", dts: ".d.ts" }),
|
|
28
|
-
// provided by the consuming app
|
|
29
|
-
neverBundle: ["node:*", "@ember/*", "@glimmer/*"],
|
|
30
|
-
...config,
|
|
31
|
-
};
|
|
32
|
-
}
|