@nullvoxpopuli/ember-rolldown 2.1.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 +46 -6
- package/dist/app-reexports.js +12 -3
- package/dist/index.js +86 -48
- package/package.json +1 -1
- package/src/app-reexports.test.ts +35 -2
- package/src/app-reexports.ts +14 -3
- package/src/babel.test.ts +17 -0
- package/src/babel.ts +21 -3
- package/src/dts.test.ts +238 -38
- package/src/externals.test.ts +118 -0
- package/src/externals.ts +45 -0
- package/src/transform.test.ts +35 -0
- package/src/transform.ts +64 -31
package/README.md
CHANGED
|
@@ -99,9 +99,42 @@ because the importing component module never loads, any declaration that
|
|
|
99
99
|
references that component dangles, surfacing as misleading
|
|
100
100
|
`UNLOADABLE_DEPENDENCY` errors on `<component>.d.ts` files.
|
|
101
101
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
### ember-scoped-css
|
|
103
|
+
|
|
104
|
+
[ember-scoped-css](https://github.com/auditboard/ember-scoped-css) works with
|
|
105
|
+
this pipeline: its template transform rides along via `ember()`'s
|
|
106
|
+
`babel.templateTransforms` option, and its unplugin (`ember-scoped-css/rollup`)
|
|
107
|
+
resolves the scoped CSS requests the transform injects:
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
import { defineConfig } from "tsdown";
|
|
111
|
+
import { ember } from "@nullvoxpopuli/ember-rolldown";
|
|
112
|
+
import { scopedCSS } from "ember-scoped-css/rollup";
|
|
113
|
+
import { scopedCSS as scopedCssBabel } from "ember-scoped-css/babel";
|
|
114
|
+
|
|
115
|
+
export default defineConfig({
|
|
116
|
+
entry: ["./src/index.ts"],
|
|
117
|
+
css: { inject: true },
|
|
118
|
+
plugins: [
|
|
119
|
+
ember({
|
|
120
|
+
babel: {
|
|
121
|
+
plugins: [scopedCssBabel()],
|
|
122
|
+
templateTransforms: [scopedCssBabel.template({})],
|
|
123
|
+
},
|
|
124
|
+
}),
|
|
125
|
+
scopedCSS(),
|
|
126
|
+
],
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
This scopes co-located `.css` files, inline `<style scoped>` blocks, and the
|
|
131
|
+
`scopedClass` pseudo-helper (that last one is what the `babel.plugins` entry
|
|
132
|
+
handles — leave it off if you don't use `scopedClass` in module code).
|
|
133
|
+
|
|
134
|
+
`css.inject` matters for libraries: it keeps the `import "./style.css"`
|
|
135
|
+
statement in `dist/index.js`, so consuming apps pull the styles in through
|
|
136
|
+
the module graph — without it the bundled CSS is emitted but nothing loads
|
|
137
|
+
it.
|
|
105
138
|
|
|
106
139
|
## What `ember()` does
|
|
107
140
|
|
|
@@ -128,12 +161,17 @@ ember({
|
|
|
128
161
|
configFile: "./babel.config.js",
|
|
129
162
|
babelHelpers: "bundled",
|
|
130
163
|
plugins: [],
|
|
164
|
+
templateTransforms: [],
|
|
131
165
|
filter: { include: { imports: ["ember-concurrency"], code: [] } },
|
|
132
166
|
},
|
|
133
167
|
});
|
|
134
168
|
```
|
|
135
169
|
|
|
136
|
-
Each option is documented on `BabelOptions`.
|
|
170
|
+
Each option is documented on `BabelOptions`. `templateTransforms` feeds
|
|
171
|
+
template AST transforms to the default template-compilation step; it can't be
|
|
172
|
+
combined with a babel config file — a config lists
|
|
173
|
+
`babel-plugin-ember-template-compilation` itself, so its transforms belong
|
|
174
|
+
there.
|
|
137
175
|
|
|
138
176
|
## App re-exports
|
|
139
177
|
|
|
@@ -156,12 +194,14 @@ export default defineConfig({
|
|
|
156
194
|
|
|
157
195
|
With no arguments, top-level services (`services/*`) are re-exported — under
|
|
158
196
|
strict mode, components and helpers are imported, but services are still
|
|
159
|
-
injected by name. A string
|
|
160
|
-
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:
|
|
161
199
|
|
|
162
200
|
```js
|
|
163
201
|
appReexports(); // services/*
|
|
164
202
|
appReexports("components/**"); // one include glob
|
|
203
|
+
appReexports(["services/*", "helpers/*"]);
|
|
204
|
+
appReexports("components/**", { exclude: ["components/-private/**"] });
|
|
165
205
|
appReexports({ include: ["services/*", "helpers/*"], exclude: [...] });
|
|
166
206
|
```
|
|
167
207
|
|
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
|
@@ -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
|
|
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
|
|
@@ -33,14 +34,17 @@ function detectConfigFile() {
|
|
|
33
34
|
* consuming app resolves it (the library keeps `decorator-transforms` as
|
|
34
35
|
* a real dependency).
|
|
35
36
|
*/
|
|
36
|
-
function defaultPlugins() {
|
|
37
|
+
function defaultPlugins(templateTransforms) {
|
|
37
38
|
return [
|
|
38
39
|
[transformTypeScript, {
|
|
39
40
|
allExtensions: true,
|
|
40
41
|
onlyRemoveTypeImports: true,
|
|
41
42
|
allowDeclareFields: true
|
|
42
43
|
}],
|
|
43
|
-
[templateCompilation, {
|
|
44
|
+
[templateCompilation, {
|
|
45
|
+
targetFormat: "hbs",
|
|
46
|
+
transforms: templateTransforms ?? []
|
|
47
|
+
}],
|
|
44
48
|
[decoratorTransforms, { runtime: { import: "decorator-transforms/runtime-esm" } }]
|
|
45
49
|
];
|
|
46
50
|
}
|
|
@@ -74,16 +78,19 @@ function emberBabel(options = {}) {
|
|
|
74
78
|
babelHelpers: options.babelHelpers ?? "bundled",
|
|
75
79
|
filter: options.filter
|
|
76
80
|
};
|
|
77
|
-
if (configFile)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
if (configFile) {
|
|
82
|
+
if (options.templateTransforms?.length) throw new Error(`\`babel.templateTransforms\` cannot be combined with a babel config file (found ${configFile}). Your config lists babel-plugin-ember-template-compilation itself, so pass the transforms to its \`transforms\` option there instead.`);
|
|
83
|
+
return maybeBabel({
|
|
84
|
+
...shared,
|
|
85
|
+
configFile,
|
|
86
|
+
plugins: options.plugins ?? []
|
|
87
|
+
});
|
|
88
|
+
}
|
|
82
89
|
return maybeBabel({
|
|
83
90
|
...shared,
|
|
84
91
|
configFile: false,
|
|
85
92
|
babelrc: false,
|
|
86
|
-
plugins: defaultPlugins().concat(options.plugins ?? [])
|
|
93
|
+
plugins: defaultPlugins(options.templateTransforms).concat(options.plugins ?? [])
|
|
87
94
|
});
|
|
88
95
|
}
|
|
89
96
|
//#endregion
|
|
@@ -162,6 +169,27 @@ function resolvableDependencies() {
|
|
|
162
169
|
return deps;
|
|
163
170
|
}
|
|
164
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
|
+
/**
|
|
165
193
|
* Keeps the library's declared dependencies and the ember virtual packages
|
|
166
194
|
* (things like `@ember/component`, `@glimmer/tracking`, the template compiler,
|
|
167
195
|
* …) external, so the app that consumes the library resolves them instead of
|
|
@@ -169,11 +197,13 @@ function resolvableDependencies() {
|
|
|
169
197
|
*/
|
|
170
198
|
function emberExternals() {
|
|
171
199
|
let deps;
|
|
200
|
+
let renamedModules;
|
|
172
201
|
return {
|
|
173
202
|
name: "ember:externals",
|
|
174
203
|
buildStart() {
|
|
175
204
|
this.addWatchFile("package.json");
|
|
176
205
|
deps = resolvableDependencies();
|
|
206
|
+
renamedModules = emberSourceRenamedModules();
|
|
177
207
|
},
|
|
178
208
|
resolveId: {
|
|
179
209
|
order: "pre",
|
|
@@ -182,6 +212,7 @@ function emberExternals() {
|
|
|
182
212
|
const pkgName = packageName(source);
|
|
183
213
|
if (!pkgName) return null;
|
|
184
214
|
if (deps.has(pkgName) || emberVirtualPeerDeps.has(pkgName) || emberVirtualPackages.has(pkgName) || compilationModules.has(pkgName)) return false;
|
|
215
|
+
if (renamedModules.has(source)) return false;
|
|
185
216
|
}
|
|
186
217
|
}
|
|
187
218
|
};
|
|
@@ -249,11 +280,27 @@ async function fixDtsExtensionsInDir(dir) {
|
|
|
249
280
|
}
|
|
250
281
|
}
|
|
251
282
|
/**
|
|
283
|
+
* A sourcemap mapping every generated line to the same line of the input.
|
|
284
|
+
*
|
|
285
|
+
* `AAAA` is the [0, 0, 0, 0] VLQ segment (first line maps to source 0, line 0,
|
|
286
|
+
* column 0); `AACA` is [0, 0, +1, 0] (each following line advances the source
|
|
287
|
+
* line by one). Exact at line granularity, which is all the specifier rewrite
|
|
288
|
+
* can disturb.
|
|
289
|
+
*/
|
|
290
|
+
function lineIdentityMap(id, source) {
|
|
291
|
+
return {
|
|
292
|
+
version: 3,
|
|
293
|
+
sources: [id],
|
|
294
|
+
sourcesContent: [source],
|
|
295
|
+
names: [],
|
|
296
|
+
mappings: source.split("\n").map((_, line) => line === 0 ? "AAAA" : "AACA").join(";")
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
252
300
|
* Maps `.gts` -> `.ts` and `.gjs` -> `.js` (so rolldown can identify them as
|
|
253
301
|
* ts/js) and preprocesses `<template>` via content-tag.
|
|
254
302
|
*/
|
|
255
303
|
function emberTransform() {
|
|
256
|
-
const virtualFiles = /* @__PURE__ */ new Set();
|
|
257
304
|
return {
|
|
258
305
|
name: "ember:transform",
|
|
259
306
|
resolveId: {
|
|
@@ -261,43 +308,27 @@ function emberTransform() {
|
|
|
261
308
|
handler(id, importer) {
|
|
262
309
|
if (!importer) return null;
|
|
263
310
|
const fileName = path.join(path.dirname(importer), id);
|
|
264
|
-
if (id.endsWith(".gts")
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
return {
|
|
274
|
-
id: fileName.replace(/\.gjs$/, ".js"),
|
|
275
|
-
meta: { fileName }
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
if (id.endsWith(".d.ts") && importer.endsWith(".d.ts")) {
|
|
279
|
-
const gtsFile = fileName.replace(".d.ts", ".gts");
|
|
280
|
-
if (virtualFiles.has(gtsFile)) return fileName;
|
|
281
|
-
}
|
|
282
|
-
if (id.endsWith(".ts") && !importer.endsWith(".d.ts")) {
|
|
311
|
+
if (id.endsWith(".gts")) return {
|
|
312
|
+
id: fileName.replace(/\.gts$/, ".ts"),
|
|
313
|
+
meta: { fileName }
|
|
314
|
+
};
|
|
315
|
+
if (id.endsWith(".gjs")) return {
|
|
316
|
+
id: fileName.replace(/\.gjs$/, ".js"),
|
|
317
|
+
meta: { fileName }
|
|
318
|
+
};
|
|
319
|
+
if (id.endsWith(".ts")) {
|
|
283
320
|
const gtsFileName = fileName.replace(/\.ts$/, ".gts");
|
|
284
|
-
if (existsSync(gtsFileName) && !existsSync(fileName)) {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
meta: { fileName: gtsFileName }
|
|
289
|
-
};
|
|
290
|
-
}
|
|
321
|
+
if (existsSync(gtsFileName) && !existsSync(fileName)) return {
|
|
322
|
+
id: fileName,
|
|
323
|
+
meta: { fileName: gtsFileName }
|
|
324
|
+
};
|
|
291
325
|
}
|
|
292
|
-
if (id.endsWith(".js")
|
|
326
|
+
if (id.endsWith(".js")) {
|
|
293
327
|
const gjsFileName = fileName.replace(/\.js$/, ".gjs");
|
|
294
|
-
if (existsSync(gjsFileName) && !existsSync(fileName)) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
meta: { fileName: gjsFileName }
|
|
299
|
-
};
|
|
300
|
-
}
|
|
328
|
+
if (existsSync(gjsFileName) && !existsSync(fileName)) return {
|
|
329
|
+
id: fileName,
|
|
330
|
+
meta: { fileName: gjsFileName }
|
|
331
|
+
};
|
|
301
332
|
}
|
|
302
333
|
return null;
|
|
303
334
|
}
|
|
@@ -306,7 +337,11 @@ function emberTransform() {
|
|
|
306
337
|
order: "pre",
|
|
307
338
|
filter: { id: /\.(ts|js)$/ },
|
|
308
339
|
async handler(id) {
|
|
309
|
-
|
|
340
|
+
let fileName = (this.getModuleInfo(id)?.meta ?? {})?.fileName;
|
|
341
|
+
if (!fileName && !existsSync(id)) {
|
|
342
|
+
if (id.endsWith(".ts") && existsSync(id.replace(/\.ts$/, ".gts"))) fileName = id.replace(/\.ts$/, ".gts");
|
|
343
|
+
else if (id.endsWith(".js") && existsSync(id.replace(/\.js$/, ".gjs"))) fileName = id.replace(/\.js$/, ".gjs");
|
|
344
|
+
}
|
|
310
345
|
if (fileName) {
|
|
311
346
|
this.addWatchFile(fileName);
|
|
312
347
|
const source = await readFile(fileName, { encoding: "utf8" });
|
|
@@ -329,9 +364,12 @@ function emberTransform() {
|
|
|
329
364
|
id: /\.(js|ts)$/
|
|
330
365
|
},
|
|
331
366
|
handler(input, id) {
|
|
332
|
-
|
|
333
|
-
if (
|
|
334
|
-
return
|
|
367
|
+
const output = input.replace(/(['"`])((?:\.\.?\/|\/|@|[A-Za-z0-9_\-])[^'"]*?\.gts)\1/g, (_m, q, p) => `${q}${p.replace(/\.gts$/, ".ts")}${q}`);
|
|
368
|
+
if (output === input) return null;
|
|
369
|
+
return {
|
|
370
|
+
code: output,
|
|
371
|
+
map: lineIdentityMap(id, input)
|
|
372
|
+
};
|
|
335
373
|
}
|
|
336
374
|
},
|
|
337
375
|
writeBundle: { async handler(options) {
|
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,17 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { emberBabel } from "./babel.ts";
|
|
4
|
+
|
|
5
|
+
describe("emberBabel", () => {
|
|
6
|
+
it("rejects templateTransforms combined with a config file", () => {
|
|
7
|
+
expect(() =>
|
|
8
|
+
emberBabel({ configFile: "./babel.config.js", templateTransforms: ["some-transform"] }),
|
|
9
|
+
).toThrow(/pass the transforms/);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("allows templateTransforms when config files are opted out", () => {
|
|
13
|
+
expect(() =>
|
|
14
|
+
emberBabel({ configFile: false, templateTransforms: ["some-transform"] }),
|
|
15
|
+
).not.toThrow();
|
|
16
|
+
});
|
|
17
|
+
});
|
package/src/babel.ts
CHANGED
|
@@ -4,6 +4,7 @@ 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 type { PluginItem } from "@babel/core";
|
|
7
|
+
import type { Transform } from "babel-plugin-ember-template-compilation";
|
|
7
8
|
import type { Plugin } from "rolldown";
|
|
8
9
|
|
|
9
10
|
export interface BabelOptions {
|
|
@@ -24,6 +25,15 @@ export interface BabelOptions {
|
|
|
24
25
|
* Extra babel plugins to run, appended after the config's plugins.
|
|
25
26
|
*/
|
|
26
27
|
plugins?: PluginItem[];
|
|
28
|
+
/**
|
|
29
|
+
* Template AST transforms (e.g. ember-scoped-css's `scopedCSS.template()`)
|
|
30
|
+
* passed to the default `babel-plugin-ember-template-compilation` entry.
|
|
31
|
+
*
|
|
32
|
+
* Only valid without a babel config file: with your own config, you list
|
|
33
|
+
* `babel-plugin-ember-template-compilation` yourself, so its `transforms`
|
|
34
|
+
* belong there.
|
|
35
|
+
*/
|
|
36
|
+
templateTransforms?: Transform[];
|
|
27
37
|
/**
|
|
28
38
|
* Opt additional files into babel (see maybeBabel's `filter`). Use this to
|
|
29
39
|
* cover addons that ship code needing babel (e.g. `ember-concurrency`).
|
|
@@ -63,7 +73,7 @@ function detectConfigFile(): string | undefined {
|
|
|
63
73
|
* consuming app resolves it (the library keeps `decorator-transforms` as
|
|
64
74
|
* a real dependency).
|
|
65
75
|
*/
|
|
66
|
-
function defaultPlugins(): PluginItem[] {
|
|
76
|
+
function defaultPlugins(templateTransforms?: Transform[]): PluginItem[] {
|
|
67
77
|
return [
|
|
68
78
|
[
|
|
69
79
|
transformTypeScript,
|
|
@@ -73,7 +83,7 @@ function defaultPlugins(): PluginItem[] {
|
|
|
73
83
|
allowDeclareFields: true,
|
|
74
84
|
},
|
|
75
85
|
],
|
|
76
|
-
[templateCompilation, { targetFormat: "hbs" }],
|
|
86
|
+
[templateCompilation, { targetFormat: "hbs", transforms: templateTransforms ?? [] }],
|
|
77
87
|
[
|
|
78
88
|
decoratorTransforms,
|
|
79
89
|
{
|
|
@@ -118,6 +128,14 @@ export function emberBabel(options: BabelOptions = {}): Plugin {
|
|
|
118
128
|
};
|
|
119
129
|
|
|
120
130
|
if (configFile) {
|
|
131
|
+
if (options.templateTransforms?.length) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
`\`babel.templateTransforms\` cannot be combined with a babel config file (found ${configFile}). ` +
|
|
134
|
+
`Your config lists babel-plugin-ember-template-compilation itself, so pass the transforms ` +
|
|
135
|
+
`to its \`transforms\` option there instead.`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
121
139
|
return maybeBabel({
|
|
122
140
|
...shared,
|
|
123
141
|
configFile,
|
|
@@ -131,6 +149,6 @@ export function emberBabel(options: BabelOptions = {}): Plugin {
|
|
|
131
149
|
// for one -- we provide the whole plugin list inline.
|
|
132
150
|
configFile: false,
|
|
133
151
|
babelrc: false,
|
|
134
|
-
plugins: defaultPlugins().concat(options.plugins ?? []),
|
|
152
|
+
plugins: defaultPlugins(options.templateTransforms).concat(options.plugins ?? []),
|
|
135
153
|
}) as unknown as Plugin;
|
|
136
154
|
}
|
package/src/dts.test.ts
CHANGED
|
@@ -6,13 +6,30 @@ import { afterEach, describe, expect, it } from "vitest";
|
|
|
6
6
|
|
|
7
7
|
import { ember } from "../index.ts";
|
|
8
8
|
|
|
9
|
+
interface Dist {
|
|
10
|
+
/** Sorted dist-relative paths of every emitted file. */
|
|
11
|
+
files: string[];
|
|
12
|
+
/** dist-relative path -> content, for every emitted .d.ts and .css file. */
|
|
13
|
+
contents: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let restoreCwd: (() => void) | undefined;
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
restoreCwd?.();
|
|
20
|
+
restoreCwd = undefined;
|
|
21
|
+
});
|
|
22
|
+
|
|
9
23
|
/**
|
|
10
24
|
* Drives `ember()` through a real tsdown build (the dts pipeline included).
|
|
11
25
|
* `files` is a map of relative path -> source, written into a temp dir that
|
|
12
26
|
* becomes the cwd for the build (the externals and isolated-declarations
|
|
13
27
|
* plugins read package.json / tsconfig.json from cwd, like the tsdown CLI).
|
|
28
|
+
*
|
|
29
|
+
* Returns the emitted file list plus the full contents of every declaration
|
|
30
|
+
* and css file (js output is covered by transform.test.ts).
|
|
14
31
|
*/
|
|
15
|
-
async function buildFixture(files: Record<string, string>, entry: string[]): Promise<
|
|
32
|
+
async function buildFixture(files: Record<string, string>, entry: string[]): Promise<Dist> {
|
|
16
33
|
const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-dts-"));
|
|
17
34
|
|
|
18
35
|
const withDefaults: Record<string, string> = {
|
|
@@ -45,33 +62,40 @@ async function buildFixture(files: Record<string, string>, entry: string[]): Pro
|
|
|
45
62
|
plugins: [ember()],
|
|
46
63
|
});
|
|
47
64
|
|
|
48
|
-
|
|
49
|
-
}
|
|
65
|
+
const distDir = path.join(dir, "dist");
|
|
66
|
+
const dist: Dist = { files: [], contents: {} };
|
|
50
67
|
|
|
51
|
-
|
|
68
|
+
for (const entry of await readdir(distDir, { withFileTypes: true, recursive: true })) {
|
|
69
|
+
if (!entry.isFile()) continue;
|
|
52
70
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
restoreCwd = undefined;
|
|
56
|
-
});
|
|
71
|
+
const relative = path.relative(distDir, path.join(entry.parentPath, entry.name));
|
|
72
|
+
dist.files.push(relative);
|
|
57
73
|
|
|
58
|
-
|
|
59
|
-
|
|
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)));
|
|
74
|
+
if (relative.endsWith(".d.ts") || relative.endsWith(".css")) {
|
|
75
|
+
dist.contents[relative] = await readFile(path.join(distDir, relative), "utf8");
|
|
63
76
|
}
|
|
64
77
|
}
|
|
65
|
-
|
|
78
|
+
|
|
79
|
+
dist.files.sort();
|
|
80
|
+
|
|
81
|
+
return dist;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** One printable document of every captured file, for a single snapshot. */
|
|
85
|
+
function printed(dist: Dist): string {
|
|
86
|
+
return Object.entries(dist.contents)
|
|
87
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
88
|
+
.map(([file, content]) => `//=== ${file}\n${content}`)
|
|
89
|
+
.join("\n");
|
|
66
90
|
}
|
|
67
91
|
|
|
68
92
|
describe("declarations (full ember() via tsdown)", () => {
|
|
69
93
|
it("emits .d.ts for an entry that type-imports from a non-entry .gts", async () => {
|
|
70
|
-
// Mirrors
|
|
94
|
+
// Mirrors a design-system shape: the component is in the runtime graph via
|
|
71
95
|
// the index entry, while a second entry (a service) only *type*-imports
|
|
72
96
|
// from it. The service's declaration then references the component's
|
|
73
97
|
// (virtual) .d.ts, which only exists in the bundler's module graph.
|
|
74
|
-
const
|
|
98
|
+
const dist = await buildFixture(
|
|
75
99
|
{
|
|
76
100
|
"src/index.ts": `export { default as Popup } from './popup.gts';`,
|
|
77
101
|
"src/service.ts": [
|
|
@@ -103,17 +127,164 @@ describe("declarations (full ember() via tsdown)", () => {
|
|
|
103
127
|
["./src/index.ts", "./src/service.ts"],
|
|
104
128
|
);
|
|
105
129
|
|
|
106
|
-
|
|
130
|
+
expect(dist.files).toMatchInlineSnapshot(`
|
|
131
|
+
[
|
|
132
|
+
"index.d.ts",
|
|
133
|
+
"index.js",
|
|
134
|
+
"index.js.map",
|
|
135
|
+
"popup-CjMmUEVb.d.ts",
|
|
136
|
+
"popup-CjMmUEVb.d.ts.map",
|
|
137
|
+
"service.d.ts",
|
|
138
|
+
"service.d.ts.map",
|
|
139
|
+
"service.js",
|
|
140
|
+
"service.js.map",
|
|
141
|
+
]
|
|
142
|
+
`);
|
|
107
143
|
|
|
108
|
-
expect(dist).
|
|
109
|
-
|
|
144
|
+
expect(printed(dist)).toMatchInlineSnapshot(`
|
|
145
|
+
"//=== index.d.ts
|
|
146
|
+
import { t as Popup } from "./popup-CjMmUEVb.js";
|
|
147
|
+
export { Popup };
|
|
148
|
+
//=== popup-CjMmUEVb.d.ts
|
|
149
|
+
import Component from "@glimmer/component";
|
|
150
|
+
//#region src/popup.d.ts
|
|
151
|
+
interface PopupBlock {
|
|
152
|
+
open: () => void;
|
|
153
|
+
}
|
|
154
|
+
interface PopupSignature {
|
|
155
|
+
Element: HTMLDivElement;
|
|
156
|
+
Blocks: {
|
|
157
|
+
default: [PopupBlock];
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
declare class Popup extends Component<PopupSignature> {}
|
|
161
|
+
//#endregion
|
|
162
|
+
export { PopupBlock as n, Popup as t };
|
|
163
|
+
//# sourceMappingURL=popup-CjMmUEVb.d.ts.map
|
|
164
|
+
//=== service.d.ts
|
|
165
|
+
import { n as PopupBlock } from "./popup-CjMmUEVb.js";
|
|
166
|
+
//#region src/service.d.ts
|
|
167
|
+
declare class TooltipService {
|
|
168
|
+
registerPopup(context: PopupBlock): void;
|
|
169
|
+
}
|
|
170
|
+
//#endregion
|
|
171
|
+
export { TooltipService };
|
|
172
|
+
//# sourceMappingURL=service.d.ts.map"
|
|
173
|
+
`);
|
|
174
|
+
});
|
|
110
175
|
|
|
111
|
-
|
|
176
|
+
it("emits .d.ts when a .gts module is only ever type-imported", async () => {
|
|
177
|
+
// Nothing value-imports popup.gts, so its virtual module never enters the
|
|
178
|
+
// runtime graph: the only route to its declaration is the dts resolver
|
|
179
|
+
// loading it on demand. The declaration pipeline registers modules as
|
|
180
|
+
// their (virtual) source loads, so the import must resolve to the source
|
|
181
|
+
// id — a pre-rewritten `.d.ts` id skips that registration and dangles.
|
|
182
|
+
const dist = await buildFixture(
|
|
183
|
+
{
|
|
184
|
+
"src/service.ts": [
|
|
185
|
+
`import type { PopupBlock } from './components/popup.gts';`,
|
|
186
|
+
``,
|
|
187
|
+
`export class TooltipService {`,
|
|
188
|
+
` registerPopup(context: PopupBlock): void {`,
|
|
189
|
+
` void context;`,
|
|
190
|
+
` }`,
|
|
191
|
+
`}`,
|
|
192
|
+
].join("\n"),
|
|
193
|
+
"src/components/popup.gts": [
|
|
194
|
+
`import Component from '@glimmer/component';`,
|
|
195
|
+
``,
|
|
196
|
+
`export interface PopupBlock {`,
|
|
197
|
+
` open: () => void;`,
|
|
198
|
+
`}`,
|
|
199
|
+
``,
|
|
200
|
+
`export default class Popup extends Component {`,
|
|
201
|
+
` <template>popup</template>`,
|
|
202
|
+
`}`,
|
|
203
|
+
].join("\n"),
|
|
204
|
+
},
|
|
205
|
+
["./src/service.ts"],
|
|
206
|
+
);
|
|
112
207
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
208
|
+
expect(dist.files).toMatchInlineSnapshot(`
|
|
209
|
+
[
|
|
210
|
+
"service.d.ts",
|
|
211
|
+
"service.d.ts.map",
|
|
212
|
+
"service.js",
|
|
213
|
+
"service.js.map",
|
|
214
|
+
]
|
|
215
|
+
`);
|
|
216
|
+
|
|
217
|
+
expect(printed(dist)).toMatchInlineSnapshot(`
|
|
218
|
+
"//=== service.d.ts
|
|
219
|
+
import "@glimmer/component";
|
|
220
|
+
//#region src/components/popup.d.ts
|
|
221
|
+
interface PopupBlock {
|
|
222
|
+
open: () => void;
|
|
223
|
+
}
|
|
224
|
+
//#endregion
|
|
225
|
+
//#region src/service.d.ts
|
|
226
|
+
declare class TooltipService {
|
|
227
|
+
registerPopup(context: PopupBlock): void;
|
|
228
|
+
}
|
|
229
|
+
//#endregion
|
|
230
|
+
export { TooltipService };
|
|
231
|
+
//# sourceMappingURL=service.d.ts.map"
|
|
232
|
+
`);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("emits .d.ts for a .gts that type-imports another (otherwise unimported) .gts", async () => {
|
|
236
|
+
// A nested component type-imports a sibling from the parent directory;
|
|
237
|
+
// its declaration then carries a relative source specifier for a module
|
|
238
|
+
// that exists only in the bundler's graph (never on disk, never in the
|
|
239
|
+
// runtime graph).
|
|
240
|
+
const dist = await buildFixture(
|
|
241
|
+
{
|
|
242
|
+
"src/index.ts": `export { default as OptionList } from './components/select/option-list.gts';`,
|
|
243
|
+
"src/components/select/option-list.gts": [
|
|
244
|
+
`import Component from '@glimmer/component';`,
|
|
245
|
+
`import type { PopupBlock } from '../popup.gts';`,
|
|
246
|
+
``,
|
|
247
|
+
`export interface OptionListArgs {`,
|
|
248
|
+
` registerPopup: (context: PopupBlock) => void;`,
|
|
249
|
+
`}`,
|
|
250
|
+
``,
|
|
251
|
+
`export default class OptionList extends Component {`,
|
|
252
|
+
` <template>options</template>`,
|
|
253
|
+
`}`,
|
|
254
|
+
].join("\n"),
|
|
255
|
+
"src/components/popup.gts": [
|
|
256
|
+
`import Component from '@glimmer/component';`,
|
|
257
|
+
``,
|
|
258
|
+
`export interface PopupBlock {`,
|
|
259
|
+
` open: () => void;`,
|
|
260
|
+
`}`,
|
|
261
|
+
``,
|
|
262
|
+
`export default class Popup extends Component {`,
|
|
263
|
+
` <template>popup</template>`,
|
|
264
|
+
`}`,
|
|
265
|
+
].join("\n"),
|
|
266
|
+
},
|
|
267
|
+
["./src/index.ts"],
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
expect(dist.files).toMatchInlineSnapshot(`
|
|
271
|
+
[
|
|
272
|
+
"index.d.ts",
|
|
273
|
+
"index.d.ts.map",
|
|
274
|
+
"index.js",
|
|
275
|
+
"index.js.map",
|
|
276
|
+
]
|
|
277
|
+
`);
|
|
278
|
+
|
|
279
|
+
expect(printed(dist)).toMatchInlineSnapshot(`
|
|
280
|
+
"//=== index.d.ts
|
|
281
|
+
import Component from "@glimmer/component";
|
|
282
|
+
//#region src/components/select/option-list.d.ts
|
|
283
|
+
declare class OptionList extends Component {}
|
|
284
|
+
//#endregion
|
|
285
|
+
export { OptionList };
|
|
286
|
+
//# sourceMappingURL=index.d.ts.map"
|
|
287
|
+
`);
|
|
117
288
|
});
|
|
118
289
|
|
|
119
290
|
it("bundles co-located CSS (via @tsdown/css) without breaking declarations", async () => {
|
|
@@ -121,8 +292,8 @@ describe("declarations (full ember() via tsdown)", () => {
|
|
|
121
292
|
// handling the build dies on tsdown's css-guard — and because the failing
|
|
122
293
|
// component module never loads, every declaration that references it
|
|
123
294
|
// dangles (surfacing as misleading UNLOADABLE_DEPENDENCY errors on
|
|
124
|
-
// <component>.d.ts). @tsdown/css
|
|
125
|
-
const
|
|
295
|
+
// <component>.d.ts). @tsdown/css makes both work.
|
|
296
|
+
const dist = await buildFixture(
|
|
126
297
|
{
|
|
127
298
|
"src/index.ts": `export { default as Popup } from './popup.gts';`,
|
|
128
299
|
"src/service.ts": [
|
|
@@ -149,18 +320,47 @@ describe("declarations (full ember() via tsdown)", () => {
|
|
|
149
320
|
["./src/index.ts", "./src/service.ts"],
|
|
150
321
|
);
|
|
151
322
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
323
|
+
expect(dist.files).toMatchInlineSnapshot(`
|
|
324
|
+
[
|
|
325
|
+
"index.d.ts",
|
|
326
|
+
"index.js",
|
|
327
|
+
"index.js.map",
|
|
328
|
+
"popup-CytznrQX.d.ts",
|
|
329
|
+
"popup-CytznrQX.d.ts.map",
|
|
330
|
+
"service.d.ts",
|
|
331
|
+
"service.d.ts.map",
|
|
332
|
+
"service.js",
|
|
333
|
+
"service.js.map",
|
|
334
|
+
"style.css",
|
|
335
|
+
]
|
|
336
|
+
`);
|
|
159
337
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
338
|
+
expect(printed(dist)).toMatchInlineSnapshot(`
|
|
339
|
+
"//=== index.d.ts
|
|
340
|
+
import { t as Popup } from "./popup-CytznrQX.js";
|
|
341
|
+
export { Popup };
|
|
342
|
+
//=== popup-CytznrQX.d.ts
|
|
343
|
+
import Component from "@glimmer/component";
|
|
344
|
+
//#region src/popup.d.ts
|
|
345
|
+
interface PopupBlock {
|
|
346
|
+
open: () => void;
|
|
347
|
+
}
|
|
348
|
+
declare class Popup extends Component {}
|
|
349
|
+
//#endregion
|
|
350
|
+
export { PopupBlock as n, Popup as t };
|
|
351
|
+
//# sourceMappingURL=popup-CytznrQX.d.ts.map
|
|
352
|
+
//=== service.d.ts
|
|
353
|
+
import { n as PopupBlock } from "./popup-CytznrQX.js";
|
|
354
|
+
//#region src/service.d.ts
|
|
355
|
+
declare function register(context: PopupBlock): void;
|
|
356
|
+
//#endregion
|
|
357
|
+
export { register };
|
|
358
|
+
//# sourceMappingURL=service.d.ts.map
|
|
359
|
+
//=== style.css
|
|
360
|
+
.popup {
|
|
361
|
+
color: red;
|
|
362
|
+
}
|
|
363
|
+
"
|
|
364
|
+
`);
|
|
165
365
|
});
|
|
166
366
|
});
|
|
@@ -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
|
@@ -170,4 +170,39 @@ describe("emberTransform (full plugin via rolldown)", () => {
|
|
|
170
170
|
"
|
|
171
171
|
`);
|
|
172
172
|
});
|
|
173
|
+
|
|
174
|
+
it("emits sourcemaps for the specifier rewrite (no SOURCEMAP_BROKEN warnings)", async () => {
|
|
175
|
+
const dir = await mkdtemp(path.join(tmpdir(), "ember-rolldown-map-"));
|
|
176
|
+
|
|
177
|
+
const files = {
|
|
178
|
+
// index.ts contains a `.gts` specifier, so it goes through the rewrite.
|
|
179
|
+
"index.ts": `export { default as Foo } from './foo.gts';`,
|
|
180
|
+
"foo.gts": `<template>Foo</template>`,
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
for (const [relative, source] of Object.entries(files)) {
|
|
184
|
+
await writeFile(path.join(dir, relative), source, "utf8");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const warnings: string[] = [];
|
|
188
|
+
const build = await rolldown({
|
|
189
|
+
input: path.join(dir, "index.ts"),
|
|
190
|
+
plugins: [emberTransform()],
|
|
191
|
+
external: (id) => !(id.startsWith(".") || path.isAbsolute(id)),
|
|
192
|
+
onwarn(warning) {
|
|
193
|
+
warnings.push(warning.code ?? String(warning));
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const { output } = await build.generate({ format: "es", sourcemap: true });
|
|
198
|
+
|
|
199
|
+
expect(warnings.filter((code) => code === "SOURCEMAP_BROKEN")).toEqual([]);
|
|
200
|
+
|
|
201
|
+
const chunk = output.find((entry) => entry.type === "chunk" && "map" in entry);
|
|
202
|
+
expect(chunk).toBeDefined();
|
|
203
|
+
// The composed map traces through the rewrite back to the original .gts
|
|
204
|
+
// source (content-tag's map survives the specifier-rewrite pass).
|
|
205
|
+
const sources = (chunk as { map?: { sources: string[] } }).map?.sources ?? [];
|
|
206
|
+
expect(sources.some((source) => source.endsWith("foo.gts"))).toBe(true);
|
|
207
|
+
});
|
|
173
208
|
});
|
package/src/transform.ts
CHANGED
|
@@ -38,13 +38,32 @@ async function fixDtsExtensionsInDir(dir: string): Promise<void> {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* A sourcemap mapping every generated line to the same line of the input.
|
|
43
|
+
*
|
|
44
|
+
* `AAAA` is the [0, 0, 0, 0] VLQ segment (first line maps to source 0, line 0,
|
|
45
|
+
* column 0); `AACA` is [0, 0, +1, 0] (each following line advances the source
|
|
46
|
+
* line by one). Exact at line granularity, which is all the specifier rewrite
|
|
47
|
+
* can disturb.
|
|
48
|
+
*/
|
|
49
|
+
function lineIdentityMap(id: string, source: string) {
|
|
50
|
+
return {
|
|
51
|
+
version: 3,
|
|
52
|
+
sources: [id],
|
|
53
|
+
sourcesContent: [source],
|
|
54
|
+
names: [],
|
|
55
|
+
mappings: source
|
|
56
|
+
.split("\n")
|
|
57
|
+
.map((_, line) => (line === 0 ? "AAAA" : "AACA"))
|
|
58
|
+
.join(";"),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
41
62
|
/**
|
|
42
63
|
* Maps `.gts` -> `.ts` and `.gjs` -> `.js` (so rolldown can identify them as
|
|
43
64
|
* ts/js) and preprocesses `<template>` via content-tag.
|
|
44
65
|
*/
|
|
45
66
|
export function emberTransform(): Plugin {
|
|
46
|
-
const virtualFiles = new Set<string>();
|
|
47
|
-
|
|
48
67
|
return {
|
|
49
68
|
name: "ember:transform",
|
|
50
69
|
|
|
@@ -55,41 +74,39 @@ export function emberTransform(): Plugin {
|
|
|
55
74
|
|
|
56
75
|
const fileName = path.join(path.dirname(importer), id);
|
|
57
76
|
|
|
58
|
-
if (id.endsWith(".gts")
|
|
59
|
-
virtualFiles.add(fileName);
|
|
77
|
+
if (id.endsWith(".gts")) {
|
|
60
78
|
return {
|
|
61
79
|
id: fileName.replace(/\.gts$/, ".ts"),
|
|
62
80
|
meta: { fileName },
|
|
63
81
|
};
|
|
64
82
|
}
|
|
65
83
|
|
|
66
|
-
if (id.endsWith(".gjs")
|
|
67
|
-
virtualFiles.add(fileName);
|
|
84
|
+
if (id.endsWith(".gjs")) {
|
|
68
85
|
return {
|
|
69
86
|
id: fileName.replace(/\.gjs$/, ".js"),
|
|
70
87
|
meta: { fileName },
|
|
71
88
|
};
|
|
72
89
|
}
|
|
73
90
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
91
|
+
// A `.ts`/`.js` specifier whose only backing file is a `.gts`/`.gjs`
|
|
92
|
+
// resolves to the virtual module. This must also serve `.d.ts`
|
|
93
|
+
// importers: rolldown-plugin-dts resolves a declaration module's
|
|
94
|
+
// imports through the plugin pipeline (`this.resolve`), and — when the
|
|
95
|
+
// resolution is a source file — loads it (registering its declaration)
|
|
96
|
+
// before mapping the import to the declaration id. Virtual modules
|
|
97
|
+
// exist nowhere else, so if we don't answer here the import either
|
|
98
|
+
// fails to resolve (no file on disk) or, worse, resolves to a
|
|
99
|
+
// declaration id that was never registered.
|
|
100
|
+
if (id.endsWith(".ts")) {
|
|
82
101
|
const gtsFileName = fileName.replace(/\.ts$/, ".gts");
|
|
83
102
|
if (existsSync(gtsFileName) && !existsSync(fileName)) {
|
|
84
|
-
virtualFiles.add(gtsFileName);
|
|
85
103
|
return { id: fileName, meta: { fileName: gtsFileName } };
|
|
86
104
|
}
|
|
87
105
|
}
|
|
88
106
|
|
|
89
|
-
if (id.endsWith(".js")
|
|
107
|
+
if (id.endsWith(".js")) {
|
|
90
108
|
const gjsFileName = fileName.replace(/\.js$/, ".gjs");
|
|
91
109
|
if (existsSync(gjsFileName) && !existsSync(fileName)) {
|
|
92
|
-
virtualFiles.add(gjsFileName);
|
|
93
110
|
return { id: fileName, meta: { fileName: gjsFileName } };
|
|
94
111
|
}
|
|
95
112
|
}
|
|
@@ -105,7 +122,18 @@ export function emberTransform(): Plugin {
|
|
|
105
122
|
},
|
|
106
123
|
async handler(id) {
|
|
107
124
|
const meta = this.getModuleInfo(id)?.meta ?? {};
|
|
108
|
-
|
|
125
|
+
let fileName = meta?.fileName;
|
|
126
|
+
|
|
127
|
+
// A virtual id can be loaded without having passed through our
|
|
128
|
+
// resolveId (rolldown-plugin-dts calls `this.load({ id })` with just
|
|
129
|
+
// the id), so no meta is attached: recover the backing file from disk.
|
|
130
|
+
if (!fileName && !existsSync(id)) {
|
|
131
|
+
if (id.endsWith(".ts") && existsSync(id.replace(/\.ts$/, ".gts"))) {
|
|
132
|
+
fileName = id.replace(/\.ts$/, ".gts");
|
|
133
|
+
} else if (id.endsWith(".js") && existsSync(id.replace(/\.js$/, ".gjs"))) {
|
|
134
|
+
fileName = id.replace(/\.js$/, ".gjs");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
109
137
|
|
|
110
138
|
if (fileName) {
|
|
111
139
|
this.addWatchFile(fileName);
|
|
@@ -132,21 +160,26 @@ export function emberTransform(): Plugin {
|
|
|
132
160
|
id: /\.(js|ts)$/,
|
|
133
161
|
},
|
|
134
162
|
handler(input, id) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
163
|
+
// Rewrite `.gts` specifiers to `.ts` — the (virtual) source id — in
|
|
164
|
+
// runtime modules AND declaration modules alike. Declaration modules
|
|
165
|
+
// must NOT be rewritten to `.d.ts`: rolldown-plugin-dts's resolver
|
|
166
|
+
// treats a source-file resolution as "load it (registering its
|
|
167
|
+
// declaration), then map to the declaration id", while a `.d.ts` id is
|
|
168
|
+
// returned as-is — unloadable when the module isn't registered yet
|
|
169
|
+
// (e.g. the `.gts` is only ever type-imported).
|
|
170
|
+
const output = input.replace(
|
|
171
|
+
/(['"`])((?:\.\.?\/|\/|@|[A-Za-z0-9_\-])[^'"]*?\.gts)\1/g,
|
|
172
|
+
(_m, q, p) => `${q}${p.replace(/\.gts$/, ".ts")}${q}`,
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
if (output === input) {
|
|
176
|
+
return null;
|
|
147
177
|
}
|
|
148
178
|
|
|
149
|
-
|
|
179
|
+
// The rewrite only shortens import specifiers in place, so a
|
|
180
|
+
// line-identity map is accurate to the line (and to the column for
|
|
181
|
+
// everything before the first rewritten specifier on a line).
|
|
182
|
+
return { code: output, map: lineIdentityMap(id, input) };
|
|
150
183
|
},
|
|
151
184
|
},
|
|
152
185
|
|