2015-10-13 90 views
7

我有一個React組件導出到一個包文件。我已經成功地使用babelify進行了轉換,但是,現在我想運行envify。我似乎無法弄清楚如何使用browserify運行多個轉換。我認爲這一定是可能的,但我不知道我的語法稍微偏離了,或者我需要編寫自定義轉換,或者我應該在我的package.json中指定轉換。這裏是我的吞嚥文件中的代碼:在gulp/browserify包上運行多個轉換

var bundleComponent = function(bundler, source, component) { 
    //bundler is just browserify('./client/components/TVScheduleTab/render.js') 

    gutil.log('Bundling ' + component) 

    return bundler 
    //this throws an error 
    .transform(babelify, envify({ 
     NODE_ENV: 'production' 
    })) 
    .bundle() 
    .on('error', function(e){ 
     gutil.log(e); 
    }) 
    .pipe(source) 
    .pipe(gulp.dest('output/')); 
}; 

回答

1

你試過鏈嗎? 正確的解決辦法是在評論

var bundleComponent = function(bundler, source, component) { 
    //bundler is just browserify('./client/components/TVScheduleTab/render.js') 

    gutil.log('Bundling ' + component) 

    return bundler 
    //this throws an error 
    .transform(babelify) 
    .transform(envify({ 
     NODE_ENV: 'production' 
    })) 
    .bundle() 
    .on('error', function(e){ 
     gutil.log(e); 
    }) 
    .pipe(source) 
    .pipe(gulp.dest('output/')); 
}; 
+0

是的,我也嘗試添加在將它們作爲一個數組,也是一個對象我傳遞到browserify指定它們。我也有''瀏覽':{ 「transform」:[ 「browserify-shim」 ] },'在我的package.json中。不知道這是否以任何方式干擾。 – SYU88

+0

你讀過這個問題 - https://github.com/hughsk/envify/issues/27? – ahz

+0

此外,這似乎很重要,但它並不明顯,你要求envify - https://github.com/hughsk/envify/issues/7#issuecomment-45418761 – ahz

1

儘管公認的答案後出現這個答案,並一定程度上接受了覆蓋的問題,我想講清楚,而不需要通過鏈接GitHub的問題進行導航。

鏈接,特別是與envify,應該是這樣的:

// NOTE: the "custom" part 
var envify = require('envify/custom'); 

gulp.task('build-production', function() { 
    browserify(browserifyOptions) 
     .transform(babelify.configure(babelifyOptions)) 
     .transform(envify({ 
      NODE_ENV: 'production' 
     })) 
     .bundle() 
     .on('error', handleErrors) 
     .pipe(source('app.js')) 
     .pipe(buffer()) 
     .pipe(uglify({ mangle: false })) 
     .pipe(gulp.dest('./build/production/js')); 
});