@nullvoxpopuli/ember-vite 1.3.0 → 1.3.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/dist/index.js CHANGED
@@ -110,9 +110,18 @@ function absolutePath(relativePath) {
110
110
  *
111
111
  * The plugins are named, not imported.
112
112
  * babel resolves them from the project, which carries them as its own dependencies.
113
+ *
114
+ * The macros take development mode from the vite mode.
115
+ * `buildMacros` reads NODE_ENV, but vite sets NODE_ENV
116
+ * only after the config hooks run.
113
117
  */
114
- function defaultBabelPlugins() {
115
- const macros = buildMacros();
118
+ function defaultBabelPlugins(mode) {
119
+ const macros = buildMacros({ configure(config) {
120
+ if (isDev(mode)) {
121
+ config.enablePackageDevelopment(process.cwd());
122
+ config.enableRuntimeMode();
123
+ }
124
+ } });
116
125
  return [
117
126
  ["@babel/plugin-transform-typescript", {
118
127
  allExtensions: true,
@@ -137,11 +146,15 @@ function defaultBabelPlugins() {
137
146
  function babelOptions(nvpConfig) {
138
147
  const configFile = resolve(join(process.cwd(), nvpConfig.babel?.configFile ?? "./babel.config.js"));
139
148
  if (existsSync(configFile)) return { configFile };
149
+ /**
150
+ * The config hook fills this list, once it knows the vite mode.
151
+ * The babel plugins keep a reference to it, so they see the filled list.
152
+ */
140
153
  return {
141
154
  configFile: false,
142
155
  babelrc: false,
143
156
  babelHelpers: "inline",
144
- plugins: defaultBabelPlugins()
157
+ plugins: []
145
158
  };
146
159
  }
147
160
  /**
@@ -177,6 +190,10 @@ function ember(nvpConfig = {}) {
177
190
  * and takes its esbuild path without it.
178
191
  */
179
192
  await emberConfig.call(this, viteConfig, env);
193
+ if ("plugins" in babel) {
194
+ babel.plugins.length = 0;
195
+ for (const plugin of defaultBabelPlugins(env.mode)) babel.plugins.push(plugin);
196
+ }
180
197
  viteConfig.experimental ||= {};
181
198
  viteConfig.build ||= {};
182
199
  viteConfig.optimizeDeps ||= {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nullvoxpopuli/ember-vite",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Meta plugin for ember projects focusing on speed.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -27,6 +27,9 @@
27
27
  "node": ">= 24"
28
28
  },
29
29
  "devDependencies": {
30
+ "@babel/plugin-transform-typescript": "^7.28.10 || ^8.0.0",
31
+ "babel-plugin-ember-template-compilation": "^4.0.1",
32
+ "decorator-transforms": "^2.4.0",
30
33
  "rolldown": "^1.1.2",
31
34
  "typescript": "^6.0.3",
32
35
  "vite": "^8.0.14"
package/src/ember.test.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { extensions } from "@embroider/vite";
2
- import type { ConfigEnv, ConfigPluginContext } from "vite";
3
- import { describe, expect, it } from "vitest";
2
+ import { fileURLToPath } from "node:url";
3
+ import type { ConfigEnv, ConfigPluginContext, Plugin } from "vite";
4
+ import { afterEach, describe, expect, it } from "vitest";
4
5
 
5
6
  import { ember } from "./ember.ts";
6
7
 
@@ -36,3 +37,74 @@ describe("ember", () => {
36
37
  expect(config.optimizeDeps.rolldownOptions.resolve.extensions).toEqual(extensions);
37
38
  });
38
39
  });
40
+
41
+ /**
42
+ * This package has no babel config,
43
+ * so `ember()` uses its default babel plugins,
44
+ * and babel resolves them from here.
45
+ */
46
+ const PACKAGE_ROOT = fileURLToPath(new URL("..", import.meta.url));
47
+
48
+ const DEV_CHECK = [
49
+ `import { isDevelopingApp, macroCondition } from "@embroider/macros";`,
50
+ `export const build = macroCondition(isDevelopingApp()) ? "development build" : "other build";`,
51
+ ].join("\n");
52
+
53
+ const CONTEXT = {
54
+ error(error: unknown) {
55
+ throw error;
56
+ },
57
+ };
58
+
59
+ /**
60
+ * Runs the config hook, then the babel plugin, the way vite does.
61
+ */
62
+ async function compile(mode: string) {
63
+ const plugins = ember() as Plugin[];
64
+ const config: any = {};
65
+
66
+ // @ts-expect-error the first entry is the config plugin
67
+ await plugins[0].config.call(ROLLDOWN, config, { command: "build", mode });
68
+
69
+ const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel");
70
+ // @ts-expect-error the babel plugin has an object transform hook
71
+ const result = await babel.transform.handler.call(CONTEXT, DEV_CHECK, `${PACKAGE_ROOT}app.js`);
72
+
73
+ return result.code as string;
74
+ }
75
+
76
+ describe("default babel plugins", () => {
77
+ let restoreCwd: (() => void) | undefined;
78
+
79
+ afterEach(() => {
80
+ restoreCwd?.();
81
+ restoreCwd = undefined;
82
+ });
83
+
84
+ function inPackageRoot() {
85
+ const previous = process.cwd();
86
+ process.chdir(PACKAGE_ROOT);
87
+ restoreCwd = () => process.chdir(previous);
88
+ }
89
+
90
+ /**
91
+ * vite sets NODE_ENV from the mode only after the config hooks run.
92
+ * Under vitest, NODE_ENV is "test".
93
+ */
94
+ it("keeps development-only code in development mode", async () => {
95
+ inPackageRoot();
96
+
97
+ const code = await compile("development");
98
+
99
+ expect(code).toContain("development build");
100
+ });
101
+
102
+ it("removes development-only code in production mode", async () => {
103
+ inPackageRoot();
104
+
105
+ const code = await compile("production");
106
+
107
+ expect(code).not.toContain("development build");
108
+ expect(code).toContain("other build");
109
+ });
110
+ });
package/src/ember.ts CHANGED
@@ -59,9 +59,20 @@ function absolutePath(relativePath: string) {
59
59
  *
60
60
  * The plugins are named, not imported.
61
61
  * babel resolves them from the project, which carries them as its own dependencies.
62
+ *
63
+ * The macros take development mode from the vite mode.
64
+ * `buildMacros` reads NODE_ENV, but vite sets NODE_ENV
65
+ * only after the config hooks run.
62
66
  */
63
- function defaultBabelPlugins() {
64
- const macros = buildMacros();
67
+ function defaultBabelPlugins(mode: string) {
68
+ const macros = buildMacros({
69
+ configure(config) {
70
+ if (isDev(mode)) {
71
+ config.enablePackageDevelopment(process.cwd());
72
+ config.enableRuntimeMode();
73
+ }
74
+ },
75
+ });
65
76
 
66
77
  return [
67
78
  [
@@ -106,11 +117,15 @@ function babelOptions(nvpConfig: Config): BabelOptions {
106
117
  return { configFile };
107
118
  }
108
119
 
120
+ /**
121
+ * The config hook fills this list, once it knows the vite mode.
122
+ * The babel plugins keep a reference to it, so they see the filled list.
123
+ */
109
124
  return {
110
125
  configFile: false,
111
126
  babelrc: false,
112
127
  babelHelpers: "inline",
113
- plugins: defaultBabelPlugins(),
128
+ plugins: [],
114
129
  };
115
130
  }
116
131
 
@@ -210,6 +225,14 @@ export function ember(nvpConfig: Config = {}) {
210
225
  */
211
226
  await emberConfig.call(this, viteConfig, env);
212
227
 
228
+ if ("plugins" in babel) {
229
+ babel.plugins.length = 0;
230
+
231
+ for (const plugin of defaultBabelPlugins(env.mode)) {
232
+ babel.plugins.push(plugin);
233
+ }
234
+ }
235
+
213
236
  viteConfig.experimental ||= {};
214
237
  viteConfig.build ||= {};
215
238
  viteConfig.optimizeDeps ||= {};