@nullvoxpopuli/ember-vite 2.1.1 → 2.2.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 +2 -2
- package/dist/index.js +26 -0
- package/package.json +1 -1
- package/src/ember.test.ts +75 -0
- package/src/ember.ts +37 -0
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ Then:
|
|
|
35
35
|
## Requirements
|
|
36
36
|
|
|
37
37
|
- `type=module` in your ember app
|
|
38
|
-
- babel config must be named `babel.config.js`
|
|
38
|
+
- babel config must be named `babel.config.js`, or passed as `babel.configFile`
|
|
39
39
|
- `@embroider/vite` is up to date~ish
|
|
40
40
|
|
|
41
41
|
## Configuration
|
|
@@ -65,4 +65,4 @@ What this plugin sets in `build`, `build.rolldownOptions.output` and `build.roll
|
|
|
65
65
|
are defaults: a setting there in your own vite config wins,
|
|
66
66
|
for example `build.sourcemap: "hidden"` or `build.rolldownOptions.output.strictExecutionOrder: false`.
|
|
67
67
|
|
|
68
|
-
If the project depends on `ember-concurrency`, `ember-intl`, `ember-scoped-css` or `unplugin-icons`, babel compiles their modules with no option.
|
|
68
|
+
If the project depends on `ember-concurrency`, `ember-intl`, `ember-qunit-nice-errors`, `ember-scoped-css` or `unplugin-icons`, babel compiles their modules with no option.
|
package/dist/index.js
CHANGED
|
@@ -134,6 +134,23 @@ function defaultBabelPlugins(mode) {
|
|
|
134
134
|
];
|
|
135
135
|
}
|
|
136
136
|
/**
|
|
137
|
+
* Other names babel loads a project config from.
|
|
138
|
+
*/
|
|
139
|
+
const OTHER_BABEL_CONFIGS = [
|
|
140
|
+
"babel.config.mjs",
|
|
141
|
+
"babel.config.cjs",
|
|
142
|
+
"babel.config.cts",
|
|
143
|
+
"babel.config.json"
|
|
144
|
+
];
|
|
145
|
+
/**
|
|
146
|
+
* A v2 addon. Its babel config is for publishing, so vite keeps the default plugins.
|
|
147
|
+
*/
|
|
148
|
+
function isLibrary() {
|
|
149
|
+
const manifestPath = join(process.cwd(), "package.json");
|
|
150
|
+
if (!existsSync(manifestPath)) return false;
|
|
151
|
+
return JSON.parse(readFileSync(manifestPath, "utf-8"))["ember-addon"]?.type === "addon";
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
137
154
|
* The project's babel config wins when it exists.
|
|
138
155
|
* `babel.configFile` selects which one (e.g. a test-only `config/test/babel.config.js`).
|
|
139
156
|
*
|
|
@@ -141,10 +158,17 @@ function defaultBabelPlugins(mode) {
|
|
|
141
158
|
* so no config file is required.
|
|
142
159
|
* Babel helpers are inlined in that case:
|
|
143
160
|
* projects without a babel config don't carry `@babel/runtime`.
|
|
161
|
+
*
|
|
162
|
+
* An app whose config has another name would otherwise get the defaults without noticing,
|
|
163
|
+
* so that is an error.
|
|
144
164
|
*/
|
|
145
165
|
function babelOptions(nvpConfig) {
|
|
146
166
|
const configFile = resolve(join(process.cwd(), nvpConfig.babel?.configFile ?? "./babel.config.js"));
|
|
147
167
|
if (existsSync(configFile)) return { configFile };
|
|
168
|
+
if (!nvpConfig.babel?.configFile && !isLibrary()) {
|
|
169
|
+
const otherConfig = OTHER_BABEL_CONFIGS.find((name) => existsSync(join(process.cwd(), name)));
|
|
170
|
+
if (otherConfig) throw new Error(`@nullvoxpopuli/ember-vite reads babel.config.js, but this project has ${otherConfig}. Pass \`ember({ babel: { configFile: "./${otherConfig}" } })\` to use it.`);
|
|
171
|
+
}
|
|
148
172
|
/**
|
|
149
173
|
* The config hook fills this list, once it knows the vite mode.
|
|
150
174
|
* The babel plugins keep a reference to it, so they see the filled list.
|
|
@@ -161,6 +185,7 @@ function babelOptions(nvpConfig) {
|
|
|
161
185
|
*
|
|
162
186
|
* - ember-concurrency's decorators
|
|
163
187
|
* - ember-intl's formatMessage macro
|
|
188
|
+
* - ember-qunit-nice-errors, which adds a message to each QUnit assertion
|
|
164
189
|
* - ember-scoped-css transforms
|
|
165
190
|
* - unplugin-icons, whose icon modules compile with `template()`.
|
|
166
191
|
* Their ids have no file extension, so only the id matches them.
|
|
@@ -171,6 +196,7 @@ const LIBRARY_FILTERS = {
|
|
|
171
196
|
imports: ["ember-intl/helpers/format-message"],
|
|
172
197
|
code: [/\bintl\.formatMessage\b/]
|
|
173
198
|
},
|
|
199
|
+
"ember-qunit-nice-errors": { code: [/\bassert\.[a-zA-Z]+\(/] },
|
|
174
200
|
"ember-scoped-css": { imports: ["ember-scoped-css"] },
|
|
175
201
|
"unplugin-icons": { ids: [/^~icons\//] }
|
|
176
202
|
};
|
package/package.json
CHANGED
package/src/ember.test.ts
CHANGED
|
@@ -265,6 +265,7 @@ function inProjectWithLibraries() {
|
|
|
265
265
|
const devDependencies = {
|
|
266
266
|
"ember-concurrency": "*",
|
|
267
267
|
"ember-intl": "*",
|
|
268
|
+
"ember-qunit-nice-errors": "*",
|
|
268
269
|
"ember-scoped-css": "*",
|
|
269
270
|
"unplugin-icons": "*",
|
|
270
271
|
};
|
|
@@ -315,6 +316,73 @@ describe("babel config file", () => {
|
|
|
315
316
|
// @ts-expect-error the babel plugin has function hooks
|
|
316
317
|
expect(babel.applyToEnvironment.call(CONTEXT, { name: "client" })).toBe(true);
|
|
317
318
|
});
|
|
319
|
+
|
|
320
|
+
it.each(["babel.config.mjs", "babel.config.cjs", "babel.config.cts", "babel.config.json"])(
|
|
321
|
+
"says how to use %s instead of ignoring it",
|
|
322
|
+
(name) => {
|
|
323
|
+
inProject({ "package.json": `{ "type": "module" }`, [name]: "{}" });
|
|
324
|
+
|
|
325
|
+
expect(() => ember()).toThrow(
|
|
326
|
+
`@nullvoxpopuli/ember-vite reads babel.config.js, but this project has ${name}. ` +
|
|
327
|
+
`Pass \`ember({ babel: { configFile: "./${name}" } })\` to use it.`,
|
|
328
|
+
);
|
|
329
|
+
},
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
it("keeps the default plugins for a library, whose babel config is for publishing", () => {
|
|
333
|
+
inProject({
|
|
334
|
+
"package.json": `{ "type": "module", "ember-addon": { "version": 2, "type": "addon" } }`,
|
|
335
|
+
"babel.config.cjs": "module.exports = {};",
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
expect(() => ember()).not.toThrow();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it("uses babel.config.js when another config sits next to it", () => {
|
|
342
|
+
inProject({
|
|
343
|
+
"package.json": `{ "type": "module" }`,
|
|
344
|
+
"babel.config.js": "export default { plugins: [] };",
|
|
345
|
+
"babel.config.cjs": "module.exports = {};",
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
expect(() => ember()).not.toThrow();
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
const MARKER_PLUGIN = [
|
|
352
|
+
`export default ({ types: t }) => ({`,
|
|
353
|
+
` visitor: {`,
|
|
354
|
+
` Program(path) {`,
|
|
355
|
+
` path.unshiftContainer("body", t.expressionStatement(t.stringLiteral("from the config file")));`,
|
|
356
|
+
` },`,
|
|
357
|
+
` },`,
|
|
358
|
+
`});`,
|
|
359
|
+
].join("\n");
|
|
360
|
+
|
|
361
|
+
it("uses the config that babel.configFile names", async () => {
|
|
362
|
+
const directory = inProject({
|
|
363
|
+
"package.json": `{ "type": "module" }`,
|
|
364
|
+
"marker-plugin.js": MARKER_PLUGIN,
|
|
365
|
+
"babel.config.mjs": `export default { plugins: ["./marker-plugin.js"] };`,
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
const babel = await startedBabel(
|
|
369
|
+
ember({ babel: { configFile: "./babel.config.mjs" } }) as Plugin[],
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
try {
|
|
373
|
+
// @ts-expect-error the babel plugin has an object transform hook
|
|
374
|
+
const result = await babel.transform.handler.call(
|
|
375
|
+
CONTEXT,
|
|
376
|
+
`import { macroCondition } from "@embroider/macros";`,
|
|
377
|
+
join(directory, "app/a.js"),
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
expect(result.code).toContain("from the config file");
|
|
381
|
+
} finally {
|
|
382
|
+
// @ts-expect-error the babel plugin ends its workers in closeBundle
|
|
383
|
+
await babel.closeBundle.call({ meta: { watchMode: false } });
|
|
384
|
+
}
|
|
385
|
+
});
|
|
318
386
|
});
|
|
319
387
|
|
|
320
388
|
describe("babel filter in the dev server", () => {
|
|
@@ -507,6 +575,11 @@ describe("libraries that need babel", () => {
|
|
|
507
575
|
`export const title = this.intl.formatMessage("Hello");`,
|
|
508
576
|
"/app/components/a.js",
|
|
509
577
|
),
|
|
578
|
+
"ember-qunit-nice-errors": interpreter(
|
|
579
|
+
filter,
|
|
580
|
+
`test("it works", (assert) => assert.ok(true));`,
|
|
581
|
+
"/tests/unit/a-test.js",
|
|
582
|
+
),
|
|
510
583
|
"ember-scoped-css": interpreter(
|
|
511
584
|
filter,
|
|
512
585
|
`import { scopedClass } from "ember-scoped-css";`,
|
|
@@ -524,6 +597,7 @@ describe("libraries that need babel", () => {
|
|
|
524
597
|
"ember-concurrency": false,
|
|
525
598
|
"ember-intl formatMessage": false,
|
|
526
599
|
"ember-intl import": false,
|
|
600
|
+
"ember-qunit-nice-errors": false,
|
|
527
601
|
"ember-scoped-css": false,
|
|
528
602
|
"unplugin-icons": false,
|
|
529
603
|
}
|
|
@@ -538,6 +612,7 @@ describe("libraries that need babel", () => {
|
|
|
538
612
|
"ember-concurrency": true,
|
|
539
613
|
"ember-intl formatMessage": true,
|
|
540
614
|
"ember-intl import": true,
|
|
615
|
+
"ember-qunit-nice-errors": true,
|
|
541
616
|
"ember-scoped-css": true,
|
|
542
617
|
"unplugin-icons": true,
|
|
543
618
|
}
|
package/src/ember.ts
CHANGED
|
@@ -97,6 +97,27 @@ type BabelOptions =
|
|
|
97
97
|
plugins: ReturnType<typeof defaultBabelPlugins>;
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Other names babel loads a project config from.
|
|
102
|
+
*/
|
|
103
|
+
const OTHER_BABEL_CONFIGS = [
|
|
104
|
+
"babel.config.mjs",
|
|
105
|
+
"babel.config.cjs",
|
|
106
|
+
"babel.config.cts",
|
|
107
|
+
"babel.config.json",
|
|
108
|
+
];
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A v2 addon. Its babel config is for publishing, so vite keeps the default plugins.
|
|
112
|
+
*/
|
|
113
|
+
function isLibrary(): boolean {
|
|
114
|
+
const manifestPath = join(process.cwd(), "package.json");
|
|
115
|
+
|
|
116
|
+
if (!existsSync(manifestPath)) return false;
|
|
117
|
+
|
|
118
|
+
return JSON.parse(readFileSync(manifestPath, "utf-8"))["ember-addon"]?.type === "addon";
|
|
119
|
+
}
|
|
120
|
+
|
|
100
121
|
/**
|
|
101
122
|
* The project's babel config wins when it exists.
|
|
102
123
|
* `babel.configFile` selects which one (e.g. a test-only `config/test/babel.config.js`).
|
|
@@ -105,6 +126,9 @@ type BabelOptions =
|
|
|
105
126
|
* so no config file is required.
|
|
106
127
|
* Babel helpers are inlined in that case:
|
|
107
128
|
* projects without a babel config don't carry `@babel/runtime`.
|
|
129
|
+
*
|
|
130
|
+
* An app whose config has another name would otherwise get the defaults without noticing,
|
|
131
|
+
* so that is an error.
|
|
108
132
|
*/
|
|
109
133
|
function babelOptions(nvpConfig: Config): BabelOptions {
|
|
110
134
|
const configFile = resolve(
|
|
@@ -115,6 +139,17 @@ function babelOptions(nvpConfig: Config): BabelOptions {
|
|
|
115
139
|
return { configFile };
|
|
116
140
|
}
|
|
117
141
|
|
|
142
|
+
if (!nvpConfig.babel?.configFile && !isLibrary()) {
|
|
143
|
+
const otherConfig = OTHER_BABEL_CONFIGS.find((name) => existsSync(join(process.cwd(), name)));
|
|
144
|
+
|
|
145
|
+
if (otherConfig) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`@nullvoxpopuli/ember-vite reads babel.config.js, but this project has ${otherConfig}. ` +
|
|
148
|
+
`Pass \`ember({ babel: { configFile: "./${otherConfig}" } })\` to use it.`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
118
153
|
/**
|
|
119
154
|
* The config hook fills this list, once it knows the vite mode.
|
|
120
155
|
* The babel plugins keep a reference to it, so they see the filled list.
|
|
@@ -132,6 +167,7 @@ function babelOptions(nvpConfig: Config): BabelOptions {
|
|
|
132
167
|
*
|
|
133
168
|
* - ember-concurrency's decorators
|
|
134
169
|
* - ember-intl's formatMessage macro
|
|
170
|
+
* - ember-qunit-nice-errors, which adds a message to each QUnit assertion
|
|
135
171
|
* - ember-scoped-css transforms
|
|
136
172
|
* - unplugin-icons, whose icon modules compile with `template()`.
|
|
137
173
|
* Their ids have no file extension, so only the id matches them.
|
|
@@ -145,6 +181,7 @@ const LIBRARY_FILTERS: Record<
|
|
|
145
181
|
imports: ["ember-intl/helpers/format-message"],
|
|
146
182
|
code: [/\bintl\.formatMessage\b/],
|
|
147
183
|
},
|
|
184
|
+
"ember-qunit-nice-errors": { code: [/\bassert\.[a-zA-Z]+\(/] },
|
|
148
185
|
"ember-scoped-css": { imports: ["ember-scoped-css"] },
|
|
149
186
|
"unplugin-icons": { ids: [/^~icons\//] },
|
|
150
187
|
};
|