@nullvoxpopuli/ember-vite 1.0.2 → 1.0.3
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 +310 -0
- package/package.json +6 -2
- package/rolldown.config.js +18 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { ember as ember$1, extensions, resolver, templateTag } from "@embroider/vite";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { babel } from "@rollup/plugin-babel";
|
|
5
|
+
import { parse } from "oxc-parser";
|
|
6
|
+
import { transform } from "oxc-transform";
|
|
7
|
+
import { walk } from "zimmerframe";
|
|
8
|
+
//#region src/maybe-babel.ts
|
|
9
|
+
/**
|
|
10
|
+
* Reasons we have to have babel
|
|
11
|
+
* - decorators
|
|
12
|
+
* - ember-concurrency
|
|
13
|
+
* - scoped-css
|
|
14
|
+
* - template compilation optimization
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* if a file imports any of these, it must be optimize
|
|
18
|
+
*/
|
|
19
|
+
const babelTemplateImports = /* @__PURE__ */ new Set([
|
|
20
|
+
"@ember/template-compiler",
|
|
21
|
+
"@ember/template-compilation",
|
|
22
|
+
"ember-cli-htmlbars",
|
|
23
|
+
"ember-cli-htmlbars-inline-precompile",
|
|
24
|
+
"htmlbars-inline-precompile"
|
|
25
|
+
]);
|
|
26
|
+
const babelMacroImports = /* @__PURE__ */ new Set([
|
|
27
|
+
"@embroider/macros",
|
|
28
|
+
"@glimmer/env",
|
|
29
|
+
"@ember/application/deprecations",
|
|
30
|
+
"@ember/debug"
|
|
31
|
+
]);
|
|
32
|
+
const babelOtherImports = /* @__PURE__ */ new Set([
|
|
33
|
+
"ember-concurrency",
|
|
34
|
+
"ember-scoped-css",
|
|
35
|
+
"ember-intl/helpers/format-message"
|
|
36
|
+
]);
|
|
37
|
+
const babelRequiredImports = [
|
|
38
|
+
...babelTemplateImports,
|
|
39
|
+
...babelMacroImports,
|
|
40
|
+
...babelOtherImports
|
|
41
|
+
];
|
|
42
|
+
const counts = {
|
|
43
|
+
total: 0,
|
|
44
|
+
babel: 0,
|
|
45
|
+
why: {
|
|
46
|
+
decorators: 0,
|
|
47
|
+
formatMessage: 0,
|
|
48
|
+
imports: {
|
|
49
|
+
templates: 0,
|
|
50
|
+
macros: 0,
|
|
51
|
+
other: 0
|
|
52
|
+
},
|
|
53
|
+
initializeRuntimeMacrosConfig: 0
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
function maybeBabel(options) {
|
|
57
|
+
const { env, parallel, ...restOptions } = options;
|
|
58
|
+
const original = (() => {
|
|
59
|
+
const babelPath = resolve(join(process.cwd(), "./babel.config.js"));
|
|
60
|
+
return babel({
|
|
61
|
+
babelHelpers: "runtime",
|
|
62
|
+
extensions,
|
|
63
|
+
skipPreflightCheck: true,
|
|
64
|
+
...restOptions,
|
|
65
|
+
configFile: babelPath,
|
|
66
|
+
parallel: parallel ?? true
|
|
67
|
+
});
|
|
68
|
+
})();
|
|
69
|
+
/**
|
|
70
|
+
* In @rollup/plugin-babel v7+, the `transform` hook is the object form
|
|
71
|
+
* `{ filter, handler }` rather than a plain function. Normalize to the
|
|
72
|
+
* underlying handler so we can invoke it directly.
|
|
73
|
+
*/
|
|
74
|
+
const originalTransform = typeof original.transform === "function" ? original.transform : original.transform?.handler;
|
|
75
|
+
const babelMacros = new Set(babelRequiredImports);
|
|
76
|
+
if (env.mode === "development") babelMacros.delete("@ember/debug");
|
|
77
|
+
async function doTransform(code, id) {
|
|
78
|
+
counts.total++;
|
|
79
|
+
const ext = id.split(".").at(-1);
|
|
80
|
+
const lang = ext === "gjs" ? "js" : ext === "gts" ? "ts" : ext;
|
|
81
|
+
let needsBabel = false;
|
|
82
|
+
walk((await parse(id, code, { lang })).program, {}, {
|
|
83
|
+
Decorator(_node, { stop }) {
|
|
84
|
+
needsBabel = true;
|
|
85
|
+
counts.why.decorators++;
|
|
86
|
+
stop();
|
|
87
|
+
},
|
|
88
|
+
MemberExpression(node, { stop }) {
|
|
89
|
+
if (node.property?.name === "formatMessage" && node.object?.name === "intl") {
|
|
90
|
+
needsBabel = true;
|
|
91
|
+
counts.why.formatMessage++;
|
|
92
|
+
stop();
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
ImportDeclaration(node, { stop }) {
|
|
96
|
+
const value = node.source.value;
|
|
97
|
+
if (babelMacros.has(value)) {
|
|
98
|
+
needsBabel = true;
|
|
99
|
+
if (babelTemplateImports.has(value)) counts.why.imports.templates++;
|
|
100
|
+
else if (babelMacroImports.has(value)) counts.why.imports.macros++;
|
|
101
|
+
else counts.why.imports.other++;
|
|
102
|
+
stop();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
if (!needsBabel && code.includes("initializeRuntimeMacrosConfig")) {
|
|
107
|
+
counts.why.initializeRuntimeMacrosConfig++;
|
|
108
|
+
needsBabel = true;
|
|
109
|
+
}
|
|
110
|
+
if (needsBabel) {
|
|
111
|
+
counts.babel++;
|
|
112
|
+
return await originalTransform.call(this, code, id);
|
|
113
|
+
}
|
|
114
|
+
if (ext === "json") return;
|
|
115
|
+
if (ext === "js" || ext === "gjs") return null;
|
|
116
|
+
const result = await transform(id, code, {
|
|
117
|
+
lang,
|
|
118
|
+
typescript: {
|
|
119
|
+
onlyRemoveTypeImports: true,
|
|
120
|
+
/**
|
|
121
|
+
* We should work towards disabling this.
|
|
122
|
+
*/
|
|
123
|
+
allowNamespaces: true,
|
|
124
|
+
removeClassFieldsWithoutInitializer: false,
|
|
125
|
+
rewriteImportExtensions: false
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
if (result.errors?.length) {
|
|
129
|
+
console.error(`Errors during oxc-transform of ${id}:`);
|
|
130
|
+
for (const err of result.errors) {
|
|
131
|
+
if (err.labels) console.error(err.labels);
|
|
132
|
+
console.error(err.codeframe || err.message);
|
|
133
|
+
}
|
|
134
|
+
throw result.errors;
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
enforce: "pre",
|
|
140
|
+
buildEnd() {
|
|
141
|
+
if (process.env.INSPECT) console.debug(`Babel usage: ${counts.babel} / ${counts.total}`, counts.why);
|
|
142
|
+
},
|
|
143
|
+
...original,
|
|
144
|
+
name: "nullvoxpopuli:babel",
|
|
145
|
+
transform: {
|
|
146
|
+
filter: { id: {
|
|
147
|
+
include: [
|
|
148
|
+
/\.js/,
|
|
149
|
+
/\.gjs/,
|
|
150
|
+
/\.ts/,
|
|
151
|
+
/\.gts/
|
|
152
|
+
],
|
|
153
|
+
exclude: [/\.json/]
|
|
154
|
+
} },
|
|
155
|
+
handler(code, id) {
|
|
156
|
+
try {
|
|
157
|
+
return doTransform.call(this, code, id);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
debugger;
|
|
160
|
+
throw e;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/ember.ts
|
|
168
|
+
const emberConfig = ember$1()[3];
|
|
169
|
+
const cwd = process.cwd();
|
|
170
|
+
function absolutePath(relativePath) {
|
|
171
|
+
return join(cwd, relativePath);
|
|
172
|
+
}
|
|
173
|
+
function ember(nvpConfig = {}) {
|
|
174
|
+
const babelConfigFile = resolve(join(process.cwd(), "./babel.config.js"));
|
|
175
|
+
return [
|
|
176
|
+
{
|
|
177
|
+
name: "ember:nullvoxpopuli",
|
|
178
|
+
async config(viteConfig, env) {
|
|
179
|
+
await emberConfig.config(viteConfig, env);
|
|
180
|
+
viteConfig.experimental ||= {};
|
|
181
|
+
viteConfig.build ||= {};
|
|
182
|
+
viteConfig.optimizeDeps ||= {};
|
|
183
|
+
viteConfig.oxc = {};
|
|
184
|
+
viteConfig.experimental.nativeMagicString = true;
|
|
185
|
+
delete viteConfig.esbuild;
|
|
186
|
+
delete viteConfig.optimizeDeps.esbuildOptions;
|
|
187
|
+
viteConfig.build.reportCompressedSize = false;
|
|
188
|
+
viteConfig.build.extensions = extensions;
|
|
189
|
+
viteConfig.optimizeDeps.extensions = [".gjs", ".gts"];
|
|
190
|
+
viteConfig.optimizeDeps.rolldownOptions ||= {};
|
|
191
|
+
viteConfig.optimizeDeps.rolldownOptions.tsconfig = true;
|
|
192
|
+
applyConfig(viteConfig, env, nvpConfig);
|
|
193
|
+
viteConfig.optimizeDeps.rolldownOptions.plugins = [
|
|
194
|
+
resolver({ rolldown: true }),
|
|
195
|
+
templateTag(),
|
|
196
|
+
maybeBabel({
|
|
197
|
+
env,
|
|
198
|
+
parallel: nvpConfig.babel?.parallel,
|
|
199
|
+
configFile: babelConfigFile
|
|
200
|
+
})
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
resolver({ rolldown: true }),
|
|
205
|
+
templateTag(),
|
|
206
|
+
maybeBabel({
|
|
207
|
+
env: { mode: "development" },
|
|
208
|
+
parallel: nvpConfig.babel?.parallel,
|
|
209
|
+
configFile: babelConfigFile
|
|
210
|
+
})
|
|
211
|
+
];
|
|
212
|
+
}
|
|
213
|
+
const MODE_DEV = "development";
|
|
214
|
+
function isDev(mode) {
|
|
215
|
+
return mode === MODE_DEV;
|
|
216
|
+
}
|
|
217
|
+
function applyConfig(viteConfig, env, nvpConfig) {
|
|
218
|
+
if (isDev(env.mode)) return dev(viteConfig, nvpConfig);
|
|
219
|
+
return prod(viteConfig, nvpConfig);
|
|
220
|
+
}
|
|
221
|
+
function dev(viteConfig, nvpConfig) {
|
|
222
|
+
/**************************************
|
|
223
|
+
*
|
|
224
|
+
* build config for tests
|
|
225
|
+
*
|
|
226
|
+
* NOTE: we can't minify because some tests
|
|
227
|
+
* are checking x.constructor.name, which changes when minified
|
|
228
|
+
*
|
|
229
|
+
*************************************/
|
|
230
|
+
Object.assign(viteConfig.build, {
|
|
231
|
+
sourcemap: true,
|
|
232
|
+
cleanCssOptions: { sourceMap: true },
|
|
233
|
+
minify: false,
|
|
234
|
+
cssMinify: false
|
|
235
|
+
});
|
|
236
|
+
viteConfig.build.rolldownOptions ||= {};
|
|
237
|
+
viteConfig.build.rolldownOptions.input ||= {};
|
|
238
|
+
viteConfig.build.rolldownOptions.output ||= {};
|
|
239
|
+
Object.assign(viteConfig.build.rolldownOptions.input, { main: absolutePath("./index.html") });
|
|
240
|
+
if (existsSync("./tests.html")) Object.assign(viteConfig.build.rolldownOptions.input, { tests: absolutePath("./tests.html") });
|
|
241
|
+
if (existsSync("./tests/index.html")) Object.assign(viteConfig.build.rolldownOptions.input, { tests: absolutePath("./tests/index.html") });
|
|
242
|
+
Object.assign(viteConfig.build.rolldownOptions.output, {
|
|
243
|
+
cleanDir: true,
|
|
244
|
+
strictExecutionOrder: true,
|
|
245
|
+
codeSplitting: { groups: [
|
|
246
|
+
{
|
|
247
|
+
name: "tests",
|
|
248
|
+
test: /[.-]test\.g?(j|t)s/,
|
|
249
|
+
priority: 10
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: "common",
|
|
253
|
+
minShareCount: 10,
|
|
254
|
+
minSize: 1e4,
|
|
255
|
+
maxSize: 1 * 1024 * 1024,
|
|
256
|
+
priority: 9
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
name: "uncommon",
|
|
260
|
+
minShareCount: 2,
|
|
261
|
+
minSize: 1e4,
|
|
262
|
+
maxSize: 1 * 1024 * 1024,
|
|
263
|
+
priority: 5
|
|
264
|
+
}
|
|
265
|
+
] }
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
function prod(viteConfig, nvpConfig) {
|
|
269
|
+
Object.assign(viteConfig.build, {
|
|
270
|
+
minify: "oxc",
|
|
271
|
+
sourcemap: true,
|
|
272
|
+
reportCompressedSize: false,
|
|
273
|
+
modulePreload: true,
|
|
274
|
+
cleanCssOptions: { sourceMap: true }
|
|
275
|
+
});
|
|
276
|
+
viteConfig.build.rolldownOptions ||= {};
|
|
277
|
+
viteConfig.build.rolldownOptions.input ||= {};
|
|
278
|
+
viteConfig.build.rolldownOptions.optimization ||= {};
|
|
279
|
+
viteConfig.build.rolldownOptions.output ||= {};
|
|
280
|
+
Object.assign(viteConfig.build.rolldownOptions.input, { main: absolutePath("./index.html") });
|
|
281
|
+
Object.assign(viteConfig.build.rolldownOptions.optimization, {
|
|
282
|
+
inlineConst: {
|
|
283
|
+
mode: "smart",
|
|
284
|
+
pass: 3
|
|
285
|
+
},
|
|
286
|
+
pifeForModuleWrappers: true
|
|
287
|
+
});
|
|
288
|
+
Object.assign(viteConfig.build.rolldownOptions.output, {
|
|
289
|
+
minify: true,
|
|
290
|
+
cleanDir: true,
|
|
291
|
+
/**
|
|
292
|
+
* This is needed due to how we manage module state.
|
|
293
|
+
* This can create waterfalls when too much top-level await is used.
|
|
294
|
+
*/
|
|
295
|
+
strictExecutionOrder: true,
|
|
296
|
+
/**
|
|
297
|
+
* Code splitting currently makes things worse
|
|
298
|
+
*/
|
|
299
|
+
codeSplitting: {
|
|
300
|
+
/**
|
|
301
|
+
* Kind of a shame this isn't automatic based on dynamic import usage
|
|
302
|
+
* in vite8.
|
|
303
|
+
* Kinda forces us to know every potential problem area of the app,
|
|
304
|
+
* rather than rely on automatic optimizations based on import/module usage
|
|
305
|
+
*/
|
|
306
|
+
groups: [...nvpConfig?.production?.codeSplittingGroups ?? []] }
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
//#endregion
|
|
310
|
+
export { ember, maybeBabel };
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nullvoxpopuli/ember-vite",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Meta plugin for ember projects focusing on speed.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "index.
|
|
6
|
+
"main": "dist/index.js",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"vite",
|
|
9
9
|
"ember",
|
|
@@ -29,7 +29,11 @@
|
|
|
29
29
|
"node": ">= 24"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
+
"rolldown": "^1.1.2",
|
|
32
33
|
"typescript": "^5.9.3",
|
|
33
34
|
"vite": "^8.0.14"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "rolldown --config"
|
|
34
38
|
}
|
|
35
39
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineConfig } from "rolldown";
|
|
2
|
+
|
|
3
|
+
import manifest from "./package.json" with { type: "json" };
|
|
4
|
+
|
|
5
|
+
const deps = Object.keys(manifest.dependencies || {});
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
input: "index.ts",
|
|
9
|
+
output: {
|
|
10
|
+
format: "esm",
|
|
11
|
+
file: "dist/index.js",
|
|
12
|
+
codeSplitting: false,
|
|
13
|
+
},
|
|
14
|
+
external(id) {
|
|
15
|
+
if (id.startsWith("node:")) return true;
|
|
16
|
+
if (deps.some((dep) => id.startsWith(dep))) return true;
|
|
17
|
+
},
|
|
18
|
+
});
|