nodejs / NG

Next Generation JavaScript IO Platform
103 stars 12 forks source link

More customizable module resolution #27

Open tblaisot opened 8 years ago

tblaisot commented 8 years ago

Posted on nodejs/node#4584

I am coming from java and maven world and after using npm and node in multi-module project, i think a lot of improvement could be done with module resolution allowing better work with local modules: Could node have a parameter (let suppose _--moduleresolution is this command line option ) to be able to provided a file with mapping between module name and path into the filesystem (like the NODE_PATH variable) ?

This file could be a map like

{
    "moduleA" : {
        "path" : "/path/to/moduleA/version/1.0.0",
        "resolve" :  {
            "moduleA" : {
                "path" : "/path/to/moduleB/version/2.0.0",
                "resolve" : { ... an so on ... }
            }
        }
    },
    "moduleB" : {
        "path" : "/path/to/moduleB/version/1.0.0",
        "resolve" : { ... an so on ... }
    }

}

//This map will look a lot like npm-shrinkwrap.json file but with local path instead of url
//I will suppose this file is named module-resolution.json

so running

node /path/to/moduleMain/index.js --module_resolution=/path/to/moduleMain/module-resolution.json

will have this effect:

Avantages:

This file could be generated by modules managers like npm or bower. Differents modules managers can handle multiple versions differently easily (if a module manager want to only allow one version of a module to be used, it just have to modify this map)

This file could be agnostic of the resolution of the js file from the module name so externals tools could use this file for require resolution: algorithme like looking for main field in /resolved/path/from/the/map/package.json for node or webpack or looking for typing field in /resolved/path/from/the/map/package.json for typescript compiler can be let to the executor discretion.

hediet commented 7 years ago

I'd suggest to even allow dependency graphs rather than just dependency trees, so that module instances can be shared. I hacked together a proof of concept that hijacks nodejs' module resolution algorithm: https://github.com/hediet/HowNodeJsModulesShouldHaveBeenDesigned