@nullvoxpopuli/ember-rolldown 2.2.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 +23 -2
- package/dist/app-reexports.js +12 -3
- package/dist/index.js +36 -3
- package/package.json +1 -1
- package/src/app-reexports.test.ts +35 -2
- package/src/app-reexports.ts +14 -3
- package/src/externals.test.ts +118 -0
- package/src/externals.ts +45 -0
- package/src/transform.test.ts +53 -2
- package/src/transform.ts +23 -2
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
|
|
@@ -194,12 +213,14 @@ export default defineConfig({
|
|
|
194
213
|
|
|
195
214
|
With no arguments, top-level services (`services/*`) are re-exported — under
|
|
196
215
|
strict mode, components and helpers are imported, but services are still
|
|
197
|
-
injected by name. A string
|
|
198
|
-
full control:
|
|
216
|
+
injected by name. A string or array of strings is the include glob(s),
|
|
217
|
+
optionally followed by the remaining options; an object gives full control:
|
|
199
218
|
|
|
200
219
|
```js
|
|
201
220
|
appReexports(); // services/*
|
|
202
221
|
appReexports("components/**"); // one include glob
|
|
222
|
+
appReexports(["services/*", "helpers/*"]);
|
|
223
|
+
appReexports("components/**", { exclude: ["components/-private/**"] });
|
|
203
224
|
appReexports({ include: ["services/*", "helpers/*"], exclude: [...] });
|
|
204
225
|
```
|
|
205
226
|
|
package/dist/app-reexports.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
@@ -3,9 +3,10 @@ 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";
|
|
7
|
-
import
|
|
6
|
+
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
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
|
};
|
|
@@ -281,7 +306,15 @@ function emberTransform() {
|
|
|
281
306
|
resolveId: {
|
|
282
307
|
order: "pre",
|
|
283
308
|
handler(id, importer) {
|
|
284
|
-
if (!importer)
|
|
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
|
+
}
|
|
285
318
|
const fileName = path.join(path.dirname(importer), id);
|
|
286
319
|
if (id.endsWith(".gts")) return {
|
|
287
320
|
id: fileName.replace(/\.gts$/, ".ts"),
|
package/package.json
CHANGED
|
@@ -51,13 +51,16 @@ describe("appReexports", () => {
|
|
|
51
51
|
await rm(projectDir, { recursive: true, force: true });
|
|
52
52
|
});
|
|
53
53
|
|
|
54
|
-
async function build(
|
|
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");
|
package/src/app-reexports.ts
CHANGED
|
@@ -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
|
|
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(
|
|
77
|
-
|
|
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
|
},
|
package/src/transform.test.ts
CHANGED
|
@@ -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(
|
|
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,
|
|
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
|
-
|
|
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
|
|