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