Skip to content

Commit

Permalink
feat(cli): use bundle-require instead of jiti (no hackfix, but also n…
Browse files Browse the repository at this point in the history
…o CJS configs)
  • Loading branch information
matejchalk authored and BioPhoton committed Aug 30, 2023
1 parent cea97cf commit 028c592
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 137 deletions.
29 changes: 19 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@nx/devkit": "16.7.4",
"@swc/helpers": "~0.5.0",
"jiti": "^1.19.3",
"bundle-require": "^4.0.1",
"tslib": "^2.3.0",
"tsup": "^7.2.0"
},
Expand All @@ -24,7 +24,6 @@
"@swc-node/register": "~1.4.2",
"@swc/cli": "~0.1.62",
"@swc/core": "~1.3.51",
"@types/babel__core": "^7.20.1",
"@types/eslint": "^8.44.2",
"@types/jest": "^29.4.0",
"@types/node": "18.7.1",
Expand Down
68 changes: 0 additions & 68 deletions packages/cli/src/lib/babel-plugin-lighthouse-hackfix.ts

This file was deleted.

23 changes: 13 additions & 10 deletions packages/cli/src/lib/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { cli } from './cli';

describe('cli', () => {
it('CommonJS', async () => {
await expect(
cli('./packages/cli/src/lib/config.mock.cjs')
).resolves.toEqual({
plugins: [{ name: 'eslint', version: '8.46.0' }],
});
it('.js', async () => {
await expect(cli('./packages/cli/src/lib/config.mock.js')).resolves.toEqual(
{
plugins: [
{ name: 'eslint', version: '8.46.0' },
{ name: 'lighthouse', defaultConfig: expect.any(Object) },
],
},
);
});

it('ESM', async () => {
it('.mjs', async () => {
await expect(
cli('./packages/cli/src/lib/config.mock.mjs')
cli('./packages/cli/src/lib/config.mock.mjs'),
).resolves.toEqual({
plugins: [
{ name: 'eslint', version: '8.46.0' },
Expand All @@ -20,14 +23,14 @@ describe('cli', () => {
});
});

it('TypeScript', async () => {
it('.ts', async () => {
await expect(cli('./packages/cli/src/lib/config.mock.ts')).resolves.toEqual(
{
plugins: [
{ name: 'eslint', version: '8.46.0' },
{ name: 'lighthouse', defaultConfig: expect.any(Object) },
],
}
},
);
});
});
43 changes: 6 additions & 37 deletions packages/cli/src/lib/cli.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
import { TransformOptions } from '@babel/core';
import jiti from 'jiti';
import { dirname, join, relative } from 'path';
import { fileURLToPath } from 'url';
import { babelPluginLighthouseHackfix } from './babel-plugin-lighthouse-hackfix';
import { bundleRequire } from 'bundle-require';

export async function cli(configPath: string) {
const path = resolveImportPath(configPath);
const data = await loadModule(path);
console.log('Loaded config:', data);
return data;
}

function resolveImportPath(path: string) {
const absolutePath = join(process.cwd(), path);
const modulePath = fileURLToPath(new URL(import.meta.url));
const relativePath = relative(dirname(modulePath), absolutePath);
if (!relativePath.startsWith('.')) {
return `./${relativePath}`;
}
return relativePath;
}

async function loadModule(path: string) {
if (/\.[cm]?ts$/.test(path)) {
const babelOptions: TransformOptions = {
plugins: [babelPluginLighthouseHackfix],
};
const jitiLoader = jiti(fileURLToPath(new URL(import.meta.url)), {
interopDefault: true,
transformOptions: {
babel: babelOptions,
},
});
return jitiLoader(path);
}

const module = await import(path);
return module.default ?? module;
const { mod } = await bundleRequire({
filepath: configPath,
format: 'esm',
});
return mod.default || mod;
}
10 changes: 0 additions & 10 deletions packages/cli/src/lib/config.mock.cjs

This file was deleted.

0 comments on commit 028c592

Please sign in to comment.