新的回答: // 评论区放不下
非常抱歉我没见过你这种写法,这是我对ionic 1.0
版本进行的增强做法,你可以参考:
var del = require('del');
var gulp = require('gulp');
var sass = require('gulp-sass');
var watch = require('gulp-watch');
var gutil = require('gulp-util');
var babel = require('gulp-babel');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var assign = require('lodash.assign');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var gulpIf = require('gulp-if');
var uglify = require('gulp-uglify');
var plumber = require('gulp-plumber');
var options = require('./config/gulp.config');
var htmlmin = require('gulp-htmlmin');
var changed = require('gulp-changed');
var minimist = require('minimist');
var watchify = require('watchify');
var browserify = require('browserify');
var sourcemaps = require('gulp-sourcemaps');
var browserSync = require('browser-sync');
var autoprefixer = require('gulp-autoprefixer');
var reload = browserSync.reload;
var argv = minimist(process.argv.slice(2));
// concat <> browserify
var mode = argv.mode;
var debug = argv.debug;
var browserifyOptions = assign({}, watchify.args, {entries: [options.scripts.main] ,debug: argv.debug});
var brify = watchify(browserify(browserifyOptions));
brify.on('update', scriptsBundle);
brify.on('log', gutil.log);
gulp.task('scripts_browserify', scriptsBundle);
function scriptsBundle(){
return brify.bundle()
.on('error', gutil.log.bind(gutil, 'Browserify Error'))
.pipe(source(options.scripts.outputFile))
.pipe(buffer())
.pipe(gulpIf(argv.debug ,sourcemaps.init({loadMaps: true})))
.pipe(gulpIf(!argv.debug ,uglify()))
// .pipe(gulpIf(!argv.debug ,rename({extname: ".min.js"})))
.pipe(gulpIf(argv.debug ,sourcemaps.write('./')))
.pipe(gulp.dest(options.scripts.dist))
.pipe(gulpIf(argv.debug ,reload({stream: true})));
}
gulp.task('scripts_concat' ,() => {
return gulp.src(options.scripts.src)
.pipe(plumber())
.pipe(gulpIf(argv.debug ,sourcemaps.init()))
.pipe(babel({presets: ['es2015']}))
.pipe(concat(options.scripts.outputFile))
.pipe(gulpIf(!argv.debug ,uglify()))
// .pipe(gulpIf(!argv.debug ,rename({suffix: ".min"})))
.pipe(gulpIf(argv.debug ,sourcemaps.write('./')))
.pipe(gulp.dest(options.scripts.dist))
.pipe(reload({stream: true}));
});
gulp.task('templates' ,() => {
return gulp.src(options.templates.src)
.pipe(changed(options.templates.dist))
.pipe(gulpIf(!argv.debug ,htmlmin()))
.pipe(gulp.dest(options.templates.dist))
.pipe(reload({stream: true}));
});
gulp.task('styles' ,['styles:fonts'] ,() => {
return gulp.src(options.styles.main)
.pipe(changed(options.styles.dist))
.pipe(gulpIf(argv.debug ,sourcemaps.init()))
.pipe(sass({outputStyle: argv.debug?'expanded':'compressed'}).on('error' ,sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions', 'Android >= 4.0'] ,
cascade: argv.debug ,
remove:true
}))
.pipe(rename(options.styles.outputFile))
// .pipe(gulpIf(!argv.debug ,rename(path => path.basename += '.min')))
.pipe(gulpIf(argv.debug ,sourcemaps.write('./')))
.pipe(gulp.dest(options.styles.dist))
.pipe(reload({stream: true}));
});
gulp.task('styles:fonts' ,() => {
return gulp.src(options.styles.fonts.src)
.pipe(changed(options.styles.fonts.dist))
.pipe(gulp.dest(options.styles.fonts.dist))
.pipe(reload({stream: true}));
});
gulp.task('resources' ,() => {
return gulp.src(options.resources.src)
.pipe(changed(options.resources.dist))
.pipe(gulp.dest(options.resources.dist))
.pipe(reload({stream: true}));
});
gulp.task('images' ,() => {
return gulp.src(options.images.src)
.pipe(changed(options.images.dist))
.pipe(gulp.dest(options.images.dist))
.pipe(reload({stream: true}));
});
gulp.task('server' ,['templates' ,'styles' ,`scripts_${mode}` ,'images' ,'resources'] ,() => {
browserSync.init(options.server);
gulp.start('watch');
});
gulp.task('build' ,['templates' ,'styles' ,`scripts_${mode}` ,'images' ,'resources'] ,() => {});
gulp.task('watch' ,() => {
watch(options.styles.src , () => { gulp.start('styles'); });
watch(options.styles.fonts.src , () => { gulp.start('styles:fonts'); });
watch(options.scripts.src ,() => { gulp.start(`scripts_${mode}`); });
watch(options.templates.src ,() => { gulp.start('templates'); });
watch(options.resources.src ,() => { gulp.start('resources'); });
watch(options.images.src ,() => { gulp.start('images'); });
watch(['./www/index.html' ,'./www/js/logic.js'] ,() => {
reload();
});
});
gulp.task('clean' ,() => {
return del(options.clean);
});
gulp.task('default' ,['clean'] ,() => {
gulp.start(argv.debug?'server':'build' ,() =>{
if(!argv.debug) process.exit(0);
});
});
没见过这种用法,你看看官方文档的用法 http://www.browsersync.cn/doc...
// 使用变量引用 `reload` 方法
var reload = browserSync.reload;
// 编译 SASS & 自动注入到浏览器
gulp.task('sass', function () {
return gulp.src('scss/styles.scss')
.pipe(sass({includePaths: ['scss']}))
.pipe(gulp.dest('css'))
.pipe(reload({stream:true}));
});
// 监听scss和html文件, 当文件发生变化后做些什么!
gulp.task('serve', ['sass'], function () {
// 从这个项目的根目录启动服务器
browserSync({
server: {
baseDir: "./"
}
});
gulp.watch("scss/*.scss", ['sass']);
gulp.watch("*.html").on("change", browserSync.reload);
});