@nullvoxpopuli/ember-vite 1.2.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 +42 -5
- package/package.json +7 -4
- package/src/ember.test.ts +110 -0
- package/src/ember.ts +64 -8
package/dist/index.js
CHANGED
|
@@ -75,7 +75,23 @@ function testHtml() {
|
|
|
75
75
|
}
|
|
76
76
|
//#endregion
|
|
77
77
|
//#region src/ember.ts
|
|
78
|
-
const
|
|
78
|
+
const EMBER_CONFIG = "vite-plugin-ember-config";
|
|
79
|
+
/**
|
|
80
|
+
* `@embroider/vite` does not export its config hook on its own.
|
|
81
|
+
*
|
|
82
|
+
* The position of the plugin in the list changes between releases,
|
|
83
|
+
* so the name is the only stable way to find it.
|
|
84
|
+
*/
|
|
85
|
+
function findEmberConfig() {
|
|
86
|
+
for (const plugin of ember$1()) {
|
|
87
|
+
if (plugin.name !== EMBER_CONFIG) continue;
|
|
88
|
+
const hook = plugin.config;
|
|
89
|
+
const handler = typeof hook === "function" ? hook : hook?.handler;
|
|
90
|
+
if (handler) return handler;
|
|
91
|
+
}
|
|
92
|
+
throw new Error(`@embroider/vite has no "${EMBER_CONFIG}" plugin with a config hook. This version of @embroider/vite is not supported by @nullvoxpopuli/ember-vite.`);
|
|
93
|
+
}
|
|
94
|
+
const emberConfig = findEmberConfig();
|
|
79
95
|
const cwd = process.cwd();
|
|
80
96
|
function absolutePath(relativePath) {
|
|
81
97
|
return join(cwd, relativePath);
|
|
@@ -94,9 +110,18 @@ function absolutePath(relativePath) {
|
|
|
94
110
|
*
|
|
95
111
|
* The plugins are named, not imported.
|
|
96
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.
|
|
97
117
|
*/
|
|
98
|
-
function defaultBabelPlugins() {
|
|
99
|
-
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
|
+
} });
|
|
100
125
|
return [
|
|
101
126
|
["@babel/plugin-transform-typescript", {
|
|
102
127
|
allExtensions: true,
|
|
@@ -121,11 +146,15 @@ function defaultBabelPlugins() {
|
|
|
121
146
|
function babelOptions(nvpConfig) {
|
|
122
147
|
const configFile = resolve(join(process.cwd(), nvpConfig.babel?.configFile ?? "./babel.config.js"));
|
|
123
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
|
+
*/
|
|
124
153
|
return {
|
|
125
154
|
configFile: false,
|
|
126
155
|
babelrc: false,
|
|
127
156
|
babelHelpers: "inline",
|
|
128
|
-
plugins:
|
|
157
|
+
plugins: []
|
|
129
158
|
};
|
|
130
159
|
}
|
|
131
160
|
/**
|
|
@@ -156,7 +185,15 @@ function ember(nvpConfig = {}) {
|
|
|
156
185
|
{
|
|
157
186
|
name: "ember:nullvoxpopuli",
|
|
158
187
|
async config(viteConfig, env) {
|
|
159
|
-
|
|
188
|
+
/**
|
|
189
|
+
* The hook reads the rolldown version from the plugin context,
|
|
190
|
+
* and takes its esbuild path without it.
|
|
191
|
+
*/
|
|
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
|
+
}
|
|
160
197
|
viteConfig.experimental ||= {};
|
|
161
198
|
viteConfig.build ||= {};
|
|
162
199
|
viteConfig.optimizeDeps ||= {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nullvoxpopuli/ember-vite",
|
|
3
|
-
"version": "1.
|
|
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",
|
|
@@ -18,15 +18,18 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@babel/core": "^7.28.10 || ^8.0.0",
|
|
21
|
-
"@embroider/core": "^4.
|
|
22
|
-
"@embroider/macros": "^1.
|
|
23
|
-
"@embroider/vite": "^1.
|
|
21
|
+
"@embroider/core": "^4.6.9",
|
|
22
|
+
"@embroider/macros": "^1.21.3",
|
|
23
|
+
"@embroider/vite": "^1.7.15",
|
|
24
24
|
"@nullvoxpopuli/ember-build-tooling-utils": "1.2.0"
|
|
25
25
|
},
|
|
26
26
|
"engines": {
|
|
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"
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { extensions } from "@embroider/vite";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import type { ConfigEnv, ConfigPluginContext, Plugin } from "vite";
|
|
4
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
+
|
|
6
|
+
import { ember } from "./ember.ts";
|
|
7
|
+
|
|
8
|
+
const ROLLDOWN = { meta: { rolldownVersion: "1.0.0" } } as ConfigPluginContext;
|
|
9
|
+
|
|
10
|
+
async function configure(env: ConfigEnv) {
|
|
11
|
+
const config: any = {};
|
|
12
|
+
const [plugin] = ember();
|
|
13
|
+
|
|
14
|
+
// @ts-expect-error the first entry is the config plugin
|
|
15
|
+
await plugin.config.call(ROLLDOWN, config, env);
|
|
16
|
+
|
|
17
|
+
return config;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("ember", () => {
|
|
21
|
+
/**
|
|
22
|
+
* These values come from the config hook of `@embroider/vite`.
|
|
23
|
+
* They are missing when that hook does not run.
|
|
24
|
+
*/
|
|
25
|
+
it("applies the config from @embroider/vite", async () => {
|
|
26
|
+
const config = await configure({ command: "build", mode: "production" });
|
|
27
|
+
|
|
28
|
+
expect(config.resolve.extensions).toEqual(extensions);
|
|
29
|
+
expect(config.server.port).toBe(4200);
|
|
30
|
+
expect(config.optimizeDeps.exclude).toContain("@embroider/macros");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("gives @embroider/vite the plugin context, so that it configures rolldown", async () => {
|
|
34
|
+
const config = await configure({ command: "build", mode: "production" });
|
|
35
|
+
|
|
36
|
+
expect(config.build.rolldownOptions.moduleTypes).toEqual({ ".ts": "js" });
|
|
37
|
+
expect(config.optimizeDeps.rolldownOptions.resolve.extensions).toEqual(extensions);
|
|
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
|
@@ -2,13 +2,43 @@ import { buildMacros } from "@embroider/macros/babel";
|
|
|
2
2
|
import { ember as embroiderEmber, extensions, resolver, templateTag } from "@embroider/vite";
|
|
3
3
|
import { existsSync } from "node:fs";
|
|
4
4
|
import { join, resolve } from "node:path";
|
|
5
|
-
import type { ConfigEnv, ResolvedConfig } from "vite";
|
|
5
|
+
import type { ConfigEnv, ConfigPluginContext, ResolvedConfig, UserConfig } from "vite";
|
|
6
6
|
|
|
7
7
|
import { maybeBabel } from "@nullvoxpopuli/ember-build-tooling-utils";
|
|
8
8
|
|
|
9
9
|
import { testHtml } from "./test-html.ts";
|
|
10
10
|
|
|
11
|
-
const
|
|
11
|
+
const EMBER_CONFIG = "vite-plugin-ember-config";
|
|
12
|
+
|
|
13
|
+
type ConfigHook = (
|
|
14
|
+
this: ConfigPluginContext,
|
|
15
|
+
config: UserConfig,
|
|
16
|
+
env: ConfigEnv,
|
|
17
|
+
) => Promise<unknown> | unknown;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `@embroider/vite` does not export its config hook on its own.
|
|
21
|
+
*
|
|
22
|
+
* The position of the plugin in the list changes between releases,
|
|
23
|
+
* so the name is the only stable way to find it.
|
|
24
|
+
*/
|
|
25
|
+
function findEmberConfig(): ConfigHook {
|
|
26
|
+
for (const plugin of embroiderEmber()) {
|
|
27
|
+
if (plugin.name !== EMBER_CONFIG) continue;
|
|
28
|
+
|
|
29
|
+
const hook = plugin.config;
|
|
30
|
+
const handler = typeof hook === "function" ? hook : hook?.handler;
|
|
31
|
+
|
|
32
|
+
if (handler) return handler as ConfigHook;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
throw new Error(
|
|
36
|
+
`@embroider/vite has no "${EMBER_CONFIG}" plugin with a config hook. ` +
|
|
37
|
+
`This version of @embroider/vite is not supported by @nullvoxpopuli/ember-vite.`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const emberConfig = findEmberConfig();
|
|
12
42
|
|
|
13
43
|
const cwd = process.cwd();
|
|
14
44
|
function absolutePath(relativePath: string) {
|
|
@@ -29,9 +59,20 @@ function absolutePath(relativePath: string) {
|
|
|
29
59
|
*
|
|
30
60
|
* The plugins are named, not imported.
|
|
31
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.
|
|
32
66
|
*/
|
|
33
|
-
function defaultBabelPlugins() {
|
|
34
|
-
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
|
+
});
|
|
35
76
|
|
|
36
77
|
return [
|
|
37
78
|
[
|
|
@@ -76,11 +117,15 @@ function babelOptions(nvpConfig: Config): BabelOptions {
|
|
|
76
117
|
return { configFile };
|
|
77
118
|
}
|
|
78
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
|
+
*/
|
|
79
124
|
return {
|
|
80
125
|
configFile: false,
|
|
81
126
|
babelrc: false,
|
|
82
127
|
babelHelpers: "inline",
|
|
83
|
-
plugins:
|
|
128
|
+
plugins: [],
|
|
84
129
|
};
|
|
85
130
|
}
|
|
86
131
|
|
|
@@ -173,9 +218,20 @@ export function ember(nvpConfig: Config = {}) {
|
|
|
173
218
|
{
|
|
174
219
|
// no compat
|
|
175
220
|
name: "ember:nullvoxpopuli",
|
|
176
|
-
async config(viteConfig: any, env: ConfigEnv) {
|
|
177
|
-
|
|
178
|
-
|
|
221
|
+
async config(this: ConfigPluginContext, viteConfig: any, env: ConfigEnv) {
|
|
222
|
+
/**
|
|
223
|
+
* The hook reads the rolldown version from the plugin context,
|
|
224
|
+
* and takes its esbuild path without it.
|
|
225
|
+
*/
|
|
226
|
+
await emberConfig.call(this, viteConfig, env);
|
|
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
|
+
}
|
|
179
235
|
|
|
180
236
|
viteConfig.experimental ||= {};
|
|
181
237
|
viteConfig.build ||= {};
|