Browserify编译为完整系统路径(使用watchify& gulp)

时间:2014-08-10 01:23:13

标签: javascript gulp browserify system-paths watchify

我来自AMD,似乎已经做错了。

我做了这样的设置:

client/js/index.js (entry point)
client/js/test.js

我希望它能够构建:

build/app.js

index.js需要test.js这样:

var test = require('./test');

我的gulp watchify任务如下所示:

var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var watchify = require('watchify');

// https://github.com/gulpjs/gulp/blob/master/docs/recipes/fast-browserify-builds-with-watchify.md
gulp.task('watch', function () {
    var bundler = watchify(browserify('./client/js/index.js', watchify.args));

    bundler.on('update', rebundle);

    function rebundle () {
        return bundler.bundle()
            // Log errors if they happen.
            .on('error', function(e) {
                gutil.log('Browserify Error', e.message);
            })
            .pipe(source('app.js'))
            .pipe(gulp.dest('./build'));
    }

    return rebundle();
});

编译后的代码看起来很错误,对于test.js我看到绝对的本地路径对于任何消费代码的人来说肯定是破坏或冗余的?

P.S。我正在运行没有args的任务(只有gulp watch

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"./client/js/index.js":[function(require,module,exports){
var test = require('./test');

var ab = function(a, b2) {
    return a + b2;
};

module.exports = ab;
},{"./test":"/Users/dtobias/Sites/browserify-test/client/js/test.js"}],"/Users/dtobias/Sites/browserify-test/client/js/test.js":[function(require,module,exports){
var helloworld = function () {
    console.log('hello world');
};

module.exports = helloworld;
},{}]},{},["./client/js/index.js"]);

1 个答案:

答案 0 :(得分:5)

watchify用于观看更改文件并自动更新文件,它不适合部署,您看到的路径是在此行watchify.args上使用watchify(browserify('./client/js/index.js', watchify.args));的结果传递给browserify的参数声明fullPaths: true,这是watchify如何在每次更改文件时加快构建过程的一部分。我建议做的是将watchify任务专门用于观看和浏览构建过程。

这可以通过设置一些开关并在监视任务中将其设置为true(从而修改代码)来轻松实现。

这样的事情:

var gulp = require('gulp');
var gutil = require('gulp-util');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var watchify = require('watchify');

// https://github.com/gulpjs/gulp/blob/master/docs/recipes/fast-browserify-builds-with-watchify.md
gulp.task('build', function(){
    browserifyfun(false);
});
gulp.task('watch', function () {
    browserifyfun(true);
});
function browserifyfun(watch){
    var b;

    if(watch){
        b = watchify(browserify('./client/js/index.js', watchify.args));
        b.on('update', rebundle(b));
    }else{
        b = browserify('./client/js/index.js');
    }

    function rebundle (bundler) {
        return bundler.bundle()
            // Log errors if they happen.
            .on('error', function(e) {
                gutil.log('Browserify Error', e.message);
            })
            .pipe(source('app.js'))
            .pipe(gulp.dest('./build'));
    }

    return rebundle(b);
}

Modified code from here