Skip to content

Commit 028c592

Browse files
matejchalkBioPhoton
authored andcommitted
feat(cli): use bundle-require instead of jiti (no hackfix, but also no CJS configs)
1 parent cea97cf commit 028c592

File tree

6 files changed

+39
-137
lines changed

6 files changed

+39
-137
lines changed

package-lock.json

+19-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"@nx/devkit": "16.7.4",
1212
"@swc/helpers": "~0.5.0",
13-
"jiti": "^1.19.3",
13+
"bundle-require": "^4.0.1",
1414
"tslib": "^2.3.0",
1515
"tsup": "^7.2.0"
1616
},
@@ -24,7 +24,6 @@
2424
"@swc-node/register": "~1.4.2",
2525
"@swc/cli": "~0.1.62",
2626
"@swc/core": "~1.3.51",
27-
"@types/babel__core": "^7.20.1",
2827
"@types/eslint": "^8.44.2",
2928
"@types/jest": "^29.4.0",
3029
"@types/node": "18.7.1",

packages/cli/src/lib/babel-plugin-lighthouse-hackfix.ts

-68
This file was deleted.

packages/cli/src/lib/cli.spec.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { cli } from './cli';
22

33
describe('cli', () => {
4-
it('CommonJS', async () => {
5-
await expect(
6-
cli('./packages/cli/src/lib/config.mock.cjs')
7-
).resolves.toEqual({
8-
plugins: [{ name: 'eslint', version: '8.46.0' }],
9-
});
4+
it('.js', async () => {
5+
await expect(cli('./packages/cli/src/lib/config.mock.js')).resolves.toEqual(
6+
{
7+
plugins: [
8+
{ name: 'eslint', version: '8.46.0' },
9+
{ name: 'lighthouse', defaultConfig: expect.any(Object) },
10+
],
11+
},
12+
);
1013
});
1114

12-
it('ESM', async () => {
15+
it('.mjs', async () => {
1316
await expect(
14-
cli('./packages/cli/src/lib/config.mock.mjs')
17+
cli('./packages/cli/src/lib/config.mock.mjs'),
1518
).resolves.toEqual({
1619
plugins: [
1720
{ name: 'eslint', version: '8.46.0' },
@@ -20,14 +23,14 @@ describe('cli', () => {
2023
});
2124
});
2225

23-
it('TypeScript', async () => {
26+
it('.ts', async () => {
2427
await expect(cli('./packages/cli/src/lib/config.mock.ts')).resolves.toEqual(
2528
{
2629
plugins: [
2730
{ name: 'eslint', version: '8.46.0' },
2831
{ name: 'lighthouse', defaultConfig: expect.any(Object) },
2932
],
30-
}
33+
},
3134
);
3235
});
3336
});

packages/cli/src/lib/cli.ts

+6-37
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
1-
import { TransformOptions } from '@babel/core';
2-
import jiti from 'jiti';
3-
import { dirname, join, relative } from 'path';
4-
import { fileURLToPath } from 'url';
5-
import { babelPluginLighthouseHackfix } from './babel-plugin-lighthouse-hackfix';
1+
import { bundleRequire } from 'bundle-require';
62

73
export async function cli(configPath: string) {
8-
const path = resolveImportPath(configPath);
9-
const data = await loadModule(path);
10-
console.log('Loaded config:', data);
11-
return data;
12-
}
13-
14-
function resolveImportPath(path: string) {
15-
const absolutePath = join(process.cwd(), path);
16-
const modulePath = fileURLToPath(new URL(import.meta.url));
17-
const relativePath = relative(dirname(modulePath), absolutePath);
18-
if (!relativePath.startsWith('.')) {
19-
return `./${relativePath}`;
20-
}
21-
return relativePath;
22-
}
23-
24-
async function loadModule(path: string) {
25-
if (/\.[cm]?ts$/.test(path)) {
26-
const babelOptions: TransformOptions = {
27-
plugins: [babelPluginLighthouseHackfix],
28-
};
29-
const jitiLoader = jiti(fileURLToPath(new URL(import.meta.url)), {
30-
interopDefault: true,
31-
transformOptions: {
32-
babel: babelOptions,
33-
},
34-
});
35-
return jitiLoader(path);
36-
}
37-
38-
const module = await import(path);
39-
return module.default ?? module;
4+
const { mod } = await bundleRequire({
5+
filepath: configPath,
6+
format: 'esm',
7+
});
8+
return mod.default || mod;
409
}

packages/cli/src/lib/config.mock.cjs

-10
This file was deleted.

0 commit comments

Comments
 (0)