fix(nitro): firebase package.json is not exported (#809)

This commit is contained in:
Xin Du (Clark) 2021-10-12 18:39:55 +01:00 committed by GitHub
parent da7ff44c93
commit 3759926de5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -4,7 +4,7 @@ import fse from 'fs-extra'
import consola from 'consola' import consola from 'consola'
import globby from 'globby' import globby from 'globby'
import { writeFile } from '../utils' import { readPackageJson, writeFile } from '../utils'
import { NitroPreset, NitroContext } from '../context' import { NitroPreset, NitroContext } from '../context'
export const firebase: NitroPreset = { export const firebase: NitroPreset = {
@ -69,9 +69,8 @@ async function writeRoutes ({ output: { publicDir, serverDir }, _nuxt: { rootDir
dependencies, dependencies,
devDependencies: { devDependencies: {
'firebase-functions-test': 'latest', 'firebase-functions-test': 'latest',
'firebase-admin': _require('firebase-admin/package.json').version, 'firebase-admin': readPackageJson('firebase-admin', _require).version,
'firebase-functions': _require('firebase-functions/package.json') 'firebase-functions': readPackageJson('firebase-functions', _require).version
.version
}, },
engines: { node: nodeVersion } engines: { node: nodeVersion }
}, },

View File

@ -125,3 +125,18 @@ export function getDependencies (dir: string, mode: keyof typeof _getDependencie
export function serializeImportName (id: string) { export function serializeImportName (id: string) {
return '_' + id.replace(/[^a-zA-Z0-9_$]/g, '_') return '_' + id.replace(/[^a-zA-Z0-9_$]/g, '_')
} }
export function readPackageJson (
packageName: string,
_require: NodeRequire = createRequire(import.meta.url)
) {
try {
return _require(`${packageName}/package.json`)
} catch (error) {
if (error.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
const [pkgModulePath] = /^(.*\/node_modules\/).*$/.exec(_require.resolve(packageName))
return fse.readJSONSync(resolve(pkgModulePath, packageName, 'package.json'))
}
throw error
}
}