-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
85 lines (76 loc) · 2.5 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const sass = require('gulp-sass'); // scss to css
const dartSass = require('sass'); // scss to css
var postcss = require('gulp-postcss'); // js in css
const autoprefixer = require('autoprefixer'); //自动完成 css 属性浏览器前缀
const fileinclude = require('gulp-file-include') // html中使用include
const cssnano = require('cssnano'); //css压缩
const webpack = require('webpack-stream'); //在这里用来处理js的打包
const scss = sass(dartSass);
const gulpMode = process.env.NODE_ENV;
//files path
const basePath = {
src: './src/',
dest: './dist/'
};
const srcAssets = {
pages: basePath.src + 'pages/',
styles: basePath.src + 'scss/',
scripts: basePath.src + 'js/',
images: basePath.src + 'img/'
};
const destAssets = {
pages: basePath.dest + 'pages/',
styles: basePath.dest + 'css/',
scripts: basePath.dest + 'js/',
images: basePath.dest + 'img/'
};
// Copy page templates into finished HTML files
gulp.task('pages', () => {
return gulp.src(srcAssets.pages + '**/*.{html,hbs,handlebars}')
.pipe(fileinclude())
.pipe(gulp.dest(destAssets.pages))
.pipe(browserSync.stream());
});
// Combine JavaScript into one file
// In production, the file is minified
gulp.task('javascript', () => {
return gulp.src(srcAssets.scripts + 'app.js', { allowEmpty: true })
.pipe(webpack({
mode: 'development',
output: {
filename: 'app.js',
},
}))
.pipe(gulp.dest(destAssets.scripts))
.pipe(browserSync.stream());
});
//sass to css and copy to dist path
gulp.task('css', () => {
const plugins = [
autoprefixer(),
((gulpMode==='production') ? cssnano : ''),
].filter(Boolean);
return gulp.src(srcAssets.styles + 'app.scss')
.pipe(scss())
.pipe(postcss(plugins))
.pipe(gulp.dest(destAssets.styles))
.pipe(browserSync.stream());
});
gulp.task('start', gulp.series('css', 'javascript', 'pages', function () {
browserSync.init({
server: {
baseDir: basePath.dest,
index: "./pages/index.html" //相对于baseDir的index路径
},
port: 3000
});
gulp.watch(srcAssets.styles + '*.scss', gulp.series('css'));
gulp.watch(srcAssets.scripts + '*.js', gulp.series('javascript'));
gulp.watch(srcAssets.pages + '*.html', gulp.series('pages'));
// gulp.watch("./src/pages/*.html").on('all', browserSync.reload);
}));
gulp.task('build', gulp.series('css', 'javascript', 'pages', function(){
}));
gulp.task('default', gulp.series('start'));