@nullvoxpopuli/ember-vite 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -61,4 +61,8 @@ ember({
61
61
 
62
62
  Each option is documented on its type.
63
63
 
64
+ What this plugin sets in `build`, `build.rolldownOptions.output` and `build.rolldownOptions.optimization`
65
+ are defaults: a setting there in your own vite config wins,
66
+ for example `build.sourcemap: "hidden"` or `build.rolldownOptions.output.strictExecutionOrder: false`.
67
+
64
68
  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
@@ -214,6 +214,11 @@ function ember(nvpConfig = {}) {
214
214
  {
215
215
  name: "ember:nullvoxpopuli",
216
216
  async config(viteConfig, env) {
217
+ /**
218
+ * Read before the config hooks below write their defaults into `viteConfig`,
219
+ * and put back over them after `applyConfig`.
220
+ */
221
+ const project = projectConfig(viteConfig);
217
222
  /**
218
223
  * The hook reads the rolldown version from the plugin context,
219
224
  * and takes its esbuild path without it.
@@ -237,7 +242,9 @@ function ember(nvpConfig = {}) {
237
242
  viteConfig.optimizeDeps.rolldownOptions ||= {};
238
243
  viteConfig.optimizeDeps.rolldownOptions.tsconfig = true;
239
244
  applyConfig(viteConfig, env, nvpConfig);
245
+ keepProjectConfig(viteConfig, project);
240
246
  viteConfig.optimizeDeps.rolldownOptions.plugins = [
247
+ ...project.optimizeDepsPlugins,
241
248
  resolver({ rolldown: true }),
242
249
  templateTag(),
243
250
  maybeBabel$1({
@@ -259,6 +266,43 @@ function ember(nvpConfig = {}) {
259
266
  ];
260
267
  }
261
268
  /**
269
+ * What the project's own vite config sets, before any config hook changes it.
270
+ *
271
+ * What the config hooks here and in `@embroider/vite` set in `build`,
272
+ * `build.rolldownOptions.output` and `build.rolldownOptions.optimization` are defaults:
273
+ * a project that sets one, such as `build.sourcemap: "hidden"`
274
+ * or `build.rolldownOptions.output.strictExecutionOrder: false`, keeps its value.
275
+ * The project's own optimizeDeps plugins run before the ones this plugin adds.
276
+ */
277
+ function projectConfig(viteConfig) {
278
+ const build = viteConfig.build ?? {};
279
+ const output = build.rolldownOptions?.output;
280
+ const optimizeDepsPlugins = viteConfig.optimizeDeps?.rolldownOptions?.plugins;
281
+ return {
282
+ /**
283
+ * Vite 8 makes `build.rollupOptions` an accessor for `build.rolldownOptions`.
284
+ * Neither is put back: that would replace the object the config hooks fill in.
285
+ */
286
+ build: setValues(build, ["rolldownOptions", "rollupOptions"]),
287
+ output: Array.isArray(output) ? {} : setValues(output),
288
+ optimization: setValues(build.rolldownOptions?.optimization),
289
+ optimizeDepsPlugins: Array.isArray(optimizeDepsPlugins) ? [...optimizeDepsPlugins] : []
290
+ };
291
+ }
292
+ /**
293
+ * The entries that have a value.
294
+ * A project that sets `sourcemap: undefined` gets the default, as if it had not set it.
295
+ */
296
+ function setValues(object, skip = []) {
297
+ return Object.fromEntries(Object.entries(object ?? {}).filter(([key, value]) => value !== void 0 && !skip.includes(key)));
298
+ }
299
+ function keepProjectConfig(viteConfig, project) {
300
+ Object.assign(viteConfig.build, project.build);
301
+ const { output, optimization } = viteConfig.build.rolldownOptions ?? {};
302
+ if (output && !Array.isArray(output)) Object.assign(output, project.output);
303
+ if (optimization) Object.assign(optimization, project.optimization);
304
+ }
305
+ /**
262
306
  * `vite preview` serves the build on `$PORT`, the port that hosts and CI give,
263
307
  * or on 4200, the port of the dev server.
264
308
  * Vite itself reads no `$PORT`, and `@embroider/vite` only sets the dev server's port.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nullvoxpopuli/ember-vite",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
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": "2.0.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,9 +1,9 @@
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
- import type { ConfigEnv, ConfigPluginContext, Plugin } from "vite";
6
+ import { resolveConfig, type ConfigEnv, type ConfigPluginContext, type Plugin } from "vite";
7
7
  import { afterEach, describe, expect, it } from "vitest";
8
8
 
9
9
  import { ember } from "./ember.ts";
@@ -76,6 +76,96 @@ describe("ember", () => {
76
76
  });
77
77
  });
78
78
 
79
+ describe("the project's own vite config", () => {
80
+ it("keeps the project's build settings, and the defaults fill in the rest", async () => {
81
+ const config = await configure(
82
+ { command: "build", mode: "production" },
83
+ {
84
+ build: {
85
+ sourcemap: "hidden",
86
+ rolldownOptions: { output: { strictExecutionOrder: false } },
87
+ },
88
+ },
89
+ );
90
+
91
+ expect(config.build.sourcemap).toBe("hidden");
92
+ expect(config.build.rolldownOptions.output.strictExecutionOrder).toBe(false);
93
+ // Not the terser that @embroider/vite picks when the project sets no minifier.
94
+ expect(config.build.minify).toBe("oxc");
95
+ expect(config.build.rolldownOptions.output.minify).toBe(true);
96
+ });
97
+
98
+ it("keeps the project's build settings in development mode", async () => {
99
+ const config = await configure(
100
+ { command: "build", mode: "development" },
101
+ { build: { sourcemap: false, rolldownOptions: { output: { codeSplitting: false } } } },
102
+ );
103
+
104
+ expect(config.build.sourcemap).toBe(false);
105
+ expect(config.build.rolldownOptions.output.codeSplitting).toBe(false);
106
+ expect(config.build.minify).toBe(false);
107
+ });
108
+
109
+ it("keeps the project's optimization settings", async () => {
110
+ const config = await configure(
111
+ { command: "build", mode: "production" },
112
+ { build: { rolldownOptions: { optimization: { pifeForModuleWrappers: false } } } },
113
+ );
114
+
115
+ expect(config.build.rolldownOptions.optimization.pifeForModuleWrappers).toBe(false);
116
+ expect(config.build.rolldownOptions.optimization.inlineConst).toEqual({
117
+ mode: "smart",
118
+ pass: 3,
119
+ });
120
+ });
121
+
122
+ it("uses the default for a setting the project leaves undefined", async () => {
123
+ const config = await configure(
124
+ { command: "build", mode: "production" },
125
+ { build: { sourcemap: undefined } },
126
+ );
127
+
128
+ expect(config.build.sourcemap).toBe(true);
129
+ });
130
+
131
+ /**
132
+ * Vite gives the config hooks a `build.rollupOptions` accessor for `build.rolldownOptions`,
133
+ * which the plain objects of `configure()` don't have.
134
+ */
135
+ it("keeps what the config hooks put in build.rolldownOptions, through vite", async () => {
136
+ const config = await resolveConfig(
137
+ {
138
+ configFile: false,
139
+ logLevel: "silent",
140
+ plugins: [ember()],
141
+ build: { sourcemap: "hidden" },
142
+ },
143
+ "build",
144
+ "production",
145
+ );
146
+
147
+ expect(config.build.sourcemap).toBe("hidden");
148
+ expect(config.build.rolldownOptions.moduleTypes).toEqual({ ".ts": "js" });
149
+ expect(config.build.rolldownOptions.output).toMatchObject({ strictExecutionOrder: true });
150
+ });
151
+
152
+ it("runs the project's optimizeDeps plugins before its own", async () => {
153
+ const config = await configure(
154
+ { command: "serve", mode: "development" },
155
+ { optimizeDeps: { rolldownOptions: { plugins: [{ name: "project-plugin" }] } } },
156
+ );
157
+
158
+ expect(
159
+ config.optimizeDeps.rolldownOptions.plugins.map((plugin: Plugin) => plugin.name),
160
+ ).toEqual([
161
+ "project-plugin",
162
+ "embroider-resolver",
163
+ "embroider-template-tag",
164
+ "nullvoxpopuli:babel",
165
+ ]);
166
+ });
167
+ });
168
+
79
169
  /**
80
170
  * This package has no babel config,
81
171
  * so `ember()` uses its default babel plugins,
@@ -153,6 +243,7 @@ function inProject(files: Record<string, string>) {
153
243
  const directory = mkdtempSync(join(cache, "project-"));
154
244
 
155
245
  for (const [name, content] of Object.entries(files)) {
246
+ mkdirSync(dirname(join(directory, name)), { recursive: true });
156
247
  writeFileSync(join(directory, name), content);
157
248
  }
158
249
 
@@ -265,12 +356,21 @@ describe("babel.parallel", () => {
265
356
  `});`,
266
357
  ].join("\n");
267
358
 
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
- });
359
+ async function compileWithConfigFile(
360
+ options: Parameters<typeof ember>[0] = {},
361
+ config = `export default { plugins: ["./thread-plugin.js"] };`,
362
+ files: Record<string, string> = {},
363
+ ) {
364
+ const directory = inProject(
365
+ Object.assign(
366
+ {
367
+ "package.json": `{ "type": "module" }`,
368
+ "thread-plugin.js": THREAD_PLUGIN,
369
+ "babel.config.js": config,
370
+ },
371
+ files,
372
+ ),
373
+ );
274
374
 
275
375
  const plugins = ember(options) as Plugin[];
276
376
  const babel = await startedBabel(plugins);
@@ -294,6 +394,43 @@ describe("babel.parallel", () => {
294
394
  expect(await compileWithConfigFile()).toContain("worker thread");
295
395
  });
296
396
 
397
+ /**
398
+ * `@warp-drive/build-config` before 5.9 finds its own folder with `typeof __dirname`,
399
+ * which Node sets to "." in an eval worker thread.
400
+ * See https://github.com/warp-drive-data/warp-drive/pull/10926.
401
+ */
402
+ function buildConfig(version: string) {
403
+ return {
404
+ "node_modules/@warp-drive/build-config/package.json": JSON.stringify({
405
+ name: "@warp-drive/build-config",
406
+ version,
407
+ type: "module",
408
+ exports: { ".": "./index.js" },
409
+ }),
410
+ "node_modules/@warp-drive/build-config/index.js": [
411
+ `if (typeof __dirname !== "undefined") throw new Error("__dirname is " + __dirname);`,
412
+ `export const setConfig = () => {};`,
413
+ ].join("\n"),
414
+ };
415
+ }
416
+
417
+ const CONFIG_WITH_WARP_DRIVE = [
418
+ `import "@warp-drive/build-config";`,
419
+ `export default { plugins: ["./thread-plugin.js"] };`,
420
+ ].join("\n");
421
+
422
+ it("gives @warp-drive/build-config before 5.9 no __dirname in a worker thread", async () => {
423
+ expect(await compileWithConfigFile({}, CONFIG_WITH_WARP_DRIVE, buildConfig("5.8.2"))).toContain(
424
+ "worker thread",
425
+ );
426
+ expect(existsSync("node_modules/.cache/nullvoxpopuli-babel")).toBe(true);
427
+ });
428
+
429
+ it("loads the config file directly without @warp-drive/build-config before 5.9", async () => {
430
+ expect(await compileWithConfigFile()).toContain("worker thread");
431
+ expect(existsSync("node_modules/.cache/nullvoxpopuli-babel")).toBe(false);
432
+ });
433
+
297
434
  it("runs babel in the main thread when parallel is false", async () => {
298
435
  expect(await compileWithConfigFile({ babel: { parallel: false } })).toContain("main thread");
299
436
  });
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)) {
@@ -270,6 +271,12 @@ export function ember(nvpConfig: Config = {}) {
270
271
  // no compat
271
272
  name: "ember:nullvoxpopuli",
272
273
  async config(this: ConfigPluginContext, viteConfig: any, env: ConfigEnv) {
274
+ /**
275
+ * Read before the config hooks below write their defaults into `viteConfig`,
276
+ * and put back over them after `applyConfig`.
277
+ */
278
+ const project = projectConfig(viteConfig);
279
+
273
280
  /**
274
281
  * The hook reads the rolldown version from the plugin context,
275
282
  * and takes its esbuild path without it.
@@ -308,8 +315,10 @@ export function ember(nvpConfig: Config = {}) {
308
315
  viteConfig.optimizeDeps.rolldownOptions.tsconfig = true;
309
316
 
310
317
  applyConfig(viteConfig, env, nvpConfig);
318
+ keepProjectConfig(viteConfig, project);
311
319
 
312
320
  viteConfig.optimizeDeps.rolldownOptions.plugins = [
321
+ ...project.optimizeDepsPlugins,
313
322
  resolver({ rolldown: true }),
314
323
  templateTag(),
315
324
 
@@ -334,6 +343,53 @@ export function ember(nvpConfig: Config = {}) {
334
343
  ];
335
344
  }
336
345
 
346
+ /**
347
+ * What the project's own vite config sets, before any config hook changes it.
348
+ *
349
+ * What the config hooks here and in `@embroider/vite` set in `build`,
350
+ * `build.rolldownOptions.output` and `build.rolldownOptions.optimization` are defaults:
351
+ * a project that sets one, such as `build.sourcemap: "hidden"`
352
+ * or `build.rolldownOptions.output.strictExecutionOrder: false`, keeps its value.
353
+ * The project's own optimizeDeps plugins run before the ones this plugin adds.
354
+ */
355
+ function projectConfig(viteConfig: UserConfig) {
356
+ const build = viteConfig.build ?? {};
357
+ const output = build.rolldownOptions?.output;
358
+ const optimizeDepsPlugins = viteConfig.optimizeDeps?.rolldownOptions?.plugins;
359
+
360
+ return {
361
+ /**
362
+ * Vite 8 makes `build.rollupOptions` an accessor for `build.rolldownOptions`.
363
+ * Neither is put back: that would replace the object the config hooks fill in.
364
+ */
365
+ build: setValues(build, ["rolldownOptions", "rollupOptions"]),
366
+ output: Array.isArray(output) ? {} : setValues(output),
367
+ optimization: setValues(build.rolldownOptions?.optimization),
368
+ optimizeDepsPlugins: Array.isArray(optimizeDepsPlugins) ? [...optimizeDepsPlugins] : [],
369
+ };
370
+ }
371
+
372
+ /**
373
+ * The entries that have a value.
374
+ * A project that sets `sourcemap: undefined` gets the default, as if it had not set it.
375
+ */
376
+ function setValues(object: object | undefined, skip: string[] = []) {
377
+ return Object.fromEntries(
378
+ Object.entries(object ?? {}).filter(
379
+ ([key, value]) => value !== undefined && !skip.includes(key),
380
+ ),
381
+ );
382
+ }
383
+
384
+ function keepProjectConfig(viteConfig: UserConfig, project: ReturnType<typeof projectConfig>) {
385
+ Object.assign(viteConfig.build!, project.build);
386
+
387
+ const { output, optimization } = viteConfig.build!.rolldownOptions ?? {};
388
+
389
+ if (output && !Array.isArray(output)) Object.assign(output, project.output);
390
+ if (optimization) Object.assign(optimization, project.optimization);
391
+ }
392
+
337
393
  /**
338
394
  * `vite preview` serves the build on `$PORT`, the port that hosts and CI give,
339
395
  * or on 4200, the port of the dev server.