1 | var $, M, _;
|
2 |
|
3 | $ = {};
|
4 |
|
5 | $.i = require('../dist/i');
|
6 |
|
7 | $.getBasename = require('../dist/getBasename');
|
8 |
|
9 | $.getDirname = require('../dist/getDirname');
|
10 |
|
11 | $.compile_ = require('../dist/compile_');
|
12 |
|
13 | $.copy_ = require('../dist/copy_');
|
14 |
|
15 | $.reload = require('../dist/reload');
|
16 |
|
17 | $.watch = require('../dist/watch');
|
18 |
|
19 | _ = {};
|
20 |
|
21 | _.merge = require('lodash/merge');
|
22 |
|
23 | _.get = require('lodash/get');
|
24 |
|
25 | M = class M {
|
26 | constructor(option) {
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | var data;
|
41 | data = {
|
42 | compile: {
|
43 | extend: ['!**/include/**', '!**/*.min.*'],
|
44 | extname: [
|
45 | '.coffee',
|
46 | '.css',
|
47 | '.html',
|
48 | '.js',
|
49 | '.md',
|
50 | '.pug',
|
51 | '.styl',
|
52 |
|
53 | '.yaml'
|
54 | ]
|
55 | },
|
56 | copy: {
|
57 | extend: ['!**/include/**'],
|
58 | extname: ['.css', '.gif', '.html', '.jpg', '.js', '.json', '.png', '.ttf', '.txt', '.vue', '.xml']
|
59 | },
|
60 | option: {},
|
61 | path: {
|
62 | source: './source',
|
63 | target: './dist'
|
64 | }
|
65 | };
|
66 | this.data = _.merge(data, option);
|
67 | this;
|
68 | }
|
69 |
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | bind() {
|
81 | process.on('uncaughtException', function(e) {
|
82 | return $.i(e.stack);
|
83 | });
|
84 | return this;
|
85 | }
|
86 |
|
87 | async compile_(source, option = {}) {
|
88 | var reg, stringSource, stringTarget, target;
|
89 | stringSource = $.getBasename(this.get('path.source'));
|
90 | stringTarget = $.getBasename(this.get('path.target'));
|
91 | reg = new RegExp(stringSource);
|
92 | target = $.getDirname(source).replace(reg, stringTarget).replace(/\/{2,}/g, '/');
|
93 | if (option.map == null) {
|
94 | option.map = true;
|
95 | }
|
96 | if (option.minify == null) {
|
97 | option.minify = false;
|
98 | }
|
99 | await $.compile_(source, target, option);
|
100 | return this;
|
101 | }
|
102 |
|
103 | async copy_(source) {
|
104 | var reg, stringSource, stringTarget, target;
|
105 | stringSource = $.getBasename(this.get('path.source'));
|
106 | stringTarget = $.getBasename(this.get('path.target'));
|
107 | reg = new RegExp(stringSource);
|
108 | target = $.getDirname(source).replace(reg, stringTarget).replace(/\/{2,}/g, '/');
|
109 | await $.copy_(source, target);
|
110 | return this;
|
111 | }
|
112 |
|
113 | execute() {
|
114 | this.bind().watchCompile().watchCopy().reloadCss();
|
115 | return this;
|
116 | }
|
117 |
|
118 | get(key) {
|
119 | return _.get(this.data, key);
|
120 | }
|
121 |
|
122 | reloadCss() {
|
123 | var pathTarget;
|
124 | pathTarget = this.get('path.target');
|
125 | $.reload(`${pathTarget}/**/*.css`);
|
126 | return this;
|
127 | }
|
128 |
|
129 | watchCompile() {
|
130 | var ext, listExt, listSource, pathBuild, pathSource;
|
131 | listExt = this.get('compile.extname');
|
132 | pathSource = this.get('path.source');
|
133 | pathBuild = this.get('path.build');
|
134 | listSource = [
|
135 | ...((function() {
|
136 | var i,
|
137 | len,
|
138 | results;
|
139 | results = [];
|
140 | for (i = 0, len = listExt.length; i < len; i++) {
|
141 | ext = listExt[i];
|
142 | results.push(`${pathSource}/**/*${ext}`);
|
143 | }
|
144 | return results;
|
145 | })()),
|
146 | ...(this.get('compile.extend'))
|
147 | ];
|
148 | $.watch(listSource, async(e) => {
|
149 | return (await this.compile_(e.path, this.data.option));
|
150 | });
|
151 | return this;
|
152 | }
|
153 |
|
154 | watchCopy() {
|
155 | var ext, listExt, listSource, pathBuild, pathSource;
|
156 | listExt = this.get('copy.extname');
|
157 | pathSource = this.get('path.source');
|
158 | pathBuild = this.get('path.build');
|
159 | listSource = [
|
160 | ...((function() {
|
161 | var i,
|
162 | len,
|
163 | results;
|
164 | results = [];
|
165 | for (i = 0, len = listExt.length; i < len; i++) {
|
166 | ext = listExt[i];
|
167 | results.push(`${pathSource}/**/*${ext}`);
|
168 | }
|
169 | return results;
|
170 | })()),
|
171 | ...(this.get('copy.extend'))
|
172 | ];
|
173 | $.watch(listSource, async(e) => {
|
174 | return (await this.copy_(e.path));
|
175 | });
|
176 | return this;
|
177 | }
|
178 |
|
179 | };
|
180 |
|
181 | module.exports = function(option = {}) {
|
182 | var m;
|
183 | m = new M(option);
|
184 | m.execute();
|
185 | return this;
|
186 | };
|