Skip to content

Commit

Permalink
fix(nitro): firebase package.json is not exported (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdo authored Oct 12, 2021
1 parent ed0e7b7 commit 31454ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/presets/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fse from 'fs-extra'
import consola from 'consola'
import globby from 'globby'

import { writeFile } from '../utils'
import { readPackageJson, writeFile } from '../utils'
import { NitroPreset, NitroContext } from '../context'

export const firebase: NitroPreset = {
Expand Down Expand Up @@ -69,9 +69,8 @@ async function writeRoutes ({ output: { publicDir, serverDir }, _nuxt: { rootDir
dependencies,
devDependencies: {
'firebase-functions-test': 'latest',
'firebase-admin': _require('firebase-admin/package.json').version,
'firebase-functions': _require('firebase-functions/package.json')
.version
'firebase-admin': readPackageJson('firebase-admin', _require).version,
'firebase-functions': readPackageJson('firebase-functions', _require).version
},
engines: { node: nodeVersion }
},
Expand Down
15 changes: 15 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,18 @@ export function getDependencies (dir: string, mode: keyof typeof _getDependencie
export function serializeImportName (id: string) {
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
}
}

0 comments on commit 31454ea

Please sign in to comment.