Skip to content

Commit 3b7927d

Browse files
matejchalkBioPhoton
authored andcommitted
feat: support TS config files using Jiti + hackfix lighthouse import.meta usages
1 parent 48cd967 commit 3b7927d

File tree

6 files changed

+112
-158
lines changed

6 files changed

+112
-158
lines changed

package-lock.json

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

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66
"postinstall": "patch-package"
77
},
88
"private": true,
9+
"workspaces": [
10+
"dist/packages/*"
11+
],
12+
"dependencies": {
13+
"@swc/helpers": "~0.5.0",
14+
"jiti": "^1.19.3"
15+
},
916
"devDependencies": {
1017
"@nx/js": "16.7.0",
1118
"@nx/rollup": "16.7.0",
1219
"@nx/vite": "16.7.0",
1320
"@nx/workspace": "16.7.0",
1421
"@swc/cli": "~0.1.62",
1522
"@swc/core": "~1.3.51",
23+
"@types/babel__core": "^7.20.1",
1624
"@types/eslint": "^8.44.2",
1725
"@types/node": "18.7.1",
1826
"@vitest/coverage-c8": "~0.32.0",
@@ -26,11 +34,5 @@
2634
"typescript": "~5.1.3",
2735
"vite": "~4.3.9",
2836
"vitest": "~0.32.0"
29-
},
30-
"dependencies": {
31-
"@swc/helpers": "~0.5.0"
32-
},
33-
"workspaces": [
34-
"dist/packages/*"
35-
]
37+
}
3638
}

packages/cli/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"project": "packages/cli/package.json",
1616
"compiler": "swc",
1717
"format": ["esm"],
18+
"external": "all",
1819
"rollupConfig": "rollup.config.js"
1920
}
2021
},
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import type { NodePath, PluginObj } from '@babel/core';
2+
import type { CallExpression } from '@babel/types';
3+
4+
type Babel = typeof import('@babel/core');
5+
6+
// replace `import.meta` in Lighthouse source code (incompatible with Jiti)
7+
// often passed to getModuleDirectory and getModulePath: https://github.com/GoogleChrome/lighthouse/blob/main/esm-utils.js#L20-L32
8+
export function babelPluginLighthouseHackfix({ types: t }: Babel): PluginObj {
9+
return {
10+
visitor: {
11+
CallExpression: (path) => {
12+
if (
13+
path.node.callee.type === 'Identifier' &&
14+
(path.node.callee.name === 'getModuleDirectory' ||
15+
path.node.callee.name === 'getModulePath') &&
16+
path.node.arguments.length === 1 &&
17+
path.node.arguments[0].type === 'MetaProperty' &&
18+
path.node.arguments[0].meta.name === 'import' &&
19+
path.node.arguments[0].property.name === 'meta'
20+
) {
21+
const dirname = getDirname(path);
22+
path.replaceWith(t.stringLiteral(dirname));
23+
}
24+
},
25+
},
26+
};
27+
}
28+
29+
function getDirname(path: NodePath<CallExpression>) {
30+
if (
31+
path.parent.type === 'VariableDeclarator' &&
32+
path.parent.id.type === 'Identifier'
33+
) {
34+
if (path.parent.id.name === 'LH_ROOT') {
35+
// https://github.com/GoogleChrome/lighthouse/blob/main/root.js#L11
36+
return './node_modules/lighthouse';
37+
}
38+
39+
const nextSibling =
40+
path.parentPath.parent.type === 'VariableDeclaration'
41+
? path.parentPath.parentPath?.getNextSibling()
42+
: null;
43+
const nextIdentifierName =
44+
(nextSibling?.node.type === 'VariableDeclaration' &&
45+
nextSibling.node.declarations[0].id.type === 'Identifier' &&
46+
nextSibling.node.declarations[0].id.name) ||
47+
null;
48+
49+
if (
50+
nextIdentifierName === 'FLOW_REPORT_TEMPLATE' ||
51+
nextIdentifierName === 'REPORT_TEMPLATE'
52+
) {
53+
// https://github.com/GoogleChrome/lighthouse/blob/main/report/generator/flow-report-assets.js#L12
54+
// https://github.com/GoogleChrome/lighthouse/blob/main/report/generator/report-assets.js#L13
55+
return './node_modules/lighthouse/report/generator';
56+
}
57+
58+
if (
59+
nextIdentifierName === 'LOCALE_MESSAGES' ||
60+
nextIdentifierName === 'files'
61+
)
62+
// https://github.com/GoogleChrome/lighthouse/blob/main/shared/localization/format.js#L15
63+
// https://github.com/GoogleChrome/lighthouse/blob/main/shared/localization/locales.js#L31
64+
return './node_modules/lighthouse/shared/localization';
65+
}
66+
67+
return '';
68+
}

packages/cli/src/lib/cli.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
import { TransformOptions } from '@babel/core';
2+
import jiti from 'jiti';
13
import { dirname, join, relative } from 'path';
24
import { fileURLToPath } from 'url';
5+
import { babelPluginLighthouseHackfix } from './babel-plugin-lighthouse-hackfix';
36

47
export async function cli(configPath: string) {
58
const path = resolveImportPath(configPath);
6-
// if (/\.[cm]?ts$/.test(path)) {
7-
// console.log('ts-node/register');
8-
// const { register } = await import('ts-node');
9-
// register();
10-
// }
11-
const module = await import(path);
12-
const data = module.default ?? module;
9+
const data = await loadModule(path);
1310
console.log('Loaded config:', data);
1411
return data;
1512
}
@@ -23,3 +20,21 @@ function resolveImportPath(path: string) {
2320
}
2421
return relativePath;
2522
}
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;
40+
}

packages/plugin-lighthouse/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"assets": [],
1515
"project": "packages/plugin-lighthouse/package.json",
1616
"compiler": "swc",
17-
"format": ["cjs", "esm"],
17+
"format": ["esm"],
1818
"external": "all",
1919
"generateExportsField": true,
2020
"rollupConfig": "rollup.config.js"

0 commit comments

Comments
 (0)