@nullvoxpopuli/ember-rolldown 2.3.0 → 2.4.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
@@ -58,6 +58,25 @@ A `babel.config.js` is optional. Without one, `ember()` compiles templates
58
58
  [decorator-transforms](https://github.com/ef4/decorator-transforms)), and
59
59
  TypeScript; with one, your config runs instead.
60
60
 
61
+ ### Entrypoints
62
+
63
+ Entries may be any extension — `.ts`, `.js`, `.gts`, `.gjs`:
64
+
65
+ ```js
66
+ export default defineConfig({
67
+ entry: ["./src/index.ts", "./src/components/menu.gts"],
68
+ plugins: [ember()],
69
+ });
70
+ ```
71
+
72
+ Whatever the source extension, the emitted `.js` and `.d.ts` paths mirror the
73
+ entry paths (that's how tsdown's dts support works): the example emits
74
+ `dist/index.js` + `dist/index.d.ts` and `dist/components/menu.js` +
75
+ `dist/components/menu.d.ts`. Type imports resolve against those emitted
76
+ paths, so your entries — together with your `exports` map — _are_ your
77
+ public API: a module that isn't an entry has no stable `dist/` path of its
78
+ own and is only reachable through the entrypoints that re-export it.
79
+
61
80
  ### Declarations
62
81
 
63
82
  Declarations are emitted with isolated declarations — the only declaration
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 } from "node:fs";
6
+ import { existsSync, readFileSync, realpathSync } 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";
@@ -306,7 +306,15 @@ function emberTransform() {
306
306
  resolveId: {
307
307
  order: "pre",
308
308
  handler(id, importer) {
309
- if (!importer) return null;
309
+ if (!importer) {
310
+ if (!id.endsWith(".gts") && !id.endsWith(".gjs")) return null;
311
+ if (!existsSync(id)) return null;
312
+ const fileName = realpathSync(path.resolve(id));
313
+ return {
314
+ id: fileName.replace(/\.gts$/, ".ts").replace(/\.gjs$/, ".js"),
315
+ meta: { fileName }
316
+ };
317
+ }
310
318
  const fileName = path.join(path.dirname(importer), id);
311
319
  if (id.endsWith(".gts")) return {
312
320
  id: fileName.replace(/\.gts$/, ".ts"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nullvoxpopuli/ember-rolldown",
3
- "version": "2.3.0",
3
+ "version": "2.4.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",
@@ -13,7 +13,10 @@ import { emberTransform } from "./transform.ts";
13
13
  * `.gts`/`.gjs`/`.ts` handling and doesn't need real dependencies installed.
14
14
  * Returns the concatenated code of every emitted chunk.
15
15
  */
16
- async function bundle(files: Record<string, string>): Promise<string> {
16
+ async function bundle(
17
+ files: Record<string, string>,
18
+ { input = ["index.ts"] }: { input?: string[] } = {},
19
+ ): Promise<string> {
17
20
  const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-build-"));
18
21
 
19
22
  for (const [relative, source] of Object.entries(files)) {
@@ -25,7 +28,7 @@ async function bundle(files: Record<string, string>): Promise<string> {
25
28
  const isLocal = (id: string) => id.startsWith(".") || id.startsWith("/") || path.isAbsolute(id);
26
29
 
27
30
  const build = await rolldown({
28
- input: path.join(dir, "index.ts"),
31
+ input: input.map((relative) => path.join(dir, relative)),
29
32
  plugins: [emberTransform()],
30
33
  external: (id) => !isLocal(id),
31
34
  onwarn() {},
@@ -171,6 +174,54 @@ describe("emberTransform (full plugin via rolldown)", () => {
171
174
  `);
172
175
  });
173
176
 
177
+ it("compiles a .gts module used directly as an entry (no importer)", async () => {
178
+ const code = await bundle(
179
+ {
180
+ // Both an entry .gts and a .ts entry importing it: the shared module
181
+ // must resolve to ONE id (the entry resolution realpaths, matching
182
+ // rolldown's own resolver) so it lands in the entry chunk and the
183
+ // index chunk imports it, rather than duplicating the code.
184
+ "index.ts": `export { default as Foo } from './foo.gts';`,
185
+ "foo.gts": [
186
+ `import Component from '@glimmer/component';`,
187
+ `export default class Foo extends Component {`,
188
+ ` <template>Entry</template>`,
189
+ `}`,
190
+ ].join("\n"),
191
+ },
192
+ { input: ["index.ts", "foo.gts"] },
193
+ );
194
+
195
+ expect(code).not.toContain("<template>");
196
+ expect(code).toContain("template(");
197
+ expect(code).toContain(`from "@glimmer/component"`);
198
+ // deduplicated: the class body appears exactly once, in foo's own chunk
199
+ expect(code.match(/extends Component/g)).toHaveLength(1);
200
+ expect(code).toContain(`from "./foo.js"`);
201
+
202
+ expect(code).toMatchInlineSnapshot(`
203
+ "import { template } from "@ember/template-compiler";
204
+ import Component from "@glimmer/component";
205
+ //#region foo.ts
206
+ var Foo = class extends Component {
207
+ static {
208
+ template(\`Entry\`, {
209
+ component: this,
210
+ eval() {
211
+ return eval(arguments[0]);
212
+ }
213
+ });
214
+ }
215
+ };
216
+ //#endregion
217
+ export { Foo as default };
218
+
219
+ import Foo from "./foo.js";
220
+ export { Foo };
221
+ "
222
+ `);
223
+ });
224
+
174
225
  it("emits sourcemaps for the specifier rewrite (no SOURCEMAP_BROKEN warnings)", async () => {
175
226
  const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-map-"));
176
227
 
package/src/transform.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Preprocessor } from "content-tag";
2
- import { existsSync } from "node:fs";
2
+ import { existsSync, realpathSync } from "node:fs";
3
3
  import { readFile, writeFile, readdir } from "node:fs/promises";
4
4
  import path from "node:path";
5
5
  import type { Plugin } from "rolldown";
@@ -70,7 +70,28 @@ export function emberTransform(): Plugin {
70
70
  resolveId: {
71
71
  order: "pre",
72
72
  handler(id, importer) {
73
- if (!importer) return null;
73
+ // Entries have no importer: their ids come straight from the build
74
+ // config (tsdown expands entry globs to on-disk paths). Resolve a
75
+ // `.gts`/`.gjs` entry to the same virtual `.ts`/`.js` id an imported
76
+ // module gets, so the load hook compiles it via content-tag instead
77
+ // of the raw `<template>` source hitting the parser. Entries may be
78
+ // any extension — the emitted `.js`/`.d.ts` paths mirror the entry
79
+ // paths either way (that's how tsdown's dts support works).
80
+ if (!importer) {
81
+ if (!id.endsWith(".gts") && !id.endsWith(".gjs")) return null;
82
+ if (!existsSync(id)) return null;
83
+
84
+ // rolldown's default resolver realpaths entry ids (e.g. macOS
85
+ // /var -> /private/var). Match it, or this same file imported from
86
+ // another entry resolves to a second module id and gets duplicated
87
+ // into both chunks.
88
+ const fileName = realpathSync(path.resolve(id));
89
+
90
+ return {
91
+ id: fileName.replace(/\.gts$/, ".ts").replace(/\.gjs$/, ".js"),
92
+ meta: { fileName },
93
+ };
94
+ }
74
95
 
75
96
  const fileName = path.join(path.dirname(importer), id);
76
97