2016-11-13 30 views
1

每当改变function.js,我必须手动运行下面的命令用来browserify function.jsbundle.js自动化每当JS文件被改变

$ browserify function.js --standalone function > bundle.js 

如何这一步骤是自动的,使得指令,当运行此browserify命令自动在后台每当function.js被修改?

我在Windows 10使用Node.js的V6.9与Webstorm 2016.2

+1

看一看这个答案的大口咕噜部分:?什么是故宫,凉亭,一饮而尽,约曼和咕噜好(http://stackoverflow.com/a/36789515/3993662 )。除此之外,你可以在webstorm中创建一个文件观察器。只需在搜索所有对话框中键入观察者 – baao

+0

https://confluence.jetbrains.com/display/PhpStorm/File+Watchers+in+PhpStorm – LazyOne

回答

1

要运行的是命令browserify;

$ browserify function.js --standalone function > bundle.js 

以从https://github.com/gulpjs/gulp/blob/master/docs/recipes/fast-browserify-builds-with-watchify.md

参考下面是你需要的全部代码。只需稍微修改参考代码就可以选择Browserify。

'use strict'; 

var watchify = require('watchify'); 
var browserify = require('browserify'); 
var gulp = require('gulp'); 
var source = require('vinyl-source-stream'); 
var buffer = require('vinyl-buffer'); 
var gutil = require('gulp-util'); 
var sourcemaps = require('gulp-sourcemaps'); 
var assign = require('lodash.assign'); 

// add custom browserify options here 
var customOpts = { 
    entries: ['./function.js'], 
    standalone: 'function', 
}; 
var opts = assign({}, watchify.args, customOpts); 
var b = watchify(browserify(opts)); 

// add transformations here 
// i.e. b.transform(coffeeify); 

gulp.task('js', bundle); // so you can run `gulp js` to build the file 
b.on('update', bundle); // on any dep update, runs the bundler 
b.on('log', gutil.log); // output build logs to terminal 

function bundle() { 
    return b.bundle() 
    // log errors if they happen 
    .on('error', gutil.log.bind(gutil, 'Browserify Error')) 
    .pipe(source('bundle.js')) 
    // optional, remove if you don't need to buffer file contents 
    .pipe(buffer()) 
    // optional, remove if you dont want sourcemaps 
    .pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file 
     // Add transformation tasks to the pipeline here. 
    .pipe(sourcemaps.write('./')) // writes .map file 
    .pipe(gulp.dest('./dist')); 
} 
+0

非常感谢! – lwdthe1