简体   繁体   English

Browserify将相对路径分配给模块,而不是绝对路径

[英]Browserify assign relative paths to modules instead of absolute paths

Given this file tree: 给定此文件树:

src
  - main.js
  ui
    - foo.js
    - bar.js

This command generates modules where identifiers have been set to absolute module paths: 此命令生成已将标识符设置为绝对模块路径的模块:

browserify ./src/**/*.js -o ./dist/bundle.js --full-paths

Resulting module id: 结果模块ID:

"/Users/rikschennink/.../src/ui/foo.js"

Is there a way to make those module paths relative? 有没有办法使这些模块路径相对? If I omit the --full-paths command I'm left with indexes as module identifiers so that's not working. 如果省略--full-paths命令, --full-paths索引作为模块标识符,因此无法正常工作。

I want to bundle all modules but still be able to reference them with require by path like this. 我想捆绑所有模块,但仍然能够像这样按需求路径引用它们。

var module = require('/ui/foo');

I'm aware I can alias the modules but that would require aliasing every single module manually which is not really a workable solution. 我知道我可以给模块起别名,但这需要手动为每个模块起别名,这实际上不是可行的解决方案。


I went with a JavaScript function that replaces absolute paths in the output stream. 我使用了JavaScript函数来替换输出流中的绝对路径。 You can find it here: https://github.com/rikschennink/conditioner-boilerplate-browserify/blob/master/browserify.js 您可以在这里找到它: https : //github.com/rikschennink/conditioner-boilerplate-browserify/blob/master/browserify.js

I would simply use sed to fix the stuff for me 我只会用sed为我修复东西

browserify -r ./src/ui/bar -r ./src/main ./src/**/*.js --full-paths | sed "s|$PWD/src||g" > ./dist/bundle.js

Then last line generated is 然后生成的最后一行是

},{}]},{},["/main.js","/ui/bar.js","/ui/ui.js"]);

Just as you wanted 就像你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM