-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support TS config files using Jiti + hackfix lighthouse import.…
…meta usages
- Loading branch information
1 parent
48cd967
commit 3b7927d
Showing
6 changed files
with
112 additions
and
158 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { NodePath, PluginObj } from '@babel/core'; | ||
import type { CallExpression } from '@babel/types'; | ||
|
||
type Babel = typeof import('@babel/core'); | ||
|
||
// replace `import.meta` in Lighthouse source code (incompatible with Jiti) | ||
// often passed to getModuleDirectory and getModulePath: https://github.com/GoogleChrome/lighthouse/blob/main/esm-utils.js#L20-L32 | ||
export function babelPluginLighthouseHackfix({ types: t }: Babel): PluginObj { | ||
return { | ||
visitor: { | ||
CallExpression: (path) => { | ||
if ( | ||
path.node.callee.type === 'Identifier' && | ||
(path.node.callee.name === 'getModuleDirectory' || | ||
path.node.callee.name === 'getModulePath') && | ||
path.node.arguments.length === 1 && | ||
path.node.arguments[0].type === 'MetaProperty' && | ||
path.node.arguments[0].meta.name === 'import' && | ||
path.node.arguments[0].property.name === 'meta' | ||
) { | ||
const dirname = getDirname(path); | ||
path.replaceWith(t.stringLiteral(dirname)); | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function getDirname(path: NodePath<CallExpression>) { | ||
if ( | ||
path.parent.type === 'VariableDeclarator' && | ||
path.parent.id.type === 'Identifier' | ||
) { | ||
if (path.parent.id.name === 'LH_ROOT') { | ||
// https://github.com/GoogleChrome/lighthouse/blob/main/root.js#L11 | ||
return './node_modules/lighthouse'; | ||
} | ||
|
||
const nextSibling = | ||
path.parentPath.parent.type === 'VariableDeclaration' | ||
? path.parentPath.parentPath?.getNextSibling() | ||
: null; | ||
const nextIdentifierName = | ||
(nextSibling?.node.type === 'VariableDeclaration' && | ||
nextSibling.node.declarations[0].id.type === 'Identifier' && | ||
nextSibling.node.declarations[0].id.name) || | ||
null; | ||
|
||
if ( | ||
nextIdentifierName === 'FLOW_REPORT_TEMPLATE' || | ||
nextIdentifierName === 'REPORT_TEMPLATE' | ||
) { | ||
// https://github.com/GoogleChrome/lighthouse/blob/main/report/generator/flow-report-assets.js#L12 | ||
// https://github.com/GoogleChrome/lighthouse/blob/main/report/generator/report-assets.js#L13 | ||
return './node_modules/lighthouse/report/generator'; | ||
} | ||
|
||
if ( | ||
nextIdentifierName === 'LOCALE_MESSAGES' || | ||
nextIdentifierName === 'files' | ||
) | ||
// https://github.com/GoogleChrome/lighthouse/blob/main/shared/localization/format.js#L15 | ||
// https://github.com/GoogleChrome/lighthouse/blob/main/shared/localization/locales.js#L31 | ||
return './node_modules/lighthouse/shared/localization'; | ||
} | ||
|
||
return ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters