/
gulpfile.js
76 lines (66 loc) · 1.95 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
var gulp = require('gulp'),
foreach = require('gulp-foreach'),
postCSS = require('gulp-postcss'),
postHTML = require('gulp-posthtml'),
named = require('vinyl-named'),
rename = require('gulp-rename'),
webpack = require('webpack-stream'),
webpackConfig = require('./webpack.config');
var config = {
css: {
src: './src/**/*.css',
dest: './public'
},
js: {
src: './src/**/*.js',
dest: './public'
},
html: {
src: './src/**/*.html',
dest: './public'
}
}
function basename(str, sep) {
return str.substr(str.lastIndexOf(sep) + 1);
}
function repath(target, removal, pathOnly) {
return target.replace(removal, '')
.replace(pathOnly ? /\.[^/.]+$/ : '', '')
.replace(new RegExp('\\' + path.sep, 'g'), '/');
}
gulp.task('css', function() {
return gulp.src(config.css.src)
.pipe(postCSS([
require('postcss-modules'),
require('cssnano')
]))
.pipe(gulp.dest(config.css.dest));
});
gulp.task('html', ['css'], function() {
return gulp.src(config.html.src)
.pipe(foreach(function(stream, file){
var currentDir = file.base,
fileName = basename(file.history[0], '/').replace('.html', '.css.json')
return stream
.pipe(postHTML([
require('posthtml-css-modules')([`${currentDir}/${fileName}`, `${currentDir}/styles.css`])
]))
}))
.pipe(gulp.dest(config.html.dest));
});
gulp.task('js', ['css'], function() {
var tmp = {};
return gulp.src(config.js.src)
.pipe(named())
.pipe(rename(function (path) {
tmp[path.basename] = path;
}))
.pipe(webpack(webpackConfig))
.pipe(rename(function (path) {
var basename = path.basename.replace('.min', '')
path.dirname = tmp[basename].dirname;
}))
.pipe(gulp.dest(config.js.dest))
});
gulp.task('build', ['default']);
gulp.task('default', ['css', 'js', 'html']);