@nullvoxpopuli/ember-vite 2.1.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.1.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",
package/src/ember.test.ts CHANGED
@@ -3,7 +3,7 @@ import { interpreter, type TopLevelFilterExpression } from "@rolldown/pluginutil
3
3
  import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
4
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,
package/src/ember.ts CHANGED
@@ -271,6 +271,12 @@ export function ember(nvpConfig: Config = {}) {
271
271
  // no compat
272
272
  name: "ember:nullvoxpopuli",
273
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
+
274
280
  /**
275
281
  * The hook reads the rolldown version from the plugin context,
276
282
  * and takes its esbuild path without it.
@@ -309,8 +315,10 @@ export function ember(nvpConfig: Config = {}) {
309
315
  viteConfig.optimizeDeps.rolldownOptions.tsconfig = true;
310
316
 
311
317
  applyConfig(viteConfig, env, nvpConfig);
318
+ keepProjectConfig(viteConfig, project);
312
319
 
313
320
  viteConfig.optimizeDeps.rolldownOptions.plugins = [
321
+ ...project.optimizeDepsPlugins,
314
322
  resolver({ rolldown: true }),
315
323
  templateTag(),
316
324
 
@@ -335,6 +343,53 @@ export function ember(nvpConfig: Config = {}) {
335
343
  ];
336
344
  }
337
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
+
338
393
  /**
339
394
  * `vite preview` serves the build on `$PORT`, the port that hosts and CI give,
340
395
  * or on 4200, the port of the dev server.