UNPKG

8.39 kBJavaScriptView Raw
1var $, M, _, gulp, gulpIf, using;
2
3$ = {};
4
5$.type = require('../dist/type');
6
7$.wrapList = require('../dist/wrapList');
8
9$.normalizePath = require('../dist/normalizePath');
10
11$.source_ = require('../dist/source_');
12
13$.getName = require('../dist/getName');
14
15$.info = require('../dist/info');
16
17_ = {};
18
19_.extend = require('lodash/extend');
20
21gulp = require('gulp');
22
23gulpIf = require('gulp-if');
24
25using = require('gulp-using');
26
27M = (function() {
28 class M {
29 /*
30 compileCoffee_(source, target, option)
31 compileCss_(source, target, option)
32 compileHtml_(source, target, option)
33 compileJs_(source, target, option)
34 compileMd_(source, target, option)
35 compilePug_(source, target, option)
36 compileStyl_(source, target, option)
37 compileTs_(source, target, option)
38 compileYaml_(source, target, option)
39 execute_(arg...)
40 */
41 async compileCoffee_(source, target, option) {
42 await new Promise(function(resolve) {
43 var base, coffee, composer, include, sourcemaps, uglify, uglifyEs;
44 coffee = require('gulp-coffee');
45 include = require('gulp-include');
46 uglifyEs = require('uglify-es');
47 composer = require('gulp-uglify/composer');
48 uglify = composer(uglifyEs, console);
49 base = option.base;
50 sourcemaps = option.map;
51 return gulp.src(source, {base, sourcemaps}).pipe(using()).pipe(include()).pipe(coffee(option)).pipe(gulpIf(option.minify, uglify())).pipe(gulp.dest(target, {sourcemaps})).on('end', function() {
52 return resolve();
53 });
54 });
55 return this;
56 }
57
58 async compileHtml_(source, target, option) {
59 await new Promise(function(resolve) {
60 var base, htmlmin, rename;
61 htmlmin = require('gulp-htmlmin');
62 rename = require('gulp-rename');
63 base = option.base;
64 return gulp.src(source, {base}).pipe(using()).pipe(rename({
65 extname: '.html'
66 })).pipe(gulpIf(option.minify, htmlmin({
67 collapseWhitespace: true
68 }))).pipe(gulp.dest(target)).on('end', function() {
69 return resolve();
70 });
71 });
72 return this;
73 }
74
75 async compileCss_(source, target, option) {
76 await new Promise(function(resolve) {
77 var base, cleanCss, sourcemaps;
78 cleanCss = require('gulp-clean-css');
79 base = option.base;
80 sourcemaps = option.map;
81 return gulp.src(source, {base, sourcemaps}).pipe(using()).pipe(gulpIf(option.minify, cleanCss())).pipe(gulp.dest(target, {sourcemaps})).on('end', function() {
82 return resolve();
83 });
84 });
85 return this;
86 }
87
88 async compileJs_(source, target, option) {
89 await new Promise(function(resolve) {
90 var base, composer, sourcemaps, uglify, uglifyEs;
91 uglifyEs = require('uglify-es');
92 composer = require('gulp-uglify/composer');
93 uglify = composer(uglifyEs, console);
94 base = option.base;
95 sourcemaps = option.map;
96 return gulp.src(source, {base, sourcemaps}).pipe(using()).pipe(gulpIf(option.minify, uglify())).pipe(gulp.dest(target, {sourcemaps})).on('end', function() {
97 return resolve();
98 });
99 });
100 return this;
101 }
102
103 async compileMd_(source, target, option) {
104 await new Promise(function(resolve) {
105 var base, htmlmin, markdown, rename;
106 htmlmin = require('gulp-htmlmin');
107 markdown = require('gulp-markdown');
108 rename = require('gulp-rename');
109 if (option.sanitize == null) {
110 option.sanitize = true;
111 }
112 base = option.base;
113 return gulp.src(source, {base}).pipe(using()).pipe(markdown(option)).pipe(rename({
114 extname: '.html'
115 })).pipe(gulpIf(option.minify, htmlmin({
116 collapseWhitespace: true
117 }))).pipe(gulp.dest(target)).on('end', function() {
118 return resolve();
119 });
120 });
121 return this;
122 }
123
124 async compilePug_(source, target, option) {
125 await new Promise(function(resolve) {
126 var base, pug;
127 pug = require('gulp-pug');
128 if (option.pretty == null) {
129 option.pretty = !option.minify;
130 }
131 base = option.base;
132 return gulp.src(source, {base}).pipe(using()).pipe(pug(option)).pipe(gulp.dest(target)).on('end', function() {
133 return resolve();
134 });
135 });
136 return this;
137 }
138
139 async compileStyl_(source, target, option) {
140 await new Promise(function(resolve) {
141 var base, sourcemaps, stylus;
142 stylus = require('gulp-stylus');
143 if (option.compress == null) {
144 option.compress = option.minify;
145 }
146 base = option.base;
147 sourcemaps = option.map;
148 return gulp.src(source, {base, sourcemaps}).pipe(using()).pipe(stylus(option)).pipe(gulp.dest(target, {sourcemaps})).on('end', function() {
149 return resolve();
150 });
151 });
152 return this;
153 }
154
155
156 // compileTs_: (source, target, option) ->
157
158 // await new Promise (resolve) ->
159
160 // ts = require 'gulp-typescript'
161 // uglifyEs = require 'uglify-es'
162 // composer = require 'gulp-uglify/composer'
163 // uglify = composer uglifyEs, console
164
165 // base = option.base
166 // isMinify = option.minify
167 // sourcemaps = option.map
168
169 // # have to delete these unknown options
170 // delete option.map
171 // delete option.minify
172
173 // gulp.src source, {base, sourcemaps}
174 // .pipe using()
175 // .pipe ts option
176 // .pipe gulpIf isMinify, uglify()
177 // .pipe gulp.dest target, {sourcemaps}
178 // .on 'end', -> resolve()
179
180 // @ # return
181 async compileYaml_(source, target, option) {
182 await new Promise(function(resolve) {
183 var base, yaml;
184 yaml = require('gulp-yaml');
185 if (option.safe == null) {
186 option.safe = true;
187 }
188 base = option.base;
189 return gulp.src(source, {base}).pipe(using()).pipe(yaml(option)).pipe(gulp.dest(target)).on('end', function() {
190 return resolve();
191 });
192 });
193 return this;
194 }
195
196 async execute_(...arg) {
197 var dirname, extname, i, len, listSource, method, msg, option, source, target, type;
198 [source, target, option] = (function() {
199 switch (arg.length) {
200 case 1:
201 return [arg[0], null, {}];
202 case 2:
203 return (function() {
204 var type;
205 type = $.type(arg[1]);
206 if (type === 'object') {
207 return [arg[0], null, arg[1]];
208 }
209 if (type === 'string') {
210 return [arg[0], arg[1], {}];
211 }
212 throw new Error(`invalid type '${type}'`);
213 })();
214 case 3:
215 return arg;
216 default:
217 throw new Error('invalid argument length');
218 }
219 })();
220 option = _.extend({
221 map: false,
222 minify: true
223 }, option);
224 // message
225 msg = `compiled ${$.wrapList(source)}`;
226 if (target) {
227 msg += ` to ${$.wrapList(target)}`;
228 }
229 // base
230 type = $.type(source);
231 if (type === 'string' && ~source.search(/\/\*/)) {
232 option.base || (option.base = $.normalizePath(source).replace(/\/\*.*/, ''));
233 }
234 // each & compile
235 listSource = (await $.source_(source));
236 for (i = 0, len = listSource.length; i < len; i++) {
237 source = listSource[i];
238 ({extname, dirname} = $.getName(source));
239 method = this.mapMethod[extname];
240 method || (function() {
241 throw new Error(`invalid extname '${extname}'`);
242 })();
243 target || (target = dirname);
244 target = $.normalizePath(target);
245 await this[method](source, target, option);
246 }
247 $.info('compile', msg);
248 return this;
249 }
250
251 };
252
253 /*
254 mapMethod
255 */
256 M.prototype.mapMethod = {
257 '.coffee': 'compileCoffee_',
258 '.css': 'compileCss_',
259 '.htm': 'compileHtml_',
260 '.html': 'compileHtml_',
261 '.js': 'compileJs_',
262 '.md': 'compileMd_',
263 '.pug': 'compilePug_',
264 '.styl': 'compileStyl_',
265 // '.ts': 'compileTs_'
266 '.yaml': 'compileYaml_',
267 '.yml': 'compileYaml_'
268 };
269
270 return M;
271
272}).call(this);
273
274module.exports = async function(...arg) {
275 var m;
276 m = new M();
277 await m.execute_(...arg);
278 return this;
279};