@nullvoxpopuli/ember-vite 1.6.0 → 2.1.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/dist/index.js CHANGED
@@ -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,8 +151,6 @@ 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
  }
@@ -212,6 +209,7 @@ function babelFilter(nvpConfig) {
212
209
  function ember(nvpConfig = {}) {
213
210
  const babel = babelOptions(nvpConfig);
214
211
  const filter = babelFilter(nvpConfig);
212
+ const parallel = nvpConfig.babel?.parallel ?? babel.configFile !== false;
215
213
  return [
216
214
  {
217
215
  name: "ember:nullvoxpopuli",
@@ -244,7 +242,7 @@ function ember(nvpConfig = {}) {
244
242
  templateTag(),
245
243
  maybeBabel$1({
246
244
  ...babel,
247
- parallel: nvpConfig.babel?.parallel,
245
+ parallel,
248
246
  filter
249
247
  })
250
248
  ];
@@ -255,7 +253,7 @@ function ember(nvpConfig = {}) {
255
253
  testHtml(),
256
254
  maybeBabel$1({
257
255
  ...babel,
258
- parallel: nvpConfig.babel?.parallel,
256
+ parallel,
259
257
  filter
260
258
  })
261
259
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nullvoxpopuli/ember-vite",
3
- "version": "1.6.0",
3
+ "version": "2.1.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": "1.3.0"
24
+ "@nullvoxpopuli/ember-build-tooling-utils": "2.0.1"
25
25
  },
26
26
  "engines": {
27
27
  "node": ">= 24"
package/src/ember.test.ts CHANGED
@@ -1,7 +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
+ import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
4
+ import { dirname, join } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
6
  import type { ConfigEnv, ConfigPluginContext, Plugin } from "vite";
7
7
  import { afterEach, describe, expect, it } from "vitest";
@@ -94,6 +94,20 @@ const CONTEXT = {
94
94
  },
95
95
  };
96
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
+
97
111
  /**
98
112
  * Runs the config hook, then the babel plugin, the way vite does.
99
113
  */
@@ -104,7 +118,7 @@ async function compile(mode: string) {
104
118
  // @ts-expect-error the first entry is the config plugin
105
119
  await plugins[0].config.call(ROLLDOWN, config, { command: "build", mode });
106
120
 
107
- const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel");
121
+ const babel = await startedBabel(plugins);
108
122
  // @ts-expect-error the babel plugin has an object transform hook
109
123
  const result = await babel.transform.handler.call(CONTEXT, DEV_CHECK, `${PACKAGE_ROOT}app.js`);
110
124
 
@@ -129,22 +143,20 @@ function inPackageRoot() {
129
143
  }
130
144
 
131
145
  /**
132
- * A project that depends on the libraries that need babel.
146
+ * A project with these files.
133
147
  * It is inside this package, so babel resolves its plugins from here.
134
148
  */
135
- function inProjectWithLibraries() {
149
+ function inProject(files: Record<string, string>) {
136
150
  const cache = join(PACKAGE_ROOT, "node_modules/.cache");
137
151
  mkdirSync(cache, { recursive: true });
138
152
 
139
153
  const directory = mkdtempSync(join(cache, "project-"));
140
- const devDependencies = {
141
- "ember-concurrency": "*",
142
- "ember-intl": "*",
143
- "ember-scoped-css": "*",
144
- "unplugin-icons": "*",
145
- };
146
154
 
147
- writeFileSync(join(directory, "package.json"), JSON.stringify({ devDependencies }));
155
+ for (const [name, content] of Object.entries(files)) {
156
+ mkdirSync(dirname(join(directory, name)), { recursive: true });
157
+ writeFileSync(join(directory, name), content);
158
+ }
159
+
148
160
  inDirectory(directory);
149
161
 
150
162
  const restore = restoreCwd!;
@@ -152,6 +164,22 @@ function inProjectWithLibraries() {
152
164
  restore();
153
165
  rmSync(directory, { recursive: true, force: true });
154
166
  };
167
+
168
+ return directory;
169
+ }
170
+
171
+ /**
172
+ * A project that depends on the libraries that need babel.
173
+ */
174
+ function inProjectWithLibraries() {
175
+ const devDependencies = {
176
+ "ember-concurrency": "*",
177
+ "ember-intl": "*",
178
+ "ember-scoped-css": "*",
179
+ "unplugin-icons": "*",
180
+ };
181
+
182
+ inProject({ "package.json": JSON.stringify({ devDependencies }) });
155
183
  }
156
184
 
157
185
  describe("default babel plugins", () => {
@@ -177,6 +205,147 @@ describe("default babel plugins", () => {
177
205
  });
178
206
  });
179
207
 
208
+ describe("babel config file", () => {
209
+ /**
210
+ * The plugins of a config file are not in the options of `@rolldown/plugin-babel`,
211
+ * and without plugins in its options, that plugin turns itself off in vite.
212
+ */
213
+ it("keeps babel on in each vite environment", async () => {
214
+ inProject({
215
+ "package.json": `{ "type": "module" }`,
216
+ "babel.config.js": `export default { plugins: [] };`,
217
+ });
218
+
219
+ const plugins = ember() as Plugin[];
220
+ const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel")!;
221
+
222
+ // @ts-expect-error the babel plugin has function hooks
223
+ await babel.configResolved.call(CONTEXT, {});
224
+
225
+ // @ts-expect-error the babel plugin has function hooks
226
+ expect(babel.applyToEnvironment.call(CONTEXT, { name: "client" })).toBe(true);
227
+ });
228
+ });
229
+
230
+ describe("babel filter in the dev server", () => {
231
+ /**
232
+ * The vite dev server ignores a filter made of filter expressions,
233
+ * so it gives the babel plugin every module, also the styles of index.html.
234
+ */
235
+ it("leaves a module outside the filter to oxc", async () => {
236
+ inPackageRoot();
237
+
238
+ const plugins = ember() as Plugin[];
239
+
240
+ // @ts-expect-error the first entry is the config plugin
241
+ await plugins[0].config.call(ROLLDOWN, {}, { command: "serve", mode: "development" });
242
+
243
+ const babel = await startedBabel(plugins);
244
+ const styles = `${PACKAGE_ROOT}index.html?html-proxy&direct&index=0.css`;
245
+
246
+ // @ts-expect-error the babel plugin has an object transform hook
247
+ const result = await babel.transform.handler.call(CONTEXT, `:root { color: red; }`, styles);
248
+
249
+ expect(result).toBeUndefined();
250
+ });
251
+ });
252
+
253
+ describe("babel.parallel", () => {
254
+ /**
255
+ * A babel plugin that writes which thread compiled the file.
256
+ */
257
+ const THREAD_PLUGIN = [
258
+ `import { isMainThread } from "node:worker_threads";`,
259
+ `export default ({ types: t }) => ({`,
260
+ ` visitor: {`,
261
+ ` Program(path) {`,
262
+ ` const thread = isMainThread ? "main thread" : "worker thread";`,
263
+ ` path.unshiftContainer("body", t.expressionStatement(t.stringLiteral(thread)));`,
264
+ ` },`,
265
+ ` },`,
266
+ `});`,
267
+ ].join("\n");
268
+
269
+ async function compileWithConfigFile(
270
+ options: Parameters<typeof ember>[0] = {},
271
+ config = `export default { plugins: ["./thread-plugin.js"] };`,
272
+ files: Record<string, string> = {},
273
+ ) {
274
+ const directory = inProject(
275
+ Object.assign(
276
+ {
277
+ "package.json": `{ "type": "module" }`,
278
+ "thread-plugin.js": THREAD_PLUGIN,
279
+ "babel.config.js": config,
280
+ },
281
+ files,
282
+ ),
283
+ );
284
+
285
+ const plugins = ember(options) as Plugin[];
286
+ const babel = await startedBabel(plugins);
287
+
288
+ try {
289
+ // @ts-expect-error the babel plugin has an object transform hook
290
+ const result = await babel.transform.handler.call(
291
+ CONTEXT,
292
+ `import { macroCondition } from "@embroider/macros";`,
293
+ join(directory, "app/a.js"),
294
+ );
295
+
296
+ return result.code as string;
297
+ } finally {
298
+ // @ts-expect-error the babel plugin ends its workers in closeBundle
299
+ await babel.closeBundle.call({ meta: { watchMode: false } });
300
+ }
301
+ }
302
+
303
+ it("runs babel in worker threads when the project has a babel config file", async () => {
304
+ expect(await compileWithConfigFile()).toContain("worker thread");
305
+ });
306
+
307
+ /**
308
+ * `@warp-drive/build-config` before 5.9 finds its own folder with `typeof __dirname`,
309
+ * which Node sets to "." in an eval worker thread.
310
+ * See https://github.com/warp-drive-data/warp-drive/pull/10926.
311
+ */
312
+ function buildConfig(version: string) {
313
+ return {
314
+ "node_modules/@warp-drive/build-config/package.json": JSON.stringify({
315
+ name: "@warp-drive/build-config",
316
+ version,
317
+ type: "module",
318
+ exports: { ".": "./index.js" },
319
+ }),
320
+ "node_modules/@warp-drive/build-config/index.js": [
321
+ `if (typeof __dirname !== "undefined") throw new Error("__dirname is " + __dirname);`,
322
+ `export const setConfig = () => {};`,
323
+ ].join("\n"),
324
+ };
325
+ }
326
+
327
+ const CONFIG_WITH_WARP_DRIVE = [
328
+ `import "@warp-drive/build-config";`,
329
+ `export default { plugins: ["./thread-plugin.js"] };`,
330
+ ].join("\n");
331
+
332
+ it("gives @warp-drive/build-config before 5.9 no __dirname in a worker thread", async () => {
333
+ expect(await compileWithConfigFile({}, CONFIG_WITH_WARP_DRIVE, buildConfig("5.8.2"))).toContain(
334
+ "worker thread",
335
+ );
336
+ expect(existsSync("node_modules/.cache/nullvoxpopuli-babel")).toBe(true);
337
+ });
338
+
339
+ it("loads the config file directly without @warp-drive/build-config before 5.9", async () => {
340
+ expect(await compileWithConfigFile()).toContain("worker thread");
341
+ expect(existsSync("node_modules/.cache/nullvoxpopuli-babel")).toBe(false);
342
+ });
343
+
344
+ it("runs babel in the main thread when parallel is false", async () => {
345
+ expect(await compileWithConfigFile({ babel: { parallel: false } })).toContain("main thread");
346
+ });
347
+ });
348
+
180
349
  /**
181
350
  * The module that unplugin-icons gives for an icon, with `compiler: "ember"`.
182
351
  */
@@ -294,7 +463,7 @@ describe("libraries that need babel", () => {
294
463
  // @ts-expect-error the first entry is the config plugin
295
464
  await plugins[0].config.call(ROLLDOWN, config, { command: "build", mode: "production" });
296
465
 
297
- const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel");
466
+ const babel = await startedBabel(plugins);
298
467
  // @ts-expect-error the babel plugin has an object transform hook
299
468
  const result = await babel.transform.handler.call(CONTEXT, ICON, "~icons/lucide/check");
300
469
 
package/src/ember.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { buildMacros } from "@embroider/macros/babel";
2
2
  import { ember as embroiderEmber, extensions, resolver, templateTag } from "@embroider/vite";
3
+ import type { PluginItem } from "@babel/core";
3
4
  import { existsSync, readFileSync } from "node:fs";
4
5
  import { join, resolve } from "node:path";
5
6
  import type { ConfigEnv, ConfigPluginContext, ResolvedConfig, UserConfig } from "vite";
@@ -64,7 +65,7 @@ function absolutePath(relativePath: string) {
64
65
  * `buildMacros` reads NODE_ENV, but vite sets NODE_ENV
65
66
  * only after the config hooks run.
66
67
  */
67
- function defaultBabelPlugins(mode: string) {
68
+ function defaultBabelPlugins(mode: string): PluginItem[] {
68
69
  const macros = buildMacros({
69
70
  configure(config) {
70
71
  if (isDev(mode)) {
@@ -93,8 +94,6 @@ type BabelOptions =
93
94
  | { configFile: string }
94
95
  | {
95
96
  configFile: false;
96
- babelrc: false;
97
- babelHelpers: "inline";
98
97
  plugins: ReturnType<typeof defaultBabelPlugins>;
99
98
  };
100
99
 
@@ -104,8 +103,7 @@ type BabelOptions =
104
103
  *
105
104
  * Without one (libraries), `defaultBabelPlugins` covers TypeScript, templates, macros, and decorators,
106
105
  * so no config file is required.
107
- *
108
- * Helpers are inlined in that case:
106
+ * Babel helpers are inlined in that case:
109
107
  * projects without a babel config don't carry `@babel/runtime`.
110
108
  */
111
109
  function babelOptions(nvpConfig: Config): BabelOptions {
@@ -123,8 +121,6 @@ function babelOptions(nvpConfig: Config): BabelOptions {
123
121
  */
124
122
  return {
125
123
  configFile: false,
126
- babelrc: false,
127
- babelHelpers: "inline",
128
124
  plugins: [],
129
125
  };
130
126
  }
@@ -195,8 +191,12 @@ function babelFilter(nvpConfig: Config) {
195
191
  interface Config {
196
192
  babel?: {
197
193
  /**
198
- * defaults to true.
199
194
  * when babel runs, do so in parallel across many cores.
195
+ *
196
+ * defaults to true when the project has a babel config file.
197
+ * Without one, babel stays in the main thread:
198
+ * the default plugins carry functions,
199
+ * and a worker thread cannot receive functions.
200
200
  */
201
201
  parallel?: false;
202
202
 
@@ -256,6 +256,7 @@ interface Config {
256
256
  export function ember(nvpConfig: Config = {}) {
257
257
  const babel = babelOptions(nvpConfig);
258
258
  const filter = babelFilter(nvpConfig);
259
+ const parallel = nvpConfig.babel?.parallel ?? babel.configFile !== false;
259
260
 
260
261
  /*
261
262
  * Plugins must be returned as top-level array entries,
@@ -317,7 +318,7 @@ export function ember(nvpConfig: Config = {}) {
317
318
  // Both must compile away here, with this app's template compiler.
318
319
  maybeBabel({
319
320
  ...babel,
320
- parallel: nvpConfig.babel?.parallel,
321
+ parallel,
321
322
  filter,
322
323
  }),
323
324
  ];
@@ -328,7 +329,7 @@ export function ember(nvpConfig: Config = {}) {
328
329
  testHtml(),
329
330
  maybeBabel({
330
331
  ...babel,
331
- parallel: nvpConfig.babel?.parallel,
332
+ parallel,
332
333
  filter,
333
334
  }),
334
335
  ];