-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(build): ensure linked packages are analyzed for commonjs #7094
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,8 @@ export function resolvePackageData( | |
id: string, | ||
basedir: string, | ||
preserveSymlinks = false, | ||
packageCache?: PackageCache | ||
packageCache?: PackageCache, | ||
cjsInclude?: (string | RegExp)[] | ||
): PackageData | null { | ||
let pkg: PackageData | undefined | ||
let cacheKey: string | undefined | ||
|
@@ -60,8 +61,8 @@ export function resolvePackageData( | |
} | ||
let pkgPath: string | undefined | ||
try { | ||
pkgPath = resolveFrom(`${id}/package.json`, basedir, preserveSymlinks) | ||
pkg = loadPackageData(pkgPath, true, packageCache) | ||
pkgPath = resolveFrom(`${id}/package.json`, basedir, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ︎ Preserve symlinks when using |
||
pkg = loadPackageData(pkgPath, preserveSymlinks, packageCache, cjsInclude) | ||
if (packageCache) { | ||
packageCache.set(cacheKey!, pkg) | ||
} | ||
|
@@ -81,10 +82,22 @@ export function resolvePackageData( | |
export function loadPackageData( | ||
pkgPath: string, | ||
preserveSymlinks?: boolean, | ||
packageCache?: PackageCache | ||
packageCache?: PackageCache, | ||
cjsInclude?: (string | RegExp)[] | ||
): PackageData { | ||
if (!preserveSymlinks) { | ||
const originalPkgPath = pkgPath | ||
pkgPath = fs.realpathSync.native(pkgPath) | ||
|
||
// In case a linked package is a local clone of a CommonJS dependency, | ||
// we need to ensure @rollup/plugin-commonjs analyzes the package even | ||
// after it's been resolved to its actual file location. | ||
if (cjsInclude && pkgPath !== originalPkgPath) { | ||
const filter = createFilter(cjsInclude, undefined, { resolve: false }) | ||
if (!filter(pkgPath) && filter(originalPkgPath)) { | ||
cjsInclude.push(path.dirname(pkgPath) + '/**') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possible downside here is that |
||
} | ||
} | ||
} | ||
|
||
let cached: PackageData | undefined | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ export interface InternalResolveOptions extends ResolveOptions { | |
isProduction: boolean | ||
ssrConfig?: SSROptions | ||
packageCache?: PackageCache | ||
cjsInclude?: (string | RegExp)[] | ||
/** | ||
* src code mode also attempts the following: | ||
* - resolving /xxx as URLs | ||
|
@@ -81,6 +82,7 @@ export interface InternalResolveOptions extends ResolveOptions { | |
export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin { | ||
const { | ||
root, | ||
isBuild, | ||
isProduction, | ||
asSrc, | ||
ssrConfig, | ||
|
@@ -120,6 +122,9 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin { | |
...baseOptions, | ||
isFromTsImporter: isTsRequest(importer ?? '') | ||
} | ||
if (!options.isBuild) { | ||
options.cjsInclude = undefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't remember why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it's because |
||
} | ||
|
||
let res: string | PartialResolvedId | undefined | ||
|
||
|
@@ -520,7 +525,13 @@ export function tryNodeResolve( | |
|
||
let pkg: PackageData | undefined | ||
const pkgId = possiblePkgIds.reverse().find((pkgId) => { | ||
pkg = resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache)! | ||
pkg = resolvePackageData( | ||
pkgId, | ||
basedir, | ||
preserveSymlinks, | ||
packageCache, | ||
options.cjsInclude | ||
)! | ||
return pkg | ||
})! | ||
|
||
|
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.
I don't remember why
const resolvedAlias
andconst resolveOptions
declarations were moved down. We could probably move them back.. :)