本文介绍了Browserify basedir选项(类似于RequireJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚有效。



:我的理解可能有误因为我来自RequireJS(想想 baseUrl )。$ b $ basedir b

编辑
确实我错了,但您仍然可以实现我正在尝试使用路径选项为你提供了这个能力(自由度) !)从静态根目录/基目录中指定所有需要路径(以。开头)。而不是从 process.cwd() ..



这实际上让我发疯,我虽然这样的功能实现起来非常基础,很多人也会跑与我相同的问题,但实际上很少有关于w的信息eb关于如何正确设置 basedir 选项。并且相信我,这不是直截了当的。





给出以下文件结构:

  js / 
js / app.js
js / src / models / Person.js
js / src / views / PersonView.js
code>

运行:

  var browserify = require('browserify'); 
var gulp = require('gulp');
$ b $ gulp.task('scripts',function(){

var b = browserify('./ app',{basedir:'./js'});

b.bundle()。pipe(gulp.dest('./ dist'));
});

我期望能够执行以下操作 require() $ PersonView.js :

  var Person =要求( './ SRC /模型/人'); 
...

而不是(显然正在工作......): p>

  var Person = require('../ models / Person'); 
...

但我得到以下错误:

 错误:找不到./src/models/Person/Users/...some path ... / js / src / views /PersonView.js

我错过了 basedir option?

解决方案

原来, basedir 是与RequireJS的 baseUrl 不一样。正如@Ben在上面评论中所述,官方文件说:



含义 basedir 仅适用于。进一步 require 在文件树结构中的深度调用将总是相对于当前正在解析的文件解析。



我的问题



路径选项 browser-resolve (它被 browserify 使用)是我正在寻找的:

(source)

Meaning that basedir only applies to the entry files. Further require calls deep in the file tree structure will always be resolved relatively to the file currently being parsed.

Answer to my question

The paths option of browser-resolve (which is used by browserify under the hood) is what I was looking for:

(source)

Just pass this option along with other browserify options when instantiating the bundler.

Note: It looks like it is messing up things when using together with browserify-shim transform.

这篇关于Browserify basedir选项(类似于RequireJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 22:48