【问题标题】:Browserify compiling to full system paths (using watchify & gulp)Browserify 编译成完整的系统路径(使用 watchify & gulp)
【发布时间】:2014-10-03 04:23:45
【问题描述】:

我来自 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,我看到绝对本地路径对于使用该代码的任何人来说肯定是损坏或冗余?

附:我正在运行没有参数的任务(只是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"]);

【问题讨论】:

    标签: javascript gulp browserify system-paths watchify


    【解决方案1】:

    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

    【讨论】:

    • 谢谢,我最终选择了 fullPath 选项,但没有意识到这是优化的一个功能,可以解决问题
    猜你喜欢
    • 1970-01-01
    • 2016-02-22
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多