0

我是 Browserify 的初学者。我尝试使用 Watchify(出于性能原因)和 Coffeeify 将它集成到 gulp.js 中。我尝试了至少五种我在 Google 中找到的或多或少不同的方法,其中最后一种是来自 gulp 文档的官方配方。

现在的问题是转换部分。我想使用 Coffeeify 来解析我的代码,该代码主要是用 CoffeeScript 编写的。

Coffeeify 成功解析了我的入口文件app.coffee,但是当我./foo.coffee从那里需要一个时,该文件似乎没有被转换,这自然会导致 Browserify 关于意外令牌等的解析错误。

有人知道如何解决这个问题吗?

这是我的 gulpfile 的相关部分,与上面的链接基本相同,只是添加了转换。

var gulp = require( 'gulp' );
var gutil = require( 'gulp-util' );

var browserify = require( 'browserify' );
var watchify = require( 'watchify' );
var coffeeify = require( 'coffeeify' );
var source = require( 'vinyl-source-stream' );


var b = watchify( browserify({
    entries: [ './coffee/app.coffee' ],
    extensions: [ '.coffee' ],
    debug: true,
    cache: false,
    packageCache: false
}) );
b.transform( coffeeify ); // as described in the gulp docs

gulp.task( 'bundle', bundle );
b.on( 'update', bundle );
b.on( 'log', gutil.log );

function bundle() {
    return b.bundle()
        .on( 'error', gutil.log.bind( gutil, 'Browserify Error' ) )
        .pipe( source( 'bundle.js' ) )
        // I actually tried to pipe in coffeeify here (gulp-coffeeify as well), didn't help
        .pipe( gulp.dest( './js' ) );
};
4

1 回答 1

0

好吧,这完全是我的错。我为这篇文章抽象了我的代码有点太远了,所以实际问题没有在这里反映出来: 实际上并不app.coffee直接需要,而是需要从目录中获取,只是为了让事情变得更容易。这正是问题所在:转换将不适用于所需的。这是预期的行为./foo.coffeefoo/foonode_modules

似乎有两种解决方法:

解决方案 1:一个名为global-transform. 在 API 中,它像b.transform( coffeeify, { global: true } ). 这会将 coffeeify 转换应用于每个必需的模块。

解决方案 2:按包定义转换。这意味着:在每个模块的 package.json 中添加这样的内容:

{
    "browserify": {
        "transform": ["coffeeify"]
    }
}

我决定使用第一个,因为我的“包”实际上甚至没有 package.json 文件,它们只是在 node_modules 目录中,可以通过 browserify 轻松访问。

于 2015-05-16T17:12:48.333 回答