You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pnpm creates hard links from the global store to the project's node_modules folders.
For example, imagine you have the following directory structure: packages/a and packages/b have the same version of lodash as a dependency. But they are different symlink that points to different files.
Since two lodash/lodash.js points to different files, both rspack(or oxc internally) and webpack(or enhanced-resolve internally) treat the two files as unrelated. So at build time, two copies of lodash code will be packaged in the output bundle, even if their content is the same.
However, it can be found that in the file system, the two lodash.js point exactly to the same inode node. Which means they are the same file in physical storage.
$ ls -i packages/a/node_modules/lodash/lodash.js
2249211 packages/a/node_modules/lodash/lodash.js
$ ls -i packages/b/node_modules/lodash/lodash.js
2249211 packages/b/node_modules/lodash/lodash.js
To solve the problem, we created an enhanced-resolve plugin to cache the request:
importfsfrom'node:fs'importtype{ResolveRequest,Resolver}from'enhanced-resolve'classInodeWebpackPlugin{static #source ='resolved'apply(resolver: Resolver){resolver.getHook(InodeWebpackPlugin.#source).tapAsync('INodeCachePlugin',(request,_,callback)=>{if(!request.path){returncallback()}try{const{ ino }=fs.statSync(request.path)constcachedRequest=this.#cache.get(ino)if(cachedRequest){// Note that the query may change for the same path// with a different query.Object.assign(request,{ ...cachedRequest,query: request.query})returncallback()}this.#cache.set(ino,request)}catch{// explicitly ignore error}returncallback()})}
#cache =newMap<number,ResolveRequest>()}
And it works as expected in webpack, the duplicated modules have been eliminated.
So I wonder if this could be added to enhanced-resolve(just like the symlinks options).
Yea, sonds good, do you want to send a PR, also is it possible to enable it by default, i.e. pnpm has ability to detect that it is pnpm, so we will enable this option by default in webpack?
Sure! I'm glad to send a PR. But actually, I'm not 100% sure about the correctness. Since it will make the compilation unstable(depends on the order of the requests).
What problem does this feature solve?
pnpm creates hard links from the global store to the project's node_modules folders.
For example, imagine you have the following directory structure:
packages/a
andpackages/b
have the same version oflodash
as a dependency. But they are different symlink that points to different files.Since two
lodash/lodash.js
points to different files, bothrspack
(oroxc
internally) andwebpack
(orenhanced-resolve
internally) treat the two files as unrelated. So at build time, two copies oflodash
code will be packaged in the output bundle, even if their content is the same.However, it can be found that in the file system, the two
lodash.js
point exactly to the same inode node. Which means they are the same file in physical storage.To solve the problem, we created an
enhanced-resolve
plugin to cache the request:And it works as expected in
webpack
, the duplicated modules have been eliminated.So I wonder if this could be added to
enhanced-resolve
(just like thesymlinks
options).Related issue: web-infra-dev/rspack#5912
The text was updated successfully, but these errors were encountered: