@nullvoxpopuli/ember-rolldown 2.2.0 → 2.3.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
@@ -194,12 +194,14 @@ export default defineConfig({
194
194
 
195
195
  With no arguments, top-level services (`services/*`) are re-exported — under
196
196
  strict mode, components and helpers are imported, but services are still
197
- injected by name. A string argument is an include glob, and an object gives
198
- full control:
197
+ injected by name. A string or array of strings is the include glob(s),
198
+ optionally followed by the remaining options; an object gives full control:
199
199
 
200
200
  ```js
201
201
  appReexports(); // services/*
202
202
  appReexports("components/**"); // one include glob
203
+ appReexports(["services/*", "helpers/*"]);
204
+ appReexports("components/**", { exclude: ["components/-private/**"] });
203
205
  appReexports({ include: ["services/*", "helpers/*"], exclude: [...] });
204
206
  ```
205
207
 
@@ -31,16 +31,25 @@ import { dirname, extname, join, matchesGlob, relative, resolve, sep } from "nod
31
31
  * ```
32
32
  *
33
33
  * With no arguments, top-level services (`services/*`) are re-exported. A
34
- * string argument is an include glob; an object gives full control:
34
+ * string or array of strings is the include glob(s), optionally followed by
35
+ * the remaining options; an object gives full control:
35
36
  *
36
37
  * ```js
37
38
  * appReexports(); // services/*
38
39
  * appReexports("components/**"); // one include glob
40
+ * appReexports(["services/*", "helpers/*"]);
41
+ * appReexports("components/**", { exclude: ["components/-private/**"] });
39
42
  * appReexports({ include: ["services/*", "helpers/*"], exclude: [...] });
40
43
  * ```
41
44
  */
42
- function appReexports(options = {}) {
43
- const resolved = typeof options === "string" ? { include: [options] } : options;
45
+ function appReexports(includeOrOptions = {}, options = {}) {
46
+ const resolved = typeof includeOrOptions === "string" ? {
47
+ ...options,
48
+ include: [includeOrOptions]
49
+ } : Array.isArray(includeOrOptions) ? {
50
+ ...options,
51
+ include: includeOrOptions
52
+ } : includeOrOptions;
44
53
  const include = resolved.include ?? ["services/*"];
45
54
  return {
46
55
  name: "ember:app-reexports",
package/dist/index.js CHANGED
@@ -4,8 +4,9 @@ import templateCompilation from "babel-plugin-ember-template-compilation";
4
4
  import decoratorTransforms from "decorator-transforms";
5
5
  import { loadPartialConfigSync } from "@babel/core";
6
6
  import { existsSync, readFileSync } from "node:fs";
7
- import pkg from "@embroider/core";
7
+ import { createRequire } from "node:module";
8
8
  import path, { dirname, resolve } from "node:path";
9
+ import pkg from "@embroider/core";
9
10
  import { Preprocessor } from "content-tag";
10
11
  import { readFile, readdir, writeFile } from "node:fs/promises";
11
12
  //#region src/babel.ts
@@ -168,6 +169,27 @@ function resolvableDependencies() {
168
169
  return deps;
169
170
  }
170
171
  /**
172
+ * The module names ember-source provides by renaming them into itself — the
173
+ * keys of its `ember-addon.renamed-modules`, normalized from file paths to
174
+ * import specifiers (`@glimmer/runtime/index.js` → `@glimmer/runtime`).
175
+ *
176
+ * This is the authoritative list of ember's provided modules, including
177
+ * private API that @embroider/core's emberVirtualPackages doesn't cover
178
+ * (e.g. `@glimmer/runtime`). It deliberately does NOT include real packages
179
+ * like `@glimmer/component`, which a library may want bundled.
180
+ *
181
+ * Resolved from the library's own dependency graph; empty when ember-source
182
+ * isn't resolvable there.
183
+ */
184
+ function emberSourceRenamedModules() {
185
+ const provided = /* @__PURE__ */ new Set();
186
+ try {
187
+ const manifest = createRequire(path.resolve("package.json"))("ember-source/package.json");
188
+ for (const key of Object.keys(manifest["ember-addon"]?.["renamed-modules"] ?? {})) provided.add(key.replace(/\.js$/, "").replace(/\/index$/, ""));
189
+ } catch {}
190
+ return provided;
191
+ }
192
+ /**
171
193
  * Keeps the library's declared dependencies and the ember virtual packages
172
194
  * (things like `@ember/component`, `@glimmer/tracking`, the template compiler,
173
195
  * …) external, so the app that consumes the library resolves them instead of
@@ -175,11 +197,13 @@ function resolvableDependencies() {
175
197
  */
176
198
  function emberExternals() {
177
199
  let deps;
200
+ let renamedModules;
178
201
  return {
179
202
  name: "ember:externals",
180
203
  buildStart() {
181
204
  this.addWatchFile("package.json");
182
205
  deps = resolvableDependencies();
206
+ renamedModules = emberSourceRenamedModules();
183
207
  },
184
208
  resolveId: {
185
209
  order: "pre",
@@ -188,6 +212,7 @@ function emberExternals() {
188
212
  const pkgName = packageName(source);
189
213
  if (!pkgName) return null;
190
214
  if (deps.has(pkgName) || emberVirtualPeerDeps.has(pkgName) || emberVirtualPackages.has(pkgName) || compilationModules.has(pkgName)) return false;
215
+ if (renamedModules.has(source)) return false;
191
216
  }
192
217
  }
193
218
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nullvoxpopuli/ember-rolldown",
3
- "version": "2.2.0",
3
+ "version": "2.3.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",
@@ -51,13 +51,16 @@ describe("appReexports", () => {
51
51
  await rm(projectDir, { recursive: true, force: true });
52
52
  });
53
53
 
54
- async function build(options?: string | AppReexportsOptions) {
54
+ async function build(
55
+ include?: string | string[] | AppReexportsOptions,
56
+ options?: Omit<AppReexportsOptions, "include">,
57
+ ) {
55
58
  const bundle = await rolldown({
56
59
  input: {
57
60
  "components/foo": "./src/components/foo.js",
58
61
  "utils/math": "./src/utils/math.js",
59
62
  },
60
- plugins: [appReexports(options)],
63
+ plugins: [appReexports(include, options)],
61
64
  });
62
65
 
63
66
  await bundle.write({ dir: "dist" });
@@ -100,6 +103,36 @@ describe("appReexports", () => {
100
103
  expect(Object.keys(manifest["ember-addon"]["app-js"])).toEqual(["./components/foo.js"]);
101
104
  });
102
105
 
106
+ it("accepts an array of include globs", async () => {
107
+ await build(["components/**", "utils/**"]);
108
+
109
+ const manifest = await readJson("package.json");
110
+
111
+ expect(Object.keys(manifest["ember-addon"]["app-js"])).toEqual([
112
+ "./components/foo.js",
113
+ "./utils/math.js",
114
+ ]);
115
+ });
116
+
117
+ it("accepts options as the second argument alongside a string or array include", async () => {
118
+ await build("**/*.js", { exclude: ["utils/**"] });
119
+
120
+ let manifest = await readJson("package.json");
121
+
122
+ expect(Object.keys(manifest["ember-addon"]["app-js"])).toEqual(["./components/foo.js"]);
123
+
124
+ await build(["components/**", "utils/**"], {
125
+ mapFilename: (name) => name.replace("math", "arithmetic"),
126
+ });
127
+
128
+ manifest = await readJson("package.json");
129
+
130
+ expect(manifest["ember-addon"]["app-js"]).toEqual({
131
+ "./components/foo.js": "./dist/_app_/components/foo.js",
132
+ "./utils/arithmetic.js": "./dist/_app_/utils/arithmetic.js",
133
+ });
134
+ });
135
+
103
136
  it("defaults to top-level services", async () => {
104
137
  await mkdir(join(projectDir, "src/services/nested"), { recursive: true });
105
138
  await writeFile(join(projectDir, "src/services/session.js"), "export default class {}\n");
@@ -65,16 +65,27 @@ export interface AppReexportsOptions {
65
65
  * ```
66
66
  *
67
67
  * With no arguments, top-level services (`services/*`) are re-exported. A
68
- * string argument is an include glob; an object gives full control:
68
+ * string or array of strings is the include glob(s), optionally followed by
69
+ * the remaining options; an object gives full control:
69
70
  *
70
71
  * ```js
71
72
  * appReexports(); // services/*
72
73
  * appReexports("components/**"); // one include glob
74
+ * appReexports(["services/*", "helpers/*"]);
75
+ * appReexports("components/**", { exclude: ["components/-private/**"] });
73
76
  * appReexports({ include: ["services/*", "helpers/*"], exclude: [...] });
74
77
  * ```
75
78
  */
76
- export function appReexports(options: string | AppReexportsOptions = {}): Plugin {
77
- const resolved = typeof options === "string" ? { include: [options] } : options;
79
+ export function appReexports(
80
+ includeOrOptions: string | string[] | AppReexportsOptions = {},
81
+ options: Omit<AppReexportsOptions, "include"> = {},
82
+ ): Plugin {
83
+ const resolved =
84
+ typeof includeOrOptions === "string"
85
+ ? { ...options, include: [includeOrOptions] }
86
+ : Array.isArray(includeOrOptions)
87
+ ? { ...options, include: includeOrOptions }
88
+ : includeOrOptions;
78
89
  const include = resolved.include ?? ["services/*"];
79
90
 
80
91
  return {
@@ -0,0 +1,118 @@
1
+ import { mkdir, mkdtemp, 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";
6
+
7
+ import { emberExternals } from "./externals.ts";
8
+
9
+ let restoreCwd: (() => void) | undefined;
10
+
11
+ afterEach(() => {
12
+ restoreCwd?.();
13
+ restoreCwd = undefined;
14
+ });
15
+
16
+ /**
17
+ * An ember-source stub whose `ember-addon.renamed-modules` provides
18
+ * `@glimmer/runtime` (file-path keys, like the real manifest).
19
+ */
20
+ const EMBER_SOURCE_STUB = {
21
+ name: "ember-source",
22
+ version: "0.0.0",
23
+ "ember-addon": {
24
+ "renamed-modules": {
25
+ "@glimmer/runtime/index.js": "ember-source/@glimmer/runtime/index.js",
26
+ "@ember/-internals/glimmer/index.js": "ember-source/@ember/-internals/glimmer/index.js",
27
+ },
28
+ },
29
+ };
30
+
31
+ /**
32
+ * Writes a fixture package (manifest + optional stub ember-source in its
33
+ * node_modules) and cds into it — the plugin reads both from the library's
34
+ * own context, like the tsdown CLI.
35
+ */
36
+ async function fixture(manifest: object, { emberSource = true } = {}): Promise<string> {
37
+ const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-externals-"));
38
+ await writeFile(path.join(dir, "package.json"), JSON.stringify(manifest));
39
+
40
+ if (emberSource) {
41
+ const stubDir = path.join(dir, "node_modules/ember-source");
42
+ await mkdir(stubDir, { recursive: true });
43
+ await writeFile(path.join(stubDir, "package.json"), JSON.stringify(EMBER_SOURCE_STUB));
44
+ }
45
+
46
+ const previousCwd = process.cwd();
47
+ process.chdir(dir);
48
+ restoreCwd = () => process.chdir(previousCwd);
49
+
50
+ return dir;
51
+ }
52
+
53
+ /** Runs the plugin's own hooks directly against the current fixture. */
54
+ function resolveWith(source: string): unknown {
55
+ const plugin = emberExternals();
56
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any
57
+ (plugin.buildStart as any).call({ addWatchFile() {} });
58
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any
59
+ return (plugin.resolveId as any).handler(source);
60
+ }
61
+
62
+ describe("emberExternals", () => {
63
+ it("externalizes declared dependencies and the ember virtual packages", async () => {
64
+ await fixture({ dependencies: { "some-dep": "*" } });
65
+ expect(resolveWith("some-dep")).toBe(false);
66
+ expect(resolveWith("@ember/component")).toBe(false);
67
+ expect(resolveWith("@glimmer/tracking")).toBe(false);
68
+ });
69
+
70
+ it("externalizes the modules ember-source provides via renamed-modules", async () => {
71
+ // Private API like @glimmer/runtime exists only inside the app's
72
+ // ember-source (its ember-addon.renamed-modules) — @embroider/core's
73
+ // emberVirtualPackages doesn't list it.
74
+ await fixture({});
75
+ expect(resolveWith("@glimmer/runtime")).toBe(false);
76
+ expect(resolveWith("@ember/-internals/glimmer")).toBe(false);
77
+ });
78
+
79
+ it("does not blanket-externalize @glimmer packages ember-source doesn't provide", async () => {
80
+ // e.g. @glimmer/component is a real package a library may want bundled;
81
+ // only declared deps / virtual packages / renamed-modules are external.
82
+ await fixture({});
83
+ expect(resolveWith("@glimmer/not-provided")).toBeUndefined();
84
+ });
85
+
86
+ it("leaves undeclared packages alone, with or without ember-source", async () => {
87
+ await fixture({});
88
+ expect(resolveWith("left-pad")).toBeUndefined();
89
+
90
+ await fixture({}, { emberSource: false });
91
+ expect(resolveWith("left-pad")).toBeUndefined();
92
+ // No ember-source in the graph -> no renamed-modules to consult.
93
+ expect(resolveWith("@glimmer/runtime")).toBeUndefined();
94
+ });
95
+
96
+ it("keeps a renamed-modules import external through a real build", async () => {
97
+ const dir = await fixture({ name: "build-fixture" });
98
+ await writeFile(
99
+ path.join(dir, "index.ts"),
100
+ `import { curry } from '@glimmer/runtime';\nexport const curried: unknown = curry;\n`,
101
+ );
102
+
103
+ const warnings: string[] = [];
104
+ const build = await rolldown({
105
+ input: path.join(dir, "index.ts"),
106
+ plugins: [emberExternals()],
107
+ onwarn(warning) {
108
+ warnings.push(warning.code ?? String(warning));
109
+ },
110
+ });
111
+ const { output } = await build.generate({ format: "es" });
112
+
113
+ expect(warnings.filter((code) => code === "UNRESOLVED_IMPORT")).toEqual([]);
114
+
115
+ const code = output.filter((chunk) => "code" in chunk).map((chunk) => chunk.code)[0] ?? "";
116
+ expect(code).toContain(`from "@glimmer/runtime"`);
117
+ });
118
+ });
package/src/externals.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { readFileSync } from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import path from "node:path";
2
4
 
3
5
  import type { Plugin } from "rolldown";
4
6
 
@@ -33,6 +35,38 @@ function resolvableDependencies(): Set<string> {
33
35
  return deps;
34
36
  }
35
37
 
38
+ /**
39
+ * The module names ember-source provides by renaming them into itself — the
40
+ * keys of its `ember-addon.renamed-modules`, normalized from file paths to
41
+ * import specifiers (`@glimmer/runtime/index.js` → `@glimmer/runtime`).
42
+ *
43
+ * This is the authoritative list of ember's provided modules, including
44
+ * private API that @embroider/core's emberVirtualPackages doesn't cover
45
+ * (e.g. `@glimmer/runtime`). It deliberately does NOT include real packages
46
+ * like `@glimmer/component`, which a library may want bundled.
47
+ *
48
+ * Resolved from the library's own dependency graph; empty when ember-source
49
+ * isn't resolvable there.
50
+ */
51
+ function emberSourceRenamedModules(): Set<string> {
52
+ const provided = new Set<string>();
53
+
54
+ try {
55
+ const require = createRequire(path.resolve("package.json"));
56
+ const manifest = require("ember-source/package.json") as {
57
+ "ember-addon"?: { "renamed-modules"?: Record<string, string> };
58
+ };
59
+
60
+ for (const key of Object.keys(manifest["ember-addon"]?.["renamed-modules"] ?? {})) {
61
+ provided.add(key.replace(/\.js$/, "").replace(/\/index$/, ""));
62
+ }
63
+ } catch {
64
+ // The library doesn't have ember-source in its graph; nothing to provide.
65
+ }
66
+
67
+ return provided;
68
+ }
69
+
36
70
  /**
37
71
  * Keeps the library's declared dependencies and the ember virtual packages
38
72
  * (things like `@ember/component`, `@glimmer/tracking`, the template compiler,
@@ -41,6 +75,7 @@ function resolvableDependencies(): Set<string> {
41
75
  */
42
76
  export function emberExternals(): Plugin {
43
77
  let deps: Set<string>;
78
+ let renamedModules: Set<string>;
44
79
 
45
80
  return {
46
81
  name: "ember:externals",
@@ -48,6 +83,7 @@ export function emberExternals(): Plugin {
48
83
  buildStart() {
49
84
  this.addWatchFile("package.json");
50
85
  deps = resolvableDependencies();
86
+ renamedModules = emberSourceRenamedModules();
51
87
  },
52
88
 
53
89
  resolveId: {
@@ -76,6 +112,15 @@ export function emberExternals(): Plugin {
76
112
  return false;
77
113
  }
78
114
 
115
+ // Modules ember-source provides by renaming them into itself
116
+ // (`ember-addon.renamed-modules`) — private API like
117
+ // `@glimmer/runtime` that only exists inside the app's ember-source.
118
+ // Matched on the full specifier: the renamed modules are module
119
+ // paths, not packages.
120
+ if (renamedModules.has(source)) {
121
+ return false;
122
+ }
123
+
79
124
  return undefined;
80
125
  },
81
126
  },