Skip to content

Commit

Permalink
fix: use safe version of getPackageJson when transpiling ESM (#1008)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
eduardoboucas and kodiakhq[bot] committed Feb 8, 2022
1 parent c7159d7 commit 8c4b234
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
12 changes: 1 addition & 11 deletions src/runtimes/node/bundlers/esbuild/special_cases.ts
@@ -1,18 +1,8 @@
import { getPackageJson, PackageJson } from '../../utils/package_json'
import { getPackageJsonIfAvailable, PackageJson } from '../../utils/package_json'

const EXTERNAL_MODULES = ['@prisma/client']
const IGNORED_MODULES = ['aws-sdk']

const getPackageJsonIfAvailable = async (srcDir: string): Promise<PackageJson> => {
try {
const packageJson = await getPackageJson(srcDir)

return packageJson
} catch {
return {}
}
}

const getModulesForNextJs = ({ dependencies, devDependencies }: PackageJson) => {
const allDependencies = { ...dependencies, ...devDependencies }
const externalModules = allDependencies.next ? ['critters', 'nanoid'] : []
Expand Down
4 changes: 2 additions & 2 deletions src/runtimes/node/bundlers/nft/es_modules.ts
Expand Up @@ -7,7 +7,7 @@ import { FeatureFlags } from '../../../../feature_flags'
import { cachedReadFile, FsCache } from '../../../../utils/fs'
import { ModuleFormat } from '../../utils/module_format'
import { getNodeSupportMatrix } from '../../utils/node_version'
import { getPackageJson, PackageJson } from '../../utils/package_json'
import { getPackageJsonIfAvailable, PackageJson } from '../../utils/package_json'

import { transpile } from './transpile'

Expand Down Expand Up @@ -73,7 +73,7 @@ const processESM = async ({
}
}

const packageJson = await getPackageJson(dirname(mainFile))
const packageJson = await getPackageJsonIfAvailable(dirname(mainFile))
const nodeSupport = getNodeSupportMatrix(config.nodeVersion)

if (featureFlags.zisi_pure_esm && packageJson.type === 'module' && nodeSupport.esm) {
Expand Down
12 changes: 11 additions & 1 deletion src/runtimes/node/utils/package_json.ts
Expand Up @@ -46,4 +46,14 @@ const getPackageJson = async function (srcDir: string): Promise<PackageJson> {
}
}

export { getPackageJson, PackageJson, sanitisePackageJson }
const getPackageJsonIfAvailable = async (srcDir: string): Promise<PackageJson> => {
try {
const packageJson = await getPackageJson(srcDir)

return packageJson
} catch {
return {}
}
}

export { getPackageJson, getPackageJsonIfAvailable, PackageJson, sanitisePackageJson }

1 comment on commit 8c4b234

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⏱ Benchmark results

largeDepsEsbuild: 8.1s

largeDepsNft: 36.9s

largeDepsZisi: 1m 6.9s

Please sign in to comment.