@nullvoxpopuli/ember-vite 1.3.1 → 1.5.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 CHANGED
@@ -53,6 +53,7 @@ ember({
53
53
  parallel: false,
54
54
  include: {
55
55
  whenImporting: ["ember-concurrency", "ember-intl/format-message"],
56
+ whenMatching: [/\bmyMacro\(/],
56
57
  },
57
58
  },
58
59
  });
package/dist/index.js CHANGED
@@ -175,7 +175,11 @@ function babelFilter(nvpConfig) {
175
175
  "ember-intl/helpers/format-message",
176
176
  ...nvpConfig.babel?.include?.whenImporting ?? []
177
177
  ],
178
- code: [/\bintl\.formatMessage\b/, "initializeRuntimeMacrosConfig"]
178
+ code: [
179
+ /\bintl\.formatMessage\b/,
180
+ "initializeRuntimeMacrosConfig",
181
+ ...nvpConfig.babel?.include?.whenMatching ?? []
182
+ ]
179
183
  } };
180
184
  }
181
185
  function ember(nvpConfig = {}) {
@@ -190,6 +194,7 @@ function ember(nvpConfig = {}) {
190
194
  * and takes its esbuild path without it.
191
195
  */
192
196
  await emberConfig.call(this, viteConfig, env);
197
+ applyPreviewPort(viteConfig);
193
198
  if ("plugins" in babel) {
194
199
  babel.plugins.length = 0;
195
200
  for (const plugin of defaultBabelPlugins(env.mode)) babel.plugins.push(plugin);
@@ -228,6 +233,16 @@ function ember(nvpConfig = {}) {
228
233
  })
229
234
  ];
230
235
  }
236
+ /**
237
+ * `vite preview` serves the build on `$PORT`, the port that hosts and CI give,
238
+ * or on 4200, the port of the dev server.
239
+ * Vite itself reads no `$PORT`, and `@embroider/vite` only sets the dev server's port.
240
+ * A port in the project's own vite config wins.
241
+ */
242
+ function applyPreviewPort(viteConfig) {
243
+ viteConfig.preview ||= {};
244
+ viteConfig.preview.port ??= Number(process.env.PORT) || 4200;
245
+ }
231
246
  const MODE_DEV = "development";
232
247
  function isDev(mode) {
233
248
  return mode === MODE_DEV;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nullvoxpopuli/ember-vite",
3
- "version": "1.3.1",
3
+ "version": "1.5.0",
4
4
  "description": "Meta plugin for ember projects focusing on speed.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -28,6 +28,7 @@
28
28
  },
