diff --git a/package-lock.json b/package-lock.json index 8028c804c..74c9990b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,8 @@ "dist/packages/*" ], "dependencies": { - "@swc/helpers": "~0.5.0" + "@swc/helpers": "~0.5.0", + "jiti": "^1.19.3" }, "devDependencies": { "@nx/js": "16.7.0", @@ -22,6 +23,7 @@ "@nx/workspace": "16.7.0", "@swc/cli": "~0.1.62", "@swc/core": "~1.3.51", + "@types/babel__core": "^7.20.1", "@types/eslint": "^8.44.2", "@types/node": "18.7.1", "@vitest/coverage-c8": "~0.32.0", @@ -3067,8 +3069,6 @@ "version": "7.20.1", "dev": true, "license": "MIT", - "optional": true, - "peer": true, "dependencies": { "@babel/parser": "^7.20.7", "@babel/types": "^7.20.7", @@ -3081,8 +3081,6 @@ "version": "7.6.4", "dev": true, "license": "MIT", - "optional": true, - "peer": true, "dependencies": { "@babel/types": "^7.0.0" } @@ -3091,8 +3089,6 @@ "version": "7.4.1", "dev": true, "license": "MIT", - "optional": true, - "peer": true, "dependencies": { "@babel/parser": "^7.1.0", "@babel/types": "^7.0.0" @@ -3102,8 +3098,6 @@ "version": "7.20.1", "dev": true, "license": "MIT", - "optional": true, - "peer": true, "dependencies": { "@babel/types": "^7.20.7" } @@ -7023,20 +7017,6 @@ "version": "1.0.0", "license": "ISC" }, - "node_modules/fsevents": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", - "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", - "dev": true, - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.1", "dev": true, @@ -7828,6 +7808,14 @@ "node": "*" } }, + "node_modules/jiti": { + "version": "1.19.3", + "resolved": "https://registry.npmjs.org/jiti/-/jiti-1.19.3.tgz", + "integrity": "sha512-5eEbBDQT/jF1xg6l36P+mWGGoH9Spuy0PCdSr2dtWRDGC6ph/w9ZCL4lmESW8f8F7MwT3XKescfP0wnZWAKL9w==", + "bin": { + "jiti": "bin/jiti.js" + } + }, "node_modules/jpeg-js": { "version": "0.4.4", "license": "BSD-3-Clause" @@ -8980,126 +8968,6 @@ "node": ">=10" } }, - "node_modules/nx/node_modules/@nx/nx-darwin-arm64": { - "version": "16.7.0", - "resolved": "https://registry.npmjs.org/@nx/nx-darwin-arm64/-/nx-darwin-arm64-16.7.0.tgz", - "integrity": "sha512-J7UYS8Rp/Eyjh5RI2l1sydDofbSd8FfXJat0r2uAfN9qxAHJD9DijC08bezSiZqsmkF9IwVkFFufDnbM1uSlxg==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/nx/node_modules/@nx/nx-darwin-x64": { - "version": "16.7.0", - "resolved": "https://registry.npmjs.org/@nx/nx-darwin-x64/-/nx-darwin-x64-16.7.0.tgz", - "integrity": "sha512-gya03azE7iRjozZ/PTX86sw6GXzfAxIqInD47sNFzJbDP7zByMkwoPnfPxyBQDjm8e1UhrfrNgTJSoCdfZ9c5w==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/nx/node_modules/@nx/nx-freebsd-x64": { - "version": "16.7.0", - "resolved": "https://registry.npmjs.org/@nx/nx-freebsd-x64/-/nx-freebsd-x64-16.7.0.tgz", - "integrity": "sha512-DC/Oi4E4aIxkN8HHcSWxoDr+MoamL6LKLWHx/bauHCoDj8NomSLDTLauffd3kFYicMqv8k1hiWB2WAsXAVALjQ==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "freebsd" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/nx/node_modules/@nx/nx-linux-arm-gnueabihf": { - "version": "16.7.0", - "resolved": "https://registry.npmjs.org/@nx/nx-linux-arm-gnueabihf/-/nx-linux-arm-gnueabihf-16.7.0.tgz", - "integrity": "sha512-Jya1kiY4+XPdcWdiydsIY1PgCF2j57i//oHY1D1q/FrMmGeXdEeWFSStj47fLew5wfbdHw42lQNPeFMtSYzAyA==", - "cpu": [ - "arm" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/nx/node_modules/@nx/nx-linux-arm64-gnu": { - "version": "16.7.0", - "resolved": "https://registry.npmjs.org/@nx/nx-linux-arm64-gnu/-/nx-linux-arm64-gnu-16.7.0.tgz", - "integrity": "sha512-RLRnytYuqjcb6+tq86og8KYHtb4/lRpzujXeTckfoe0nA/z+TkZMIc+LSGbFlIa6Voar1O6+UAw5Fc9/EC909A==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/nx/node_modules/@nx/nx-linux-arm64-musl": { - "version": "16.7.0", - "resolved": "https://registry.npmjs.org/@nx/nx-linux-arm64-musl/-/nx-linux-arm64-musl-16.7.0.tgz", - "integrity": "sha512-ZPF+Q0wX2CE81/3ynZfGPPmvMd4ABEwfJ31/7bgingcGSUJ20aIBFbZLdVjX4zO5plofTRujrggIi2SUHBoHzg==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/nx/node_modules/@nx/nx-win32-arm64-msvc": { - "version": "16.7.0", - "resolved": "https://registry.npmjs.org/@nx/nx-win32-arm64-msvc/-/nx-win32-arm64-msvc-16.7.0.tgz", - "integrity": "sha512-JmLH63ntsunlxveXTU8f5jMKZGNPXU++I8NKd+A+Texb5h90zoc7GDvyVImFTXzx0duU1CGjreQRiBqiOcQ4Ew==", - "cpu": [ - "arm64" - ], - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">= 10" - } - }, - "node_modules/nx/node_modules/@nx/nx-win32-x64-msvc": { - "version": "16.7.0", - "resolved": "https://registry.npmjs.org/@nx/nx-win32-x64-msvc/-/nx-win32-x64-msvc-16.7.0.tgz", - "integrity": "sha512-R8erkoQ/+6HOCC9JTd3wMIa/VhfCR1Lwzws0mhSe0i5IU1mYdiZi67K8DchSXuLUheeEAZOQB4jW0c6P2jMgWA==", - "cpu": [ - "x64" - ], - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">= 10" - } - }, "node_modules/oauth-sign": { "version": "0.9.0", "dev": true, diff --git a/package.json b/package.json index 1f74482fd..325f7bba6 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,13 @@ "postinstall": "patch-package" }, "private": true, + "workspaces": [ + "dist/packages/*" + ], + "dependencies": { + "@swc/helpers": "~0.5.0", + "jiti": "^1.19.3" + }, "devDependencies": { "@nx/js": "16.7.0", "@nx/rollup": "16.7.0", @@ -13,6 +20,7 @@ "@nx/workspace": "16.7.0", "@swc/cli": "~0.1.62", "@swc/core": "~1.3.51", + "@types/babel__core": "^7.20.1", "@types/eslint": "^8.44.2", "@types/node": "18.7.1", "@vitest/coverage-c8": "~0.32.0", @@ -26,11 +34,5 @@ "typescript": "~5.1.3", "vite": "~4.3.9", "vitest": "~0.32.0" - }, - "dependencies": { - "@swc/helpers": "~0.5.0" - }, - "workspaces": [ - "dist/packages/*" - ] + } } diff --git a/packages/cli/project.json b/packages/cli/project.json index e0b35f7aa..42a14f0a6 100644 --- a/packages/cli/project.json +++ b/packages/cli/project.json @@ -15,6 +15,7 @@ "project": "packages/cli/package.json", "compiler": "swc", "format": ["esm"], + "external": "all", "rollupConfig": "rollup.config.js" } }, diff --git a/packages/cli/src/lib/babel-plugin-lighthouse-hackfix.ts b/packages/cli/src/lib/babel-plugin-lighthouse-hackfix.ts new file mode 100644 index 000000000..80a0eb8bc --- /dev/null +++ b/packages/cli/src/lib/babel-plugin-lighthouse-hackfix.ts @@ -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) { + 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 ''; +} diff --git a/packages/cli/src/lib/cli.ts b/packages/cli/src/lib/cli.ts index eee79d9d5..89473975c 100644 --- a/packages/cli/src/lib/cli.ts +++ b/packages/cli/src/lib/cli.ts @@ -1,15 +1,12 @@ +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'; export async function cli(configPath: string) { const path = resolveImportPath(configPath); - // if (/\.[cm]?ts$/.test(path)) { - // console.log('ts-node/register'); - // const { register } = await import('ts-node'); - // register(); - // } - const module = await import(path); - const data = module.default ?? module; + const data = await loadModule(path); console.log('Loaded config:', data); return data; } @@ -23,3 +20,21 @@ function resolveImportPath(path: string) { } 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; +} diff --git a/packages/plugin-lighthouse/project.json b/packages/plugin-lighthouse/project.json index d87b23fa9..1eaa8a4df 100644 --- a/packages/plugin-lighthouse/project.json +++ b/packages/plugin-lighthouse/project.json @@ -14,7 +14,7 @@ "assets": [], "project": "packages/plugin-lighthouse/package.json", "compiler": "swc", - "format": ["cjs", "esm"], + "format": ["esm"], "external": "all", "generateExportsField": true, "rollupConfig": "rollup.config.js"