@nullvoxpopuli/ember-vite 1.3.0 → 1.4.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 +31 -3
- package/package.json +4 -1
- package/src/ember.test.ts +111 -4
- package/src/ember.ts +39 -3
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:
|
|
157
|
+
plugins: []
|
|
145
158
|
};
|
|
146
159
|
}
|
|
147
160
|
/**
|
|
@@ -177,6 +190,11 @@ function ember(nvpConfig = {}) {
|
|
|
177
190
|
* and takes its esbuild path without it.
|
|
178
191
|
*/
|
|
179
192
|
await emberConfig.call(this, viteConfig, env);
|
|
193
|
+
applyPreviewPort(viteConfig);
|
|
194
|
+
if ("plugins" in babel) {
|
|
195
|
+
babel.plugins.length = 0;
|
|
196
|
+
for (const plugin of defaultBabelPlugins(env.mode)) babel.plugins.push(plugin);
|
|
197
|
+
}
|
|
180
198
|
viteConfig.experimental ||= {};
|
|
181
199
|
viteConfig.build ||= {};
|
|
182
200
|
viteConfig.optimizeDeps ||= {};
|
|
@@ -211,6 +229,16 @@ function ember(nvpConfig = {}) {
|
|
|
211
229
|
})
|
|
212
230
|
];
|
|
213
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* `vite preview` serves the build on `$PORT`, the port that hosts and CI give,
|
|
234
|
+
* or on 4200, the port of the dev server.
|
|
235
|
+
* Vite itself reads no `$PORT`, and `@embroider/vite` only sets the dev server's port.
|
|
236
|
+
* A port in the project's own vite config wins.
|
|
237
|
+
*/
|
|
238
|
+
function applyPreviewPort(viteConfig) {
|
|
239
|
+
viteConfig.preview ||= {};
|
|
240
|
+
viteConfig.preview.port ??= Number(process.env.PORT) || 4200;
|
|
241
|
+
}
|
|
214
242
|
const MODE_DEV = "development";
|
|
215
243
|
function isDev(mode) {
|
|
216
244
|
return mode === MODE_DEV;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nullvoxpopuli/ember-vite",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
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,13 +1,13 @@
|
|
|
1
1
|
import { extensions } from "@embroider/vite";
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
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
|
|
|
7
8
|
const ROLLDOWN = { meta: { rolldownVersion: "1.0.0" } } as ConfigPluginContext;
|
|
8
9
|
|
|
9
|
-
async function configure(env: ConfigEnv) {
|
|
10
|
-
const config: any = {};
|
|
10
|
+
async function configure(env: ConfigEnv, config: any = {}) {
|
|
11
11
|
const [plugin] = ember();
|
|
12
12
|
|
|
13
13
|
// @ts-expect-error the first entry is the config plugin
|
|
@@ -29,6 +29,42 @@ describe("ember", () => {
|
|
|
29
29
|
expect(config.optimizeDeps.exclude).toContain("@embroider/macros");
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
+
describe("preview port", () => {
|
|
33
|
+
const original = process.env.PORT;
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
if (original === undefined) delete process.env.PORT;
|
|
37
|
+
else process.env.PORT = original;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("is 4200 without $PORT, like the dev server", async () => {
|
|
41
|
+
delete process.env.PORT;
|
|
42
|
+
|
|
43
|
+
const config = await configure({ command: "serve", mode: "production" });
|
|
44
|
+
|
|
45
|
+
expect(config.preview.port).toBe(4200);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("comes from $PORT", async () => {
|
|
49
|
+
process.env.PORT = "3123";
|
|
50
|
+
|
|
51
|
+
const config = await configure({ command: "serve", mode: "production" });
|
|
52
|
+
|
|
53
|
+
expect(config.preview.port).toBe(3123);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("keeps the port in the project's vite config", async () => {
|
|
57
|
+
process.env.PORT = "3123";
|
|
58
|
+
|
|
59
|
+
const config = await configure(
|
|
60
|
+
{ command: "serve", mode: "production" },
|
|
61
|
+
{ preview: { port: 8080 } },
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
expect(config.preview.port).toBe(8080);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
32
68
|
it("gives @embroider/vite the plugin context, so that it configures rolldown", async () => {
|
|
33
69
|
const config = await configure({ command: "build", mode: "production" });
|
|
34
70
|
|
|
@@ -36,3 +72,74 @@ describe("ember", () => {
|
|
|
36
72
|
expect(config.optimizeDeps.rolldownOptions.resolve.extensions).toEqual(extensions);
|
|
37
73
|
});
|
|
38
74
|
});
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* This package has no babel config,
|
|
78
|
+
* so `ember()` uses its default babel plugins,
|
|
79
|
+
* and babel resolves them from here.
|
|
80
|
+
*/
|
|
81
|
+
const PACKAGE_ROOT = fileURLToPath(new URL("..", import.meta.url));
|
|
82
|
+
|
|
83
|
+
const DEV_CHECK = [
|
|
84
|
+
`import { isDevelopingApp, macroCondition } from "@embroider/macros";`,
|
|
85
|
+
`export const build = macroCondition(isDevelopingApp()) ? "development build" : "other build";`,
|
|
86
|
+
].join("\n");
|
|
87
|
+
|
|
88
|
+
const CONTEXT = {
|
|
89
|
+
error(error: unknown) {
|
|
90
|
+
throw error;
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Runs the config hook, then the babel plugin, the way vite does.
|
|
96
|
+
*/
|
|
97
|
+
async function compile(mode: string) {
|
|
98
|
+
const plugins = ember() as Plugin[];
|
|
99
|
+
const config: any = {};
|
|
100
|
+
|
|
101
|
+
// @ts-expect-error the first entry is the config plugin
|
|
102
|
+
await plugins[0].config.call(ROLLDOWN, config, { command: "build", mode });
|
|
103
|
+
|
|
104
|
+
const babel = plugins.find((plugin) => plugin.name === "nullvoxpopuli:babel");
|
|
105
|
+
// @ts-expect-error the babel plugin has an object transform hook
|
|
106
|
+
const result = await babel.transform.handler.call(CONTEXT, DEV_CHECK, `${PACKAGE_ROOT}app.js`);
|
|
107
|
+
|
|
108
|
+
return result.code as string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
describe("default babel plugins", () => {
|
|
112
|
+
let restoreCwd: (() => void) | undefined;
|
|
113
|
+
|
|
114
|
+
afterEach(() => {
|
|
115
|
+
restoreCwd?.();
|
|
116
|
+
restoreCwd = undefined;
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
function inPackageRoot() {
|
|
120
|
+
const previous = process.cwd();
|
|
121
|
+
process.chdir(PACKAGE_ROOT);
|
|
122
|
+
restoreCwd = () => process.chdir(previous);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* vite sets NODE_ENV from the mode only after the config hooks run.
|
|
127
|
+
* Under vitest, NODE_ENV is "test".
|
|
128
|
+
*/
|
|
129
|
+
it("keeps development-only code in development mode", async () => {
|
|
130
|
+
inPackageRoot();
|
|
131
|
+
|
|
132
|
+
const code = await compile("development");
|
|
133
|
+
|
|
134
|
+
expect(code).toContain("development build");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("removes development-only code in production mode", async () => {
|
|
138
|
+
inPackageRoot();
|
|
139
|
+
|
|
140
|
+
const code = await compile("production");
|
|
141
|
+
|
|
142
|
+
expect(code).not.toContain("development build");
|
|
143
|
+
expect(code).toContain("other build");
|
|
144
|
+
});
|
|
145
|
+
});
|
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:
|
|
128
|
+
plugins: [],
|
|
114
129
|
};
|
|
115
130
|
}
|
|
116
131
|
|
|
@@ -210,6 +225,16 @@ export function ember(nvpConfig: Config = {}) {
|
|
|
210
225
|
*/
|
|
211
226
|
await emberConfig.call(this, viteConfig, env);
|
|
212
227
|
|
|
228
|
+
applyPreviewPort(viteConfig);
|
|
229
|
+
|
|
230
|
+
if ("plugins" in babel) {
|
|
231
|
+
babel.plugins.length = 0;
|
|
232
|
+
|
|
233
|
+
for (const plugin of defaultBabelPlugins(env.mode)) {
|
|
234
|
+
babel.plugins.push(plugin);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
213
238
|
viteConfig.experimental ||= {};
|
|
214
239
|
viteConfig.build ||= {};
|
|
215
240
|
viteConfig.optimizeDeps ||= {};
|
|
@@ -258,6 +283,17 @@ export function ember(nvpConfig: Config = {}) {
|
|
|
258
283
|
];
|
|
259
284
|
}
|
|
260
285
|
|
|
286
|
+
/**
|
|
287
|
+
* `vite preview` serves the build on `$PORT`, the port that hosts and CI give,
|
|
288
|
+
* or on 4200, the port of the dev server.
|
|
289
|
+
* Vite itself reads no `$PORT`, and `@embroider/vite` only sets the dev server's port.
|
|
290
|
+
* A port in the project's own vite config wins.
|
|
291
|
+
*/
|
|
292
|
+
function applyPreviewPort(viteConfig: UserConfig) {
|
|
293
|
+
viteConfig.preview ||= {};
|
|
294
|
+
viteConfig.preview.port ??= Number(process.env.PORT) || 4200;
|
|
295
|
+
}
|
|
296
|
+
|
|
261
297
|
const MODE_DEV = "development";
|
|
262
298
|
|
|
263
299
|
function isDev(mode: string) {
|