Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

aleclarson
Copy link
Member

Description

Ensure linked packages are analyzed for commonjs if the symlink pointing to them is matched by any of the commonjsOptions.include patterns.

Additional context

Fixes #5668
Fixes #2697

Thanks to @fwouts for sponsoring this PR :)


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

…if the symlink pointing to them is matched by any of the `commonjsOptions.include` patterns.
}

// resolve alias with internal client alias
const resolvedAlias = normalizeAlias(
Copy link
Member Author

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 and const resolveOptions declarations were moved down. We could probably move them back.. :)

@@ -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)
Copy link
Member Author

@aleclarson aleclarson Feb 25, 2022

Choose a reason for hiding this comment

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

ℹ︎ Preserve symlinks when using resolveFrom so we have the symlink path inside loadPackageData.

if (cjsInclude && pkgPath !== originalPkgPath) {
const filter = createFilter(cjsInclude, undefined, { resolve: false })
if (!filter(pkgPath) && filter(originalPkgPath)) {
cjsInclude.push(path.dirname(pkgPath) + '/**')
Copy link
Member Author

Choose a reason for hiding this comment

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

Possible downside here is that node_modules might be inadvertently matched? It's not easy to determine the source folder in a package, so might have to live with it.

@@ -120,6 +122,9 @@ export function resolvePlugin(baseOptions: InternalResolveOptions): Plugin {
...baseOptions,
isFromTsImporter: isTsRequest(importer ?? '')
}
if (!options.isBuild) {
options.cjsInclude = undefined
Copy link
Member Author

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 cjsInclude is disabled in dev mode, but it would be nice to figure out why and add a comment here.. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose it's because @rollup/plugin-commonjs isn't used in dev mode 😆

@TeckTn
Copy link

TeckTn commented Mar 4, 2022

Is there any chance that this fix will pass in v2.9 ? It’s needed to migrate my CRA to vite.

@Niputi
Copy link
Contributor

Niputi commented Mar 4, 2022

@TeckTn until this is relased, if it will be check out this section https://vitejs.dev/guide/dep-pre-bundling.html#monorepos-and-linked-dependencies

@fwouts
Copy link
Contributor

fwouts commented Apr 8, 2022

@aleclarson out of curiosity, will this fix #2139 once merged? :)

@aleclarson
Copy link
Member Author

@fwouts No I don't think it will. IIRC, the issue there is related to @rollup/plugin-commonjs

@fwouts
Copy link
Contributor

fwouts commented May 12, 2022

What can we do to help get this PR merged? :)

@bluwy
Copy link
Member

bluwy commented May 13, 2022

IIUC this only automates setting build.commonjsOptions.include for linked packages found while loading package data. This only works in build though, in dev we also have to optimize them (and watch for changes), for it to completely work ootb.

I'm not sure if we want to merge a partial solution, and the regex trick is a bit hacky. Maybe it would be better to retrieve linked packages while performing a scan, and have the Vite dev server and build handle accordingly.

@patak-dev
Copy link
Member

@fwouts is this still an issue when using esbuild deps optimization at build time? I think we may want to avoid adding more complexity to the rollup plugin commonjs if it wont be recommended in the future

@fwouts
Copy link
Contributor

fwouts commented Jun 14, 2022

@fwouts is this still an issue when using esbuild deps optimization at build time? I think we may want to avoid adding more complexity to the rollup plugin commonjs if it wont be recommended in the future

I haven't tried it yet (sorry haven't had the bandwidth) but I expect it wouldn't be anymore!

@datrinh
Copy link

datrinh commented Jul 15, 2022

We are facing the same issue and applying the proposed solution in docs is not enough. It does solve it for build time, but not dev time. Any progress on this?

@fwouts
Copy link
Contributor

fwouts commented Jul 15, 2022

@datrinh have you tried Vite 3?

@shubhamsWEB
Copy link

shubhamsWEB commented Aug 10, 2022

Even after referring the docs for this our issue was not resolved, We are using Vite 3.0.4. Any update on this will be helpful.
I really appreciate any help you can provide.

@isc30
Copy link

isc30 commented Sep 2, 2022

same as @shubhamsWEB

@bluwy
Copy link
Member

bluwy commented Apr 1, 2023

Closing as this is outdated, with some concerns noted, and the linked issues are already closed with docs for now.

@bluwy bluwy closed this Apr 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants