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

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

问题描述

我无法弄清楚 browserify 的方式basedir 选项 有效..

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

注意:我对整个 basedir 概念的理解可能是错误的,因为我来自 RequireJS(想想 baseUrl).

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

编辑确实我错了,但是您仍然可以使用 paths 选项实现我想要做的事情,请参阅下面的答案.

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

我了解 basedir 选项使您能够(自由!)从静态根/基本目录指定所有需要的路径(以 . 开头).. 而不是来自 process.cwd()..

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()..

这简直把我逼疯了,我虽然这样的功能实现起来非常基础,而且很多人都会遇到和我一样的问题,但实际上网络上很少有关于如何实现的信息正确设置 basedir 选项..相信我,这不是直截了当..

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..

下面是让我抓狂的 BASIC 示例.

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

给定以下文件结构:

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

并运行:

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

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

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

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

我希望能够在 PersonView.js 中执行以下 require() 调用:

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');
...

但我收到以下错误:

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

basedir 选项我缺少什么?

推荐答案

原来basedir和RequireJS的baseUrl不一样.正如@Ben 在上面的评论中所说,官方文档说:

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 是browserify 开始捆绑的目录from 用于以 ..

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

(来源)

意思是basedir只适用于入口文件.文件树结构深处的进一步 require 调用将始终相对于当前正在解析的文件进行解析.

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.

browser-resolvepaths 选项(由 browserify 在后台使用)是我正在寻找的:

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

paths - 如果在正常上找不到任何东西,则使用 require.paths 数组node_modules 递归遍历

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

(来源)

只需将此选项与其他 browserify 选项一起传递 实例化捆绑器时.

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

注意:看起来是 搞砸了a> 与 browserify-shim transform 一起使用时.

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

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

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