Skip to content

Commit 2a7e2d3

Browse files
committed
refactor: use jitsi to handle mjs configs
1 parent 1ae896f commit 2a7e2d3

5 files changed

Lines changed: 26 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ If no provider suits your specific needs, you can create a build file in your pr
7474
- `buildium.config.mts`
7575
- `buildium.config.ts`
7676
- `buildium.config.cjs`
77+
- `buildium.config.mjs`
7778
- `buildium.config.js`
7879
- `buildium.config.json`
7980
- `buildium.config.json5`
@@ -86,6 +87,7 @@ If no provider suits your specific needs, you can create a build file in your pr
8687
- `.buildium.mts`
8788
- `.buildium.ts`
8889
- `.buildium.cjs`
90+
- `.buildium.mjs`
8991
- `.buildium.js`
9092
- `.buildium.js`
9193
- `.buildium.json`

lib/buildium.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/atom-build.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ const explorer = cosmiconfig(pkg.name, {
2626
// Only the extensions whose built-in loader is wrong or missing. cosmiconfig
2727
// merges this map over `defaultLoaders`, so an extension left out here keeps
2828
// its built-in loader rather than losing support — and every entry present is
29-
// a deliberate override. The JavaScript ones displace `loadJs`, which tries a
30-
// dynamic `import()` first; `.mts`/`.cts`/`.ts` displace it too, since it does
31-
// no type stripping at all. See `loaders.ts`.
29+
// a deliberate override. `.js`/`.cjs` displace `loadJs`, which tries a dynamic
30+
// `import()` first; the transpiled ones displace it too, since it does no type
31+
// stripping and cannot read ESM here either. See `loaders.ts`.
3232
loaders: {
3333
'.cjs': loaders.javascript,
3434
'.js': loaders.javascript,
35-
'.ts': loaders.typescript,
36-
'.cts': loaders.typescript,
37-
'.mts': loaders.typescript,
35+
'.mjs': loaders.transpiled,
36+
'.ts': loaders.transpiled,
37+
'.cts': loaders.transpiled,
38+
'.mts': loaders.transpiled,
3839
'.toml': loaders.toml,
3940
'.json': loaders.jsonc,
4041
'.json5': loaders.json5,

src/loaders.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parseJSON5, parseJSONC, parseTOML, type JSONCParseError } from 'confbox';
22
import { pklLoader } from 'cosmiconfig-loader-pkl';
33
import { createJiti } from 'jiti';
4+
import path from 'path';
45

56
/** Matches cosmiconfig's `Loader` signature, which may also be synchronous. */
67
type Loader = (filePath: string, content: string) => object | null | Promise<object | null>;
@@ -81,7 +82,7 @@ function describe(error: JSONCParseError, content: string): string {
8182
*/
8283
const jiti = createJiti('', { interopDefault: true, moduleCache: false, fsCache: false });
8384

84-
const loaders: Record<'javascript' | 'json5' | 'jsonc' | 'pkl' | 'toml' | 'typescript', Loader> = {
85+
const loaders: Record<'javascript' | 'json5' | 'jsonc' | 'pkl' | 'toml' | 'transpiled', Loader> = {
8586
// Must be `require`, and must not be `import()`.
8687
//
8788
// Package code runs in Pulsar's *renderer* process, where a dynamic `import()`
@@ -166,10 +167,11 @@ const loaders: Record<'javascript' | 'json5' | 'jsonc' | 'pkl' | 'toml' | 'types
166167
}
167168
},
168169

169-
// TypeScript, and any build file written as an ES module. Neither `require`
170-
// nor cosmiconfig's `loadJs` can read these: `loadJs` does no type stripping
171-
// at all, so it falls back to `require`, which reads the source as CommonJS
172-
// JavaScript and reports `Unexpected token 'export'`.
170+
// Everything `require` cannot read, whether because of type annotations
171+
// (`.ts`, `.mts`, `.cts`) or ESM syntax (`.mjs`, and the TypeScript ones
172+
// again). cosmiconfig's `loadJs` cannot read them either: it does no type
173+
// stripping at all, so it falls back to `require`, which parses the source as
174+
// CommonJS JavaScript and reports `Unexpected token 'export'`.
173175
//
174176
// jiti transpiles and evaluates in-process, which also means the dynamic
175177
// `import()` described on the `javascript` loader is never reached. That is
@@ -178,11 +180,15 @@ const loaders: Record<'javascript' | 'json5' | 'jsonc' | 'pkl' | 'toml' | 'types
178180
// down is not an error anyone gets to see. jiti marks the synchronous call
179181
// deprecated in favour of `jiti.import()` "for better compatibility"; here
180182
// that trade runs the wrong way, so the deprecation is accepted knowingly.
181-
typescript(filePath: string) {
183+
//
184+
// The format in the error message follows the extension rather than the
185+
// loader: a failed `.mjs` is a JavaScript error, and saying otherwise would
186+
// point the reader at a language their build file is not written in.
187+
transpiled(filePath: string) {
182188
try {
183189
return unwrapDefault(jiti(filePath) as Record<string, unknown> | null);
184190
} catch (error) {
185-
rethrow('TypeScript', filePath, error);
191+
rethrow(path.extname(filePath).endsWith('js') ? 'JavaScript' : 'TypeScript', filePath, error);
186192
}
187193
}
188194
};

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ const buildFileNames = [
135135
'buildium.config.mts',
136136
'buildium.config.ts',
137137
'buildium.config.cjs',
138+
'buildium.config.mjs',
138139
'buildium.config.js',
139140
'buildium.config.json',
140141
'buildium.config.json5',
@@ -147,6 +148,7 @@ const buildFileNames = [
147148
'.buildium.mts',
148149
'.buildium.ts',
149150
'.buildium.cjs',
151+
'.buildium.mjs',
150152
'.buildium.js',
151153
'.buildium.json',
152154
'.buildium.json5',

0 commit comments

Comments
 (0)