如何使用browserify包含非节点模块 [英] How to include non node modules using browserify

查看:84
本文介绍了如何使用browserify包含非节点模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用程序中使用依赖关系管理,并遇到了require.js和browserify。我无法决定使用哪一个。

I want to use a dependency management in application and came across require.js and browserify . i am unable to decide which one to go with.

如果任何人可以告诉我如何包含自定义的javascript模块(非节点模块),这将是一个决定性的因素。进入我的js。我看到browserify很容易包含节点模块。

It would be a decicive factor if any one can tell me how i can include custom made javascript modules (non node modules) into my js. I see browserify is including node modules with ease.

推荐答案

假设我们要将以下功能封装到模块中:

Let's say we want to encapsulate following functionality into module:

sayHelloInEnglish = function() {
  return "Hello";
};

然后我们创建文件 greetings.js

module.exports = {
    sayHelloInEnglish: function() {
        return "Hello";
        }
    }; 

然后我们想在另一个模块中使用问候模块,例如。在我们的 main.js 文件中:

Then we want to use greetings module in another module, eg. in our main.js file:

var greetings = require("./greetings.js");
greetings.sayHelloInEnglish();

这就是我们声明依赖关系的方式。

That's how we declare dependencies.

除此之外我们还需要一个构建过程,因此我们的JS代码可以在浏览器中运行。为此,我选择了gulp.js流式构建系统。然后您只需要创建一个这样的任务:

Apart from that we need a building process as well, so our JS code could run inside browser. For that I've chosen gulp.js streaming build system. Then all you need is to create one task like this:

gulp.task('browserify', function () {
    gulp.src('main.js')
        .pipe(browserify())
        .pipe(concat('main.js'))
        .pipe(gulp.dest('dist/js'));
});

此任务将加载main.js的所有依赖项,在main.js主体之前包含它们然后它将作为新文件保存到'dist / js'或您将选择的任何目的地。

This task will load all dependencies of main.js, include them before the main.js body and then it will save altogether as a new file into 'dist/js', or any destination you will choose.

这篇关于如何使用browserify包含非节点模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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