This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: modify
type
field of transpiled ESM packages (#783)
* refactor: rename internal variables * feat: add `rewrites` object * refactor: load esbuild output from memory * feat: modify package.json of transpiled ESM packages * chore: update tests * chore: remove unused import
- Loading branch information
1 parent
7e5772d
commit c9d3337
Showing
11 changed files
with
143 additions
and
66 deletions.
There are no files selected for viewing
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,52 @@ | ||
import { resolve } from 'path' | ||
|
||
import { NodeFileTraceReasons } from '@vercel/nft' | ||
|
||
import { cachedReadFile, FsCache } from '../../../../utils/fs' | ||
import { PackageJson } from '../../utils/package_json' | ||
|
||
const getESMPackageJsons = (esmPaths: Set<string>, reasons: NodeFileTraceReasons, basePath?: string) => { | ||
const packageJsons: string[] = [...reasons.entries()] | ||
.filter(([, reason]) => { | ||
if (reason.type !== 'resolve') { | ||
return false | ||
} | ||
|
||
const hasESMParent = [...reason.parents].some((parentPath) => esmPaths.has(parentPath)) | ||
|
||
return hasESMParent | ||
}) | ||
.map(([path]) => (basePath ? resolve(basePath, path) : resolve(path))) | ||
|
||
return packageJsons | ||
} | ||
|
||
const getPatchedESMPackages = async ( | ||
esmPaths: Set<string>, | ||
reasons: NodeFileTraceReasons, | ||
fsCache: FsCache, | ||
basePath?: string, | ||
) => { | ||
const packages = getESMPackageJsons(esmPaths, reasons, basePath) | ||
const patchedPackages = await Promise.all(packages.map((path) => patchESMPackage(path, fsCache))) | ||
const patchedPackagesMap = new Map() | ||
|
||
packages.forEach((packagePath, index) => { | ||
patchedPackagesMap.set(packagePath, patchedPackages[index]) | ||
}) | ||
|
||
return patchedPackagesMap | ||
} | ||
|
||
const patchESMPackage = async (path: string, fsCache: FsCache) => { | ||
const file = (await cachedReadFile(fsCache, path, 'utf8')) as string | ||
const packageJson: PackageJson = JSON.parse(file) | ||
const patchedPackageJson = { | ||
...packageJson, | ||
type: 'commonjs', | ||
} | ||
|
||
return JSON.stringify(patchedPackageJson) | ||
} | ||
|
||
export { getPatchedESMPackages } |
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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import esm from 'esm-module' | ||
|
||
export default function getZero() { | ||
return 0 | ||
return esm() && 0 | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/fixtures/local-require-esm/node_modules/esm-module/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
tests/fixtures/local-require-esm/node_modules/esm-module/package.json
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
c9d3337
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⏱ Benchmark results
largeDepsEsbuild: 9s
largeDepsNft: 1m 2.4s
largeDepsZisi: 1m 12.9s