1 | 'use strict';
|
2 |
|
3 | var extend = require('extend'),
|
4 | fs = require('fs-extra'),
|
5 | path = require('path'),
|
6 | glob = require('glob'),
|
7 | sourcemaps = require('gulp-sourcemaps'),
|
8 | less = require('gulp-less'),
|
9 | minifyCSS = require('gulp-minify-css'),
|
10 | uglify = require('gulp-uglify'),
|
11 | rename = require('gulp-rename');
|
12 |
|
13 | var Plumes = function(gulp, config) {
|
14 |
|
15 | config = extend(true, {
|
16 | path: {
|
17 | less: './features/**/css/*.less',
|
18 | js: './features/**/js/*.js',
|
19 | html: './features/**/html/*.html',
|
20 | resources: './features/**/resources',
|
21 | public: './public'
|
22 | },
|
23 | default: null,
|
24 | watchs: [],
|
25 | watcher: true
|
26 | }, config);
|
27 |
|
28 | function _publicByFeature(filePath) {
|
29 | filePath.dirname = filePath.dirname.split(path.sep)[0];
|
30 | }
|
31 |
|
32 | config.default = config.default || [];
|
33 |
|
34 | var defaultTask = ['less', 'minify', 'html', 'resources'].concat(config.default);
|
35 | if (config.watcher) {
|
36 | defaultTask.push('watch');
|
37 | }
|
38 |
|
39 | gulp.task('default', defaultTask);
|
40 |
|
41 | fs.removeSync(config.path.public);
|
42 |
|
43 | gulp.task('less', function(done) {
|
44 | gulp.src(config.path.less)
|
45 | .pipe(less())
|
46 | .pipe(rename(_publicByFeature))
|
47 | .pipe(gulp.dest(config.path.public))
|
48 | .pipe(minifyCSS({
|
49 | keepSpecialComments: 0
|
50 | }))
|
51 | .pipe(rename({
|
52 | extname: '.min.css'
|
53 | }))
|
54 | .pipe(gulp.dest(config.path.public))
|
55 | .on('end', done);
|
56 | });
|
57 |
|
58 | gulp.task('minify', function(done) {
|
59 | gulp.src(config.path.js)
|
60 | .pipe(sourcemaps.init())
|
61 | .pipe(rename(_publicByFeature))
|
62 | .pipe(gulp.dest(config.path.public))
|
63 | .pipe(uglify())
|
64 | .pipe(rename({
|
65 | extname: '.min.js'
|
66 | }))
|
67 | .pipe(sourcemaps.write('./'))
|
68 | .pipe(gulp.dest(config.path.public))
|
69 | .on('end', done);
|
70 | });
|
71 |
|
72 | gulp.task('html', function(done) {
|
73 | gulp.src(config.path.html)
|
74 | .pipe(rename(_publicByFeature))
|
75 | .pipe(gulp.dest(config.path.public))
|
76 | .on('end', done);
|
77 | });
|
78 |
|
79 | gulp.task('resources', function(done) {
|
80 |
|
81 | if (config.path.resources.indexOf('**') < 0) {
|
82 | return done();
|
83 | }
|
84 |
|
85 | var featureIndex = config.path.resources.split('**')[0].split('/').length - 1;
|
86 |
|
87 | glob.sync(config.path.resources).forEach(function(directory) {
|
88 | var featureName = directory.split('/')[featureIndex];
|
89 |
|
90 | fs.copySync(directory, path.resolve(config.path.public, featureName));
|
91 | });
|
92 |
|
93 | done();
|
94 | });
|
95 |
|
96 | gulp.task('watch', function() {
|
97 | gulp.watch(config.path.less, ['less']);
|
98 | gulp.watch(config.path.js, ['minify']);
|
99 | gulp.watch(config.path.html, ['html']);
|
100 | gulp.watch(config.path.html, ['resources']);
|
101 |
|
102 | if (config.watchs && config.watchs.length) {
|
103 | config.watchs.forEach(function(watchFunc) {
|
104 | watchFunc();
|
105 | });
|
106 | }
|
107 | });
|
108 |
|
109 | };
|
110 |
|
111 | module.exports = Plumes;
|