UNPKG

6.1 kBJavaScriptView Raw
1
2'use strict';
3
4var
5 // sass = require("gulp-sass"),
6 gulp = require("gulp"),
7 minifyCSS = require("gulp-minify-css"),
8 concatCSS = require("gulp-concat-css"),
9 stylus = require("gulp-stylus"),
10 rename = require("gulp-rename"),
11 uglify = require("gulp-uglify"),
12 gulpConcat = require("gulp-concat"),
13 less = require("gulp-less"),
14 coffee = require("gulp-coffee"),
15 postcss = require("gulp-postcss"),
16 autoprefixer = require("autoprefixer"),
17 changed = require("gulp-changed"),
18 ejs = require("gulp-ejs"),
19 ect = require("gulp-ect-simple"),
20 jade = require("gulp-jade"),
21 gulpif = require("gulp-if"),
22 livereload = require("gulp-livereload"),
23 nodeDir = require("node-dir"),
24
25 options = require('minimist')(process.argv.slice(2)),
26 path = require('path');
27
28
29if (!options.dir) {
30 console.error(' Enter output path');
31 process.exit(1);
32}
33
34var confPath = path.join(__dirname, '../config.js');
35var conf = require(confPath)(options);
36
37var
38
39 lastFilesCount = -1,
40 tasks = [],
41
42 buildSystem = [
43 // order: less > sass > stylus => css
44 // css
45 { task: "less", order: 1, path: `${ conf.srcPath }less/**/*.less` },
46 // { task: "sass", order: 2, path: `${ conf.srcPath }sass/**/*.s[c|a]ss` },
47 { task: "stylus", order: 3, path: `${ conf.srcPath }stylus/**/*.styl` },
48 { task: "css", order: 4, path: `${ conf.srcPath }css/**/*.css` },
49
50 // js
51 { task: "coffee", order: 5, path: `${ conf.srcPath }coffee/**/*.coffee` },
52 { task: "js", order: 6, path: `${ conf.srcPath }js/**/*.js` },
53
54 // html
55 { task: "ect", order: 7, path: `${ conf.srcPath }ect/**/*.ect` },
56 { task: "ejs", order: 8, path: `${ conf.srcPath }ejs/**/*.ejs` },
57 { task: "jade", order: 9, path: `${ conf.srcPath }jade/**/*.jade` },
58 ];
59
60
61/* Gulp */
62
63
64
65
66
67function log(msg) {
68 var str = "";
69
70 if (arguments.length === 2)
71 str = ` ${ msg } -> `;
72 else
73 str = ` ${ msg.name }: ${ msg.message } ${ msg.filename }`;
74
75 console.log(str);
76};
77
78
79/* CSS */
80gulp.task("sass", function() {
81 gulp.src([ `${conf.srcPath}sass/**/*.s[c|a]ss`, `!${conf.srcPath}sass/**/_*.s[c|a]ss` ])
82 .pipe(changed(`${conf.srcPath}css/`))
83 .pipe(sass().on("error", sass.logError))
84 .pipe(gulp.dest(`${conf.srcPath}css/_sass/`));
85});
86
87gulp.task("stylus", function() {
88 gulp.src([ `${conf.srcPath}stylus/**/*.styl`, `!${conf.srcPath}stylus/**/_*.styl` ])
89 .pipe(changed(`${conf.srcPath}css/`))
90 .pipe(stylus({ paths: conf.stylusPaths, compress: false }).on("error", log))
91 .pipe(gulp.dest(`${conf.srcPath}css/_stylus/`));
92});
93
94gulp.task("less", function() {
95 gulp.src([ `${conf.srcPath}less/**/*.less`, `!${conf.srcPath}less/**/_*.less` ])
96 .pipe(changed(`${conf.srcPath}css/`))
97 .pipe(less().on("error", log))
98 .pipe(gulp.dest(`${conf.srcPath}css/_less/`));
99});
100
101gulp.task("css", function() {
102 gulp.src([ `${conf.srcPath}css/**/*.css`, `!${conf.srcPath}css/**/_*.css` ])
103 .pipe(changed(`${conf.outputPath}css/`))
104
105 .pipe(gulpif(conf.concat, concatCSS('main.css', { rebaseUrls: false })))
106 .pipe(gulpif(conf.minify, minifyCSS({ rebase: true, root: './' }).on("error", log)))
107
108 .pipe(gulpif(conf.minify, rename(function(path) { path.basename += '.min' })))
109 .pipe(gulpif(!conf.minify, rename({ dirname: '' })))
110
111 .pipe(postcss([ autoprefixer({ browsers: [ "> 1%" ] }) ]))
112 .pipe(gulp.dest(`${conf.outputPath}style/`))
113 .pipe(livereload());
114});
115
116
117/* JavaScript */
118gulp.task("js", function() {
119 gulp.src([ `${conf.srcPath}js/**/*.js`, `!${conf.srcPath}js/**/_*.js` ])
120 .pipe(changed(`${conf.outputPath}js/`))
121
122 .pipe(gulpif(conf.concat, gulpConcat('main.js')))
123 .pipe(gulpif(conf.minify, uglify().on("error", log)))
124
125 .pipe(gulpif(conf.minify, rename(function(path) { path.basename += '.min' })))
126 .pipe(gulpif(!conf.minify, rename({ dirname: '' })))
127
128 .pipe(gulp.dest(`${conf.outputPath}js/`))
129 .pipe(livereload());
130});
131
132gulp.task("coffee", function() {
133 gulp.src([ `${conf.srcPath}coffee/**/*.coffee`, `!${conf.srcPath}coffee/**/_*.coffee` ])
134 .pipe(changed(`${conf.srcPath}js/`))
135 .pipe(coffee({ bare: true }).on('error', log))
136 .pipe(gulp.dest(`${conf.srcPath}js/_coffee/`));
137});
138
139
140/* HTML */
141gulp.task("ejs", function() {
142 gulp.src([ `${conf.srcPath}ejs/**/*.ejs`, `!${conf.srcPath}ejs/**/_*.ejs` ])
143 .pipe(changed(`${conf.srcPath}html/`))
144 .pipe(ejs().on("error", log))
145 .pipe(gulp.dest(conf.outputPath))
146 .pipe(livereload());
147});
148
149gulp.task("ect", function() {
150 gulp.src([ `${conf.srcPath}ect/**/*.ect`, `!${conf.srcPath}ect/**/_*.ect` ])
151 .pipe(changed(conf.outputPath))
152 .pipe(rename({ dirname: '' }))
153 .pipe(ect({
154 options: {
155 root: `${conf.srcPath}ect/`,
156 ext: ".ect",
157 open: "{{",
158 close: "}}"
159 }
160 }).on("error", log))
161 .pipe(gulp.dest(conf.outputPath))
162 .pipe(livereload());
163});
164
165gulp.task("jade", function() {
166 gulp.src([ `${conf.srcPath}jade/**/*.jade`, `!${conf.srcPath}jade/**/_*.jade` ])
167 .pipe(changed(conf.outputPath))
168 .pipe(rename({ dirname: '' }))
169 .pipe(jade({
170 pretty: true
171 }).on("error", log))
172 .pipe(gulp.dest(conf.outputPath))
173 .pipe(livereload());
174});
175
176
177/* GULP */
178gulp.task("watch", [ 'build' ], function() {
179 livereload.listen();
180
181 for (var i = 0, len = buildSystem.length; i < len; i++)
182 tasks.push(gulp.watch(buildSystem[i].path, [ buildSystem[i].task ]));
183});
184
185gulp.task('build', function() {
186
187 for (var i = 0, len = buildSystem.length; i < len; i++) {
188 var task = buildSystem[i];
189 task.task && gulp.run(task.task);
190 }
191
192});
193
194gulp.task('default', [ 'build' ]);
195
196
197setInterval(function() {
198 nodeDir.files(conf.srcPath, function(err, files) {
199 if (err)
200 return console.error(err);
201
202 if (lastFilesCount !== -1 && lastFilesCount !== files.length) {
203 console.log('New file added');
204
205 tasks.forEach(function(t) {
206 t.end();
207 });
208
209 tasks = [];
210
211 gulp.watch("watch");
212 }
213
214 lastFilesCount = files.length;
215 });
216}, 500);