@nullvoxpopuli/ember-vite 1.5.0 → 2.0.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 +3 -1
- package/dist/index.js +48 -23
- package/package.json +2 -2
- package/src/ember.test.ts +270 -13
- package/src/ember.ts +66 -31
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ ember({
|
|
|
52
52
|
babel: {
|
|
53
53
|
parallel: false,
|
|
54
54
|
include: {
|
|
55
|
-
whenImporting: ["
|
|
55
|
+
whenImporting: ["my-babel-only-library"],
|
|
56
56
|
whenMatching: [/\bmyMacro\(/],
|
|
57
57
|
},
|
|
58
58
|
},
|
|
@@ -60,3 +60,5 @@ ember({
|
|
|
60
60
|
```
|
|
61
61
|
|
|
62
62
|
Each option is documented on its type.
|
|
63
|
+
|
|
64
|
+
If the project depends on `ember-concurrency`, `ember-intl`, `ember-scoped-css` or `unplugin-icons`, babel compiles their modules with no option.
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { buildMacros } from "@embroider/macros/babel";
|
|
2
2
|
import { ember as ember$1, extensions, resolver, templateTag } from "@embroider/vite";
|
|
3
|
-
import { existsSync } from "node:fs";
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
4
|
import { join, resolve } from "node:path";
|
|
5
5
|
import { maybeBabel, maybeBabel as maybeBabel$1 } from "@nullvoxpopuli/ember-build-tooling-utils";
|
|
6
6
|
//#region src/test-html.ts
|
|
@@ -139,8 +139,7 @@ function defaultBabelPlugins(mode) {
|
|
|
139
139
|
*
|
|
140
140
|
* Without one (libraries), `defaultBabelPlugins` covers TypeScript, templates, macros, and decorators,
|
|
141
141
|
* so no config file is required.
|
|
142
|
-
*
|
|
143
|
-
* Helpers are inlined in that case:
|
|
142
|
+
* Babel helpers are inlined in that case:
|
|
144
143
|
* projects without a babel config don't carry `@babel/runtime`.
|
|
145
144
|
*/
|
|
146
145
|
function babelOptions(nvpConfig) {
|
|
@@ -152,39 +151,65 @@ function babelOptions(nvpConfig) {
|
|
|
152
151
|
*/
|
|
153
152
|
return {
|
|
154
153
|
configFile: false,
|
|
155
|
-
babelrc: false,
|
|
156
|
-
babelHelpers: "inline",
|
|
157
154
|
plugins: []
|
|
158
155
|
};
|
|
159
156
|
}
|
|
160
157
|
/**
|
|
161
|
-
*
|
|
158
|
+
* Libraries that ship code that still needs babel.
|
|
159
|
+
* Each one adds to the filter only when the project depends on it,
|
|
160
|
+
* so other projects do not pay for the check.
|
|
162
161
|
*
|
|
163
|
-
* These libraries ship code that still needs babel:
|
|
164
162
|
* - ember-concurrency's decorators
|
|
165
|
-
* - ember-scoped-css transforms
|
|
166
163
|
* - ember-intl's formatMessage macro
|
|
167
|
-
*
|
|
168
|
-
*
|
|
164
|
+
* - ember-scoped-css transforms
|
|
165
|
+
* - unplugin-icons, whose icon modules compile with `template()`.
|
|
166
|
+
* Their ids have no file extension, so only the id matches them.
|
|
167
|
+
*/
|
|
168
|
+
const LIBRARY_FILTERS = {
|
|
169
|
+
"ember-concurrency": { imports: ["ember-concurrency"] },
|
|
170
|
+
"ember-intl": {
|
|
171
|
+
imports: ["ember-intl/helpers/format-message"],
|
|
172
|
+
code: [/\bintl\.formatMessage\b/]
|
|
173
|
+
},
|
|
174
|
+
"ember-scoped-css": { imports: ["ember-scoped-css"] },
|
|
175
|
+
"unplugin-icons": { ids: [/^~icons\//] }
|
|
176
|
+
};
|
|
177
|
+
function projectDependencies() {
|
|
178
|
+
const manifestPath = join(process.cwd(), "package.json");
|
|
179
|
+
if (!existsSync(manifestPath)) return /* @__PURE__ */ new Set();
|
|
180
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
|
|
181
|
+
return new Set(Object.keys({
|
|
182
|
+
...manifest.dependencies,
|
|
183
|
+
...manifest.devDependencies
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Additional opt-ins beyond maybeBabel's built-in list.
|
|
188
|
+
* The `initializeRuntimeMacrosConfig` runtime call must compile away in every project.
|
|
169
189
|
*/
|
|
170
190
|
function babelFilter(nvpConfig) {
|
|
191
|
+
const imports = [];
|
|
192
|
+
const code = ["initializeRuntimeMacrosConfig"];
|
|
193
|
+
const ids = [];
|
|
194
|
+
const dependencies = projectDependencies();
|
|
195
|
+
for (const [name, filter] of Object.entries(LIBRARY_FILTERS)) {
|
|
196
|
+
if (!dependencies.has(name)) continue;
|
|
197
|
+
for (const specifier of filter.imports ?? []) imports.push(specifier);
|
|
198
|
+
for (const pattern of filter.code ?? []) code.push(pattern);
|
|
199
|
+
for (const pattern of filter.ids ?? []) ids.push(pattern);
|
|
200
|
+
}
|
|
201
|
+
for (const specifier of nvpConfig.babel?.include?.whenImporting ?? []) imports.push(specifier);
|
|
202
|
+
for (const pattern of nvpConfig.babel?.include?.whenMatching ?? []) code.push(pattern);
|
|
171
203
|
return { include: {
|
|
172
|
-
imports
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
"ember-intl/helpers/format-message",
|
|
176
|
-
...nvpConfig.babel?.include?.whenImporting ?? []
|
|
177
|
-
],
|
|
178
|
-
code: [
|
|
179
|
-
/\bintl\.formatMessage\b/,
|
|
180
|
-
"initializeRuntimeMacrosConfig",
|
|
181
|
-
...nvpConfig.babel?.include?.whenMatching ?? []
|
|
182
|
-
]
|
|
204
|
+
imports,
|
|
205
|
+
code,
|
|
206
|
+
ids
|
|
183
207
|
} };
|
|
184
208
|
}
|
|
185
209
|
function ember(nvpConfig = {}) {
|
|
186
210
|
const babel = babelOptions(nvpConfig);
|
|
187
211
|
const filter = babelFilter(nvpConfig);
|
|
212
|
+
const parallel = nvpConfig.babel?.parallel ?? babel.configFile !== false;
|
|
188
213
|
return [
|
|
189
214
|
{
|
|
190
215
|
name: "ember:nullvoxpopuli",
|
|
@@ -217,7 +242,7 @@ function ember(nvpConfig = {}) {
|
|
|
217
242
|
templateTag(),
|
|
218
243
|
maybeBabel$1({
|
|
219
244
|
...babel,
|
|
220
|
-
parallel
|
|
245
|
+
parallel,
|
|
221
246
|
filter
|
|
222
247
|
})
|
|
223
248
|
];
|
|
@@ -228,7 +253,7 @@ function ember(nvpConfig = {}) {
|
|
|
228
253
|
testHtml(),
|
|
229
254
|
maybeBabel$1({
|
|
230
255
|
...babel,
|
|
231
|
-
parallel
|
|
256
|
+
parallel,
|
|
232
257
|
filter
|
|
233
258
|
})
|
|
234
259
|
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nullvoxpopuli/ember-vite",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Meta plugin for ember projects focusing on speed.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"@embroider/core": "^4.6.9",
|
|
22
22
|
"@embroider/macros": "^1.21.3",
|
|
23
23
|
"@embroider/vite": "^1.7.15",
|
|
24
|
-
"@nullvoxpopuli/ember-build-tooling-utils": "
|
|
24
|
+
"@nullvoxpopuli/ember-build-tooling-utils": "2.0.0"
|
|
25
25
|
},
|
|
26
26
|
"engines": {
|
|
27
27
|
"node": ">= 24"
|
package/src/ember.test.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { extensions } from "@embroider/vite";
|
|
2
2
|
import { interpreter, type TopLevelFilterExpression } from "@rolldown/pluginutils";
|
|
3
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
3
5
|
import { fileURLToPath } from "node:url";
|
|
4
6
|
import type { ConfigEnv, ConfigPluginContext, Plugin } from "vite";
|
|
5
7
|
import { afterEach, describe, expect, it } from "vitest";
|
|
@@ -92,6 +94,20 @@ const CONTEXT = {
|
|
|
92
94
|
},
|
|
93
95
|
};
|
|
94
96
|
|
|
97
|
+
/**
|
|
98
|
+
* The babel plugin, after the hooks that rolldown runs before a transform.
|
|
99
|
+
*/
|
|
100
|
+
async function startedBabel(plugins: Plugin[]) {
|
|
101
|
+
const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel")!;
|
|
102
|
+
|
|
103
|
+
// @ts-expect-error the babel plugin has function hooks
|
|
104
|
+
await babel.buildStart.call(CONTEXT);
|
|
105
|
+
// @ts-expect-error the babel plugin has function hooks
|
|
106
|
+
babel.outputOptions.call({ meta: {} }, {});
|
|
107
|
+
|
|
108
|
+
return babel;
|
|
109
|
+
}
|
|
110
|
+
|
|
95
111
|
/**
|
|
96
112
|
* Runs the config hook, then the babel plugin, the way vite does.
|
|
97
113
|
*/
|
|
@@ -102,27 +118,70 @@ async function compile(mode: string) {
|
|
|
102
118
|
// @ts-expect-error the first entry is the config plugin
|
|
103
119
|
await plugins[0].config.call(ROLLDOWN, config, { command: "build", mode });
|
|
104
120
|
|
|
105
|
-
const babel = plugins
|
|
121
|
+
const babel = await startedBabel(plugins);
|
|
106
122
|
// @ts-expect-error the babel plugin has an object transform hook
|
|
107
123
|
const result = await babel.transform.handler.call(CONTEXT, DEV_CHECK, `${PACKAGE_ROOT}app.js`);
|
|
108
124
|
|
|
109
125
|
return result.code as string;
|
|
110
126
|
}
|
|
111
127
|
|
|
112
|
-
|
|
113
|
-
let restoreCwd: (() => void) | undefined;
|
|
128
|
+
let restoreCwd: (() => void) | undefined;
|
|
114
129
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
130
|
+
afterEach(() => {
|
|
131
|
+
restoreCwd?.();
|
|
132
|
+
restoreCwd = undefined;
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
function inDirectory(directory: string) {
|
|
136
|
+
const previous = process.cwd();
|
|
137
|
+
process.chdir(directory);
|
|
138
|
+
restoreCwd = () => process.chdir(previous);
|
|
139
|
+
}
|
|
119
140
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
141
|
+
function inPackageRoot() {
|
|
142
|
+
inDirectory(PACKAGE_ROOT);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A project with these files.
|
|
147
|
+
* It is inside this package, so babel resolves its plugins from here.
|
|
148
|
+
*/
|
|
149
|
+
function inProject(files: Record<string, string>) {
|
|
150
|
+
const cache = join(PACKAGE_ROOT, "node_modules/.cache");
|
|
151
|
+
mkdirSync(cache, { recursive: true });
|
|
152
|
+
|
|
153
|
+
const directory = mkdtempSync(join(cache, "project-"));
|
|
154
|
+
|
|
155
|
+
for (const [name, content] of Object.entries(files)) {
|
|
156
|
+
writeFileSync(join(directory, name), content);
|
|
124
157
|
}
|
|
125
158
|
|
|
159
|
+
inDirectory(directory);
|
|
160
|
+
|
|
161
|
+
const restore = restoreCwd!;
|
|
162
|
+
restoreCwd = () => {
|
|
163
|
+
restore();
|
|
164
|
+
rmSync(directory, { recursive: true, force: true });
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
return directory;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* A project that depends on the libraries that need babel.
|
|
172
|
+
*/
|
|
173
|
+
function inProjectWithLibraries() {
|
|
174
|
+
const devDependencies = {
|
|
175
|
+
"ember-concurrency": "*",
|
|
176
|
+
"ember-intl": "*",
|
|
177
|
+
"ember-scoped-css": "*",
|
|
178
|
+
"unplugin-icons": "*",
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
inProject({ "package.json": JSON.stringify({ devDependencies }) });
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
describe("default babel plugins", () => {
|
|
126
185
|
/**
|
|
127
186
|
* vite sets NODE_ENV from the mode only after the config hooks run.
|
|
128
187
|
* Under vitest, NODE_ENV is "test".
|
|
@@ -145,14 +204,121 @@ describe("default babel plugins", () => {
|
|
|
145
204
|
});
|
|
146
205
|
});
|
|
147
206
|
|
|
207
|
+
describe("babel config file", () => {
|
|
208
|
+
/**
|
|
209
|
+
* The plugins of a config file are not in the options of `@rolldown/plugin-babel`,
|
|
210
|
+
* and without plugins in its options, that plugin turns itself off in vite.
|
|
211
|
+
*/
|
|
212
|
+
it("keeps babel on in each vite environment", async () => {
|
|
213
|
+
inProject({
|
|
214
|
+
"package.json": `{ "type": "module" }`,
|
|
215
|
+
"babel.config.js": `export default { plugins: [] };`,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const plugins = ember() as Plugin[];
|
|
219
|
+
const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel")!;
|
|
220
|
+
|
|
221
|
+
// @ts-expect-error the babel plugin has function hooks
|
|
222
|
+
await babel.configResolved.call(CONTEXT, {});
|
|
223
|
+
|
|
224
|
+
// @ts-expect-error the babel plugin has function hooks
|
|
225
|
+
expect(babel.applyToEnvironment.call(CONTEXT, { name: "client" })).toBe(true);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
describe("babel filter in the dev server", () => {
|
|
230
|
+
/**
|
|
231
|
+
* The vite dev server ignores a filter made of filter expressions,
|
|
232
|
+
* so it gives the babel plugin every module, also the styles of index.html.
|
|
233
|
+
*/
|
|
234
|
+
it("leaves a module outside the filter to oxc", async () => {
|
|
235
|
+
inPackageRoot();
|
|
236
|
+
|
|
237
|
+
const plugins = ember() as Plugin[];
|
|
238
|
+
|
|
239
|
+
// @ts-expect-error the first entry is the config plugin
|
|
240
|
+
await plugins[0].config.call(ROLLDOWN, {}, { command: "serve", mode: "development" });
|
|
241
|
+
|
|
242
|
+
const babel = await startedBabel(plugins);
|
|
243
|
+
const styles = `${PACKAGE_ROOT}index.html?html-proxy&direct&index=0.css`;
|
|
244
|
+
|
|
245
|
+
// @ts-expect-error the babel plugin has an object transform hook
|
|
246
|
+
const result = await babel.transform.handler.call(CONTEXT, `:root { color: red; }`, styles);
|
|
247
|
+
|
|
248
|
+
expect(result).toBeUndefined();
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe("babel.parallel", () => {
|
|
253
|
+
/**
|
|
254
|
+
* A babel plugin that writes which thread compiled the file.
|
|
255
|
+
*/
|
|
256
|
+
const THREAD_PLUGIN = [
|
|
257
|
+
`import { isMainThread } from "node:worker_threads";`,
|
|
258
|
+
`export default ({ types: t }) => ({`,
|
|
259
|
+
` visitor: {`,
|
|
260
|
+
` Program(path) {`,
|
|
261
|
+
` const thread = isMainThread ? "main thread" : "worker thread";`,
|
|
262
|
+
` path.unshiftContainer("body", t.expressionStatement(t.stringLiteral(thread)));`,
|
|
263
|
+
` },`,
|
|
264
|
+
` },`,
|
|
265
|
+
`});`,
|
|
266
|
+
].join("\n");
|
|
267
|
+
|
|
268
|
+
async function compileWithConfigFile(options: Parameters<typeof ember>[0] = {}) {
|
|
269
|
+
const directory = inProject({
|
|
270
|
+
"package.json": `{ "type": "module" }`,
|
|
271
|
+
"thread-plugin.js": THREAD_PLUGIN,
|
|
272
|
+
"babel.config.js": `export default { plugins: ["./thread-plugin.js"] };`,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
const plugins = ember(options) as Plugin[];
|
|
276
|
+
const babel = await startedBabel(plugins);
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
// @ts-expect-error the babel plugin has an object transform hook
|
|
280
|
+
const result = await babel.transform.handler.call(
|
|
281
|
+
CONTEXT,
|
|
282
|
+
`import { macroCondition } from "@embroider/macros";`,
|
|
283
|
+
join(directory, "app/a.js"),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
return result.code as string;
|
|
287
|
+
} finally {
|
|
288
|
+
// @ts-expect-error the babel plugin ends its workers in closeBundle
|
|
289
|
+
await babel.closeBundle.call({ meta: { watchMode: false } });
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
it("runs babel in worker threads when the project has a babel config file", async () => {
|
|
294
|
+
expect(await compileWithConfigFile()).toContain("worker thread");
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("runs babel in the main thread when parallel is false", async () => {
|
|
298
|
+
expect(await compileWithConfigFile({ babel: { parallel: false } })).toContain("main thread");
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* The module that unplugin-icons gives for an icon, with `compiler: "ember"`.
|
|
304
|
+
*/
|
|
305
|
+
const ICON = [
|
|
306
|
+
`import { template } from "@ember/template-compiler";`,
|
|
307
|
+
`export default template("<svg ...attributes><path d='M20 6L9 17l-5-5'/></svg>");`,
|
|
308
|
+
].join("\n");
|
|
309
|
+
|
|
148
310
|
describe("babel.include", () => {
|
|
149
|
-
function usesBabel(
|
|
311
|
+
function usesBabel(
|
|
312
|
+
options: Parameters<typeof ember>[0],
|
|
313
|
+
source: string,
|
|
314
|
+
moduleId = "/app/services/a.ts",
|
|
315
|
+
) {
|
|
150
316
|
const plugins = ember(options) as Plugin[];
|
|
151
317
|
const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel");
|
|
152
318
|
// @ts-expect-error maybeBabel sets the filter on the transform hook
|
|
153
319
|
const filter = babel.transform.filter as TopLevelFilterExpression[];
|
|
154
320
|
|
|
155
|
-
return interpreter(filter, source,
|
|
321
|
+
return interpreter(filter, source, moduleId);
|
|
156
322
|
}
|
|
157
323
|
|
|
158
324
|
const source = `owner.register(Key, Impl);`;
|
|
@@ -180,3 +346,94 @@ describe("babel.include", () => {
|
|
|
180
346
|
).toBe(true);
|
|
181
347
|
});
|
|
182
348
|
});
|
|
349
|
+
|
|
350
|
+
describe("libraries that need babel", () => {
|
|
351
|
+
function babelPerModule() {
|
|
352
|
+
const plugins = ember() as Plugin[];
|
|
353
|
+
const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel");
|
|
354
|
+
// @ts-expect-error maybeBabel sets the filter on the transform hook
|
|
355
|
+
const filter = babel.transform.filter as TopLevelFilterExpression[];
|
|
356
|
+
|
|
357
|
+
return {
|
|
358
|
+
"ember-concurrency": interpreter(
|
|
359
|
+
filter,
|
|
360
|
+
`import { task } from "ember-concurrency";`,
|
|
361
|
+
"/app/components/a.js",
|
|
362
|
+
),
|
|
363
|
+
"ember-intl import": interpreter(
|
|
364
|
+
filter,
|
|
365
|
+
`import formatMessage from "ember-intl/helpers/format-message";`,
|
|
366
|
+
"/app/components/a.js",
|
|
367
|
+
),
|
|
368
|
+
"ember-intl formatMessage": interpreter(
|
|
369
|
+
filter,
|
|
370
|
+
`export const title = this.intl.formatMessage("Hello");`,
|
|
371
|
+
"/app/components/a.js",
|
|
372
|
+
),
|
|
373
|
+
"ember-scoped-css": interpreter(
|
|
374
|
+
filter,
|
|
375
|
+
`import { scopedClass } from "ember-scoped-css";`,
|
|
376
|
+
"/app/components/a.js",
|
|
377
|
+
),
|
|
378
|
+
"unplugin-icons": interpreter(filter, ICON, "~icons/lucide/check"),
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
it("leaves their modules to oxc when the project does not depend on them", () => {
|
|
383
|
+
inPackageRoot();
|
|
384
|
+
|
|
385
|
+
expect(babelPerModule()).toMatchInlineSnapshot(`
|
|
386
|
+
{
|
|
387
|
+
"ember-concurrency": false,
|
|
388
|
+
"ember-intl formatMessage": false,
|
|
389
|
+
"ember-intl import": false,
|
|
390
|
+
"ember-scoped-css": false,
|
|
391
|
+
"unplugin-icons": false,
|
|
392
|
+
}
|
|
393
|
+
`);
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it("sends their modules to babel when the project depends on them", () => {
|
|
397
|
+
inProjectWithLibraries();
|
|
398
|
+
|
|
399
|
+
expect(babelPerModule()).toMatchInlineSnapshot(`
|
|
400
|
+
{
|
|
401
|
+
"ember-concurrency": true,
|
|
402
|
+
"ember-intl formatMessage": true,
|
|
403
|
+
"ember-intl import": true,
|
|
404
|
+
"ember-scoped-css": true,
|
|
405
|
+
"unplugin-icons": true,
|
|
406
|
+
}
|
|
407
|
+
`);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
it("compiles the template of an icon module, which has no extension", async () => {
|
|
411
|
+
inProjectWithLibraries();
|
|
412
|
+
|
|
413
|
+
const plugins = ember() as Plugin[];
|
|
414
|
+
const config: any = {};
|
|
415
|
+
|
|
416
|
+
// @ts-expect-error the first entry is the config plugin
|
|
417
|
+
await plugins[0].config.call(ROLLDOWN, config, { command: "build", mode: "production" });
|
|
418
|
+
|
|
419
|
+
const babel = await startedBabel(plugins);
|
|
420
|
+
// @ts-expect-error the babel plugin has an object transform hook
|
|
421
|
+
const result = await babel.transform.handler.call(CONTEXT, ICON, "~icons/lucide/check");
|
|
422
|
+
|
|
423
|
+
expect(result.code).toMatchInlineSnapshot(`
|
|
424
|
+
"import { setComponentTemplate } from "@ember/component";
|
|
425
|
+
import { createTemplateFactory } from "@ember/template-factory";
|
|
426
|
+
import templateOnly from "@ember/component/template-only";
|
|
427
|
+
export default setComponentTemplate(createTemplateFactory(
|
|
428
|
+
/*
|
|
429
|
+
<svg ...attributes><path d='M20 6L9 17l-5-5'/></svg>
|
|
430
|
+
*/
|
|
431
|
+
{
|
|
432
|
+
"id": null,
|
|
433
|
+
"block": "[[[11,\\"svg\\"],[17,1],[12],[10,\\"path\\"],[14,\\"d\\",\\"M20 6L9 17l-5-5\\"],[12],[13],[13]],[\\"&attrs\\"],[]]",
|
|
434
|
+
"moduleName": "(unknown template module)",
|
|
435
|
+
"isStrictMode": true
|
|
436
|
+
}), templateOnly());"
|
|
437
|
+
`);
|
|
438
|
+
});
|
|
439
|
+
});
|
package/src/ember.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { buildMacros } from "@embroider/macros/babel";
|
|
2
2
|
import { ember as embroiderEmber, extensions, resolver, templateTag } from "@embroider/vite";
|
|
3
|
-
import { existsSync } from "node:fs";
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
4
|
import { join, resolve } from "node:path";
|
|
5
5
|
import type { ConfigEnv, ConfigPluginContext, ResolvedConfig, UserConfig } from "vite";
|
|
6
6
|
|
|
@@ -93,8 +93,6 @@ type BabelOptions =
|
|
|
93
93
|
| { configFile: string }
|
|
94
94
|
| {
|
|
95
95
|
configFile: false;
|
|
96
|
-
babelrc: false;
|
|
97
|
-
babelHelpers: "inline";
|
|
98
96
|
plugins: ReturnType<typeof defaultBabelPlugins>;
|
|
99
97
|
};
|
|
100
98
|
|
|
@@ -104,8 +102,7 @@ type BabelOptions =
|
|
|
104
102
|
*
|
|
105
103
|
* Without one (libraries), `defaultBabelPlugins` covers TypeScript, templates, macros, and decorators,
|
|
106
104
|
* so no config file is required.
|
|
107
|
-
*
|
|
108
|
-
* Helpers are inlined in that case:
|
|
105
|
+
* Babel helpers are inlined in that case:
|
|
109
106
|
* projects without a babel config don't carry `@babel/runtime`.
|
|
110
107
|
*/
|
|
111
108
|
function babelOptions(nvpConfig: Config): BabelOptions {
|
|
@@ -123,45 +120,82 @@ function babelOptions(nvpConfig: Config): BabelOptions {
|
|
|
123
120
|
*/
|
|
124
121
|
return {
|
|
125
122
|
configFile: false,
|
|
126
|
-
babelrc: false,
|
|
127
|
-
babelHelpers: "inline",
|
|
128
123
|
plugins: [],
|
|
129
124
|
};
|
|
130
125
|
}
|
|
131
126
|
|
|
132
127
|
/**
|
|
133
|
-
*
|
|
128
|
+
* Libraries that ship code that still needs babel.
|
|
129
|
+
* Each one adds to the filter only when the project depends on it,
|
|
130
|
+
* so other projects do not pay for the check.
|
|
134
131
|
*
|
|
135
|
-
* These libraries ship code that still needs babel:
|
|
136
132
|
* - ember-concurrency's decorators
|
|
137
|
-
* - ember-scoped-css transforms
|
|
138
133
|
* - ember-intl's formatMessage macro
|
|
139
|
-
*
|
|
140
|
-
*
|
|
134
|
+
* - ember-scoped-css transforms
|
|
135
|
+
* - unplugin-icons, whose icon modules compile with `template()`.
|
|
136
|
+
* Their ids have no file extension, so only the id matches them.
|
|
137
|
+
*/
|
|
138
|
+
const LIBRARY_FILTERS: Record<
|
|
139
|
+
string,
|
|
140
|
+
{ imports?: string[]; code?: (string | RegExp)[]; ids?: RegExp[] }
|
|
141
|
+
> = {
|
|
142
|
+
"ember-concurrency": { imports: ["ember-concurrency"] },
|
|
143
|
+
"ember-intl": {
|
|
144
|
+
imports: ["ember-intl/helpers/format-message"],
|
|
145
|
+
code: [/\bintl\.formatMessage\b/],
|
|
146
|
+
},
|
|
147
|
+
"ember-scoped-css": { imports: ["ember-scoped-css"] },
|
|
148
|
+
"unplugin-icons": { ids: [/^~icons\//] },
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
function projectDependencies(): Set<string> {
|
|
152
|
+
const manifestPath = join(process.cwd(), "package.json");
|
|
153
|
+
|
|
154
|
+
if (!existsSync(manifestPath)) return new Set();
|
|
155
|
+
|
|
156
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
|
|
157
|
+
|
|
158
|
+
return new Set(Object.keys({ ...manifest.dependencies, ...manifest.devDependencies }));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Additional opt-ins beyond maybeBabel's built-in list.
|
|
163
|
+
* The `initializeRuntimeMacrosConfig` runtime call must compile away in every project.
|
|
141
164
|
*/
|
|
142
165
|
function babelFilter(nvpConfig: Config) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
166
|
+
const imports: string[] = [];
|
|
167
|
+
const code: (string | RegExp)[] = ["initializeRuntimeMacrosConfig"];
|
|
168
|
+
const ids: RegExp[] = [];
|
|
169
|
+
const dependencies = projectDependencies();
|
|
170
|
+
|
|
171
|
+
for (const [name, filter] of Object.entries(LIBRARY_FILTERS)) {
|
|
172
|
+
if (!dependencies.has(name)) continue;
|
|
173
|
+
|
|
174
|
+
for (const specifier of filter.imports ?? []) imports.push(specifier);
|
|
175
|
+
for (const pattern of filter.code ?? []) code.push(pattern);
|
|
176
|
+
for (const pattern of filter.ids ?? []) ids.push(pattern);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
for (const specifier of nvpConfig.babel?.include?.whenImporting ?? []) {
|
|
180
|
+
imports.push(specifier);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
for (const pattern of nvpConfig.babel?.include?.whenMatching ?? []) {
|
|
184
|
+
code.push(pattern);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { include: { imports, code, ids } };
|
|
158
188
|
}
|
|
159
189
|
|
|
160
190
|
interface Config {
|
|
161
191
|
babel?: {
|
|
162
192
|
/**
|
|
163
|
-
* defaults to true.
|
|
164
193
|
* when babel runs, do so in parallel across many cores.
|
|
194
|
+
*
|
|
195
|
+
* defaults to true when the project has a babel config file.
|
|
196
|
+
* Without one, babel stays in the main thread:
|
|
197
|
+
* the default plugins carry functions,
|
|
198
|
+
* and a worker thread cannot receive functions.
|
|
165
199
|
*/
|
|
166
200
|
parallel?: false;
|
|
167
201
|
|
|
@@ -186,7 +220,7 @@ interface Config {
|
|
|
186
220
|
* if a file imports from anything in this array, use babel instead of
|
|
187
221
|
* oxc for that file.
|
|
188
222
|
*
|
|
189
|
-
* For example: ['
|
|
223
|
+
* For example: ['my-babel-only-library'],
|
|
190
224
|
*/
|
|
191
225
|
whenImporting?: (string | "ember-concurrency" | "ember-scoped-css")[];
|
|
192
226
|
|
|
@@ -221,6 +255,7 @@ interface Config {
|
|
|
221
255
|
export function ember(nvpConfig: Config = {}) {
|
|
222
256
|
const babel = babelOptions(nvpConfig);
|
|
223
257
|
const filter = babelFilter(nvpConfig);
|
|
258
|
+
const parallel = nvpConfig.babel?.parallel ?? babel.configFile !== false;
|
|
224
259
|
|
|
225
260
|
/*
|
|
226
261
|
* Plugins must be returned as top-level array entries,
|
|
@@ -282,7 +317,7 @@ export function ember(nvpConfig: Config = {}) {
|
|
|
282
317
|
// Both must compile away here, with this app's template compiler.
|
|
283
318
|
maybeBabel({
|
|
284
319
|
...babel,
|
|
285
|
-
parallel
|
|
320
|
+
parallel,
|
|
286
321
|
filter,
|
|
287
322
|
}),
|
|
288
323
|
];
|
|
@@ -293,7 +328,7 @@ export function ember(nvpConfig: Config = {}) {
|
|
|
293
328
|
testHtml(),
|
|
294
329
|
maybeBabel({
|
|
295
330
|
...babel,
|
|
296
|
-
parallel
|
|
331
|
+
parallel,
|
|
297
332
|
filter,
|
|
298
333
|
}),
|
|
299
334
|
];
|