Browserify - 在 package.json 中对在 npm3 中不起作用的符号链接模块进行转换

Browserify - transform in package.json on symlinked modules not working in npm3

环境: node@5.70 npm@3.75 browserify@13.0.0 babelify@7.2.0

正在尝试构建适用于 npm@2babelify@6 的应用程序,但在升级到 npm@3babelify@7 时遇到问题。请允许我尝试解释这个问题。

}, "devDependencies": { "babel-preset-es2015": "^6.5.0", "babelify": "^7.2.0" }, "browserify": { "transform": [ [ "babelify", { "presets": [ "es2015" ] } ] ] }

尝试构建时出现错误:

"Browserify Error: Couldn't find preset "es2015" relative to directory"

npm 3 已经展平了依赖树,所以 babel-preset-es2015 符号链接模块需要在 appnode_modules 中。根据@substack here and this example here 的解释,browserify 应该从符号链接模块向上走树到 app 并在那里检查 node_modules,但它似乎并没有这样做。

我的目录布局如下所示:

~/projects |-- app |-- entry.js (this file can see babel-preset just fine) |-- node_modules |-- babel-preset-es2015 |-- my-module (symlink pointing at ~/projects/modules-shared/my-module) |-- index.js (we want babelify to transform this file) |-- modules-shared |-- my-module

如果我在 modules-shared 文件夹中安装 babel-preset-es2015,browserify 会找到预设。

回到这个我有点忽略的问题。这个问题很难解决,因为它与所用工具的两个不同特性有关。

首先,Browserify 没有按应有的方式处理符号链接。它不是将符号链接的 npm 模块视为处于其符号链接位置,而是占据其在文件系统中的实际位置,因此无法在 npm3 的平面文件树下找到其依赖项。

其次,Babel 要求它的依赖项在相关模块的 node_modules 中,实际上是对 npm2 嵌套文件树系统的硬依赖。

所以出于同样的原因我们有两个不同的问题 - npm3 改变了 node_modules 的组织方式。

我能找到的最好的解决方法是编写一个自定义脚本,只安装任何符号链接模块的 babel 依赖项。您可能认为只安装符号链接模块的所有依赖项是个好主意,但这会导致 Browserified 包中出现重复实例,从而导致各种微妙的、难以理解的错误。

Browserify 问题可能可以通过此转换解决 realpathify,但至于 Babel 的问题,我看不到任何解决方案的进展。