Browserify basedir选项(类似于RequireJS) [英] Browserify basedir option (RequireJS-like)

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

问题描述

我无法弄清楚 browserify basedir 选项有效。



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

编辑
确实我错了,但您仍然可以实现我正在尝试使用路径 basedir 选项为你提供了这个能力(自由度) !)从静态根目录/基目录中指定所有需要路径(以开头)。而不是从 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在上面评论中所述,官方文件说:


opts.basedir 是browserify 开始将绑定为以开头的文件夹的目录。


(来源)



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



我的问题



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


paths - 在普通
上找不到任何内容的require.paths数组。 href =https://github.com/defunctzombie/node-browser-resolve>(来源)



只需将此选项与其他 browserify 选项实例化捆绑器



注意:它看起来像 href =https://github.com/thlorenz/browserify-shim> browserify-shim 转换


I cannot figure out how the browserify basedir option works..

Note: I may be wrong in my understanding of the whole basedir concept because I'm coming from RequireJS (think baseUrl).

EDIT Indeed I was wrong, but you can still achieve what I was trying to do with the paths option, see my answer below.

I understand that the basedir option gives you the ability (the freedom!) to specify all require paths (starting with a .) from a static root/base dir.. And NOT from process.cwd()..

This is literally driving me crazy, I though such a feature would be so basic to implement and that a lot of people would have run into the same issue as me, but there is actually very few info on the web about how to setup properly the basedir option.. And trust me, this is not straight-forward..

So here is the BASIC example that's driving me crazy.

Given the following file structure:

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

And running:

var browserify = require('browserify');
var gulp = require('gulp');

gulp.task('scripts', function() {

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

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

I would expect being able to do the following require() call in PersonView.js:

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

Instead of (which is obviously working...):

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

But I get the following error:

Error: module "./src/models/Person" not found from "/Users/...some path.../js/src/views/PersonView.js"

What am I missing about the basedir option?

解决方案

It turns out that basedir is not the same as RequireJS's baseUrl. As stated by @Ben in the comment above, the official docs says:

opts.basedir is the directory that browserify starts bundling from for filenames that start with ..

(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:

paths - require.paths array to use if nothing is found on the normal node_modules recursive walk

(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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