bloody-ux / module-decompose-loader

a webpack loader to enhance module importing and reduce the bundle size
MIT License
4 stars 1 forks source link

feature request: better detection of component path #4

Open trusktr opened 2 years ago

trusktr commented 2 years ago

Here for example is a Three.js component that imports everything from an index, importing the whole Three.js lib:

https://github.com/mrdoob/three.js/blob/f9331aa74f87b12e91f34886e0ed54b8d79b31bf/examples/jsm/controls/OrbitControls.js#L1-L9

When I try to set the loader config,

                    options: {
                        modules: {
                            three: {
                                // nothing, but need this empty object literal
                            },
                        },
                    },

the build has an error:

Module not found: Error: Package path ./LinearMipmapLinearFilter is not exported from package /home/trusktr/src/lume+lume/node_modules/three (see exports field in /home/trusktr/src/lume+lume/node_modules/three/package.json)

Besides that Webpack is trying to obey exports field, it is also trying to import the wrong path. three/LinearMipmapLinearFilter is not the file where that thing comes from; it comes from three/src/constants.

I wonder if there's a way to make it correctly detect the path of items.

trusktr commented 2 years ago

I see! this works a little better, but not quite:

                    options: {
                        modules: {
                            three: {
                                components: 'src',
                            },
                        },
                    },
trusktr commented 2 years ago

I took a look! I see it is using TS for the AST. Maybe instead we can tell TS to resolve the original file location of given item, to get the path that way.

trusktr commented 2 years ago

Hmmm, check out this getSourceFile method. Maybe we can use that.

https://stackoverflow.com/questions/58653091/typescript-compiler-api-find-which-file-a-type-came-from