29
29
  "devDependencies": {
30
30
  "@babel/plugin-transform-typescript": "^7.28.10 || ^8.0.0",
31
+ "@rolldown/pluginutils": "^1.0.1",
31
32
  "babel-plugin-ember-template-compilation": "^4.0.1",
32
33
  "decorator-transforms": "^2.4.0",
33
34
  "rolldown": "^1.1.2",
package/src/ember.test.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { extensions } from "@embroider/vite";
2
+ import { interpreter, type TopLevelFilterExpression } from "@rolldown/pluginutils";
2
3
  import { fileURLToPath } from "node:url";
3
4
  import type { ConfigEnv, ConfigPluginContext, Plugin } from "vite";
4
5
  import { afterEach, describe, expect, it } from "vitest";
@@ -7,8 +8,7 @@ import { ember } from "./ember.ts";
7
8
 
8
9
  const ROLLDOWN = { meta: { rolldownVersion: "1.0.0" } } as ConfigPluginContext;
9
10
 
10
- async function configure(env: ConfigEnv) {
11
- const config: any = {};
11
+ async function configure(env: ConfigEnv, config: any = {}) {
12
12
  const [plugin] = ember();
13
13
 
14
14
  // @ts-expect-error the first entry is the config plugin
@@ -30,6 +30,42 @@ describe("ember", () => {
30
30
  expect(config.optimizeDeps.exclude).toContain("@embroider/macros");
31
31
  });
32
32
 
33
+ describe("preview port", () => {
34
+ const original = process.env.PORT;
35
+
36
+ afterEach(() => {
37
+ if (original === undefined) delete process.env.PORT;
38
+ else process.env.PORT = original;
39
+ });
40
+
41
+ it("is 4200 without $PORT, like the dev server", async () => {
42
+ delete process.env.PORT;
43
+
44
+ const config = await configure({ command: "serve", mode: "production" });
45
+
46
+ expect(config.preview.port).toBe(4200);
47
+ });
48
+
49
+ it("comes from $PORT", async () => {
50
+ process.env.PORT = "3123";
51
+
52
+ const config = await configure({ command: "serve", mode: "production" });
53
+
54
+ expect(config.preview.port).toBe(3123);
55
+ });
56
+
57
+ it("keeps the port in the project's vite config", async () => {
58
+ process.env.PORT = "3123";
59
+
60
+ const config = await configure(
61
+ { command: "serve", mode: "production" },
62
+ { preview: { port: 8080 } },
63
+ );
64
+
65
+ expect(config.preview.port).toBe(8080);
66
+ });
67
+ });
68
+
33
69
  it("gives @embroider/vite the plugin context, so that it configures rolldown", async () => {
34
70
  const config = await configure({ command: "build", mode: "production" });
35
71
 
@@ -108,3 +144,39 @@ describe("default babel plugins", () => {
108
144
  expect(code).toContain("other build");
109
145
  });
110
146
  });
147
+
148
+ describe("babel.include", () => {
149
+ function usesBabel(options: Parameters<typeof ember>[0], source: string) {
150
+ const plugins = ember(options) as Plugin[];
151
+ const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel");
152
+ // @ts-expect-error maybeBabel sets the filter on the transform hook
153
+ const filter = babel.transform.filter as TopLevelFilterExpression[];
154
+
155
+ return interpreter(filter, source, "/app/services/a.ts");
156
+ }
157
+
158
+ const source = `owner.register(Key, Impl);`;
159
+
160
+ it("leaves files without a babel-only pattern to oxc", () => {
161
+ expect(usesBabel({}, source)).toBe(false);
162
+ });
163
+
164
+ it("sends files that contain a whenMatching string to babel", () => {
165
+ expect(usesBabel({ babel: { include: { whenMatching: [".register("] } } }, source)).toBe(true);
166
+ });
167
+
168
+ it("sends files that match a whenMatching regular expression to babel", () => {
169
+ expect(usesBabel({ babel: { include: { whenMatching: [/\.register\(/] } } }, source)).toBe(
170
+ true,
171
+ );
172
+ });
173
+
174
+ it("sends files that import a whenImporting module to babel", () => {
175
+ expect(
176
+ usesBabel(
177
+ { babel: { include: { whenImporting: ["my-lib"] } } },
178
+ `import { a } from "my-lib";`,
179
+ ),
180
+ ).toBe(true);
181
+ });
182
+ });
package/src/ember.ts CHANGED
@@ -148,7 +148,11 @@ function babelFilter(nvpConfig: Config) {
148
148
  "ember-intl/helpers/format-message",
149
149
  ...(nvpConfig.babel?.include?.whenImporting ?? []),
150
150
  ],
151
- code: [/\bintl\.formatMessage\b/, "initializeRuntimeMacrosConfig"] as (string | RegExp)[],
151
+ code: [
152
+ /\bintl\.formatMessage\b/,
153
+ "initializeRuntimeMacrosConfig",
154
+ ...(nvpConfig.babel?.include?.whenMatching ?? []),
155
+ ] as (string | RegExp)[],
152
156
  },
153
157
  };
154
158
  }
@@ -185,6 +189,18 @@ interface Config {
185
189
  * For example: ['ember-concurrency'],
186
190
  */
187
191
  whenImporting?: (string | "ember-concurrency" | "ember-scoped-css")[];
192
+
193
+ /**
194
+ * if a file's source contains any of these strings,
195
+ * or matches any of these regular expressions,
196
+ * use babel instead of oxc for that file.
197
+ *
198
+ * Use this for babel plugins that look for a code pattern rather than
199
+ * an import.
200
+ *
201
+ * For example: [/\.register\(/],
202
+ */
203
+ whenMatching?: (string | RegExp)[];
188
204
  };
189
205
  };
190
206
  /**
@@ -225,6 +241,8 @@ export function ember(nvpConfig: Config = {}) {
225
241
  */
226
242
  await emberConfig.call(this, viteConfig, env);
227
243
 
244
+ applyPreviewPort(viteConfig);
245
+
228
246
  if ("plugins" in babel) {
229
247
  babel.plugins.length = 0;
230
248
 
@@ -281,6 +299,17 @@ export function ember(nvpConfig: Config = {}) {
281
299
  ];
282
300
  }
283
301
 
302
+ /**
303
+ * `vite preview` serves the build on `$PORT`, the port that hosts and CI give,
304
+ * or on 4200, the port of the dev server.
305
+ * Vite itself reads no `$PORT`, and `@embroider/vite` only sets the dev server's port.
306
+ * A port in the project's own vite config wins.
307
+ */
308
+ function applyPreviewPort(viteConfig: UserConfig) {
309
+ viteConfig.preview ||= {};
310
+ viteConfig.preview.port ??= Number(process.env.PORT) || 4200;
311
+ }
312
+
284
313
  const MODE_DEV = "development";
285
314
 
286
315
  function isDev(mode: string) {