利用gulp 打包压缩代码

var gulp = require('gulp'),
    uglify = require("gulp-uglify");
    webpack = require('gulp-webpack'),


var path = require("path");
var pc = {
  entry: './src/index',
  output: {
    path: path.join(__dirname, 'scripts'),
    filename: "app.js",
    publicPath: '/scripts/'
  },
  module: {
      loaders: [{
        test: /\.jsx?$/,
        loaders: ['react-hot', 'babel'],
        include: path.join(__dirname, 'src')
      }]
  }
};

gulp.task("webpack", function(callback) {
  var myConfig = Object.create(pc);
  // run webpack
  webpack(
    // configuration
    myConfig
  , function(err, stats) {
    // if(err) throw new gutil.PluginError("webpack", err);
    // gutil.log("[webpack]", stats.toString({
    //   // output options
    // }));
    callback();
  });
});
gulp.task('minify', function () {
    gulp.src('scripts/app.js')
    .pipe(uglify()) // 要压缩的js文件
    .pipe(gulp.dest('scripts')); //压缩后的路径
});

gulp.task('default', ['webpack', 'minify']);

在项目目录直接 运行 gulp 即可(前提你的装了gulp)

你可能感兴趣的:(利用gulp 打包压缩代码)