1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | class Compile{
|
13 | constructor(option){
|
14 | const _ts = this;
|
15 |
|
16 | option = option || {};
|
17 |
|
18 | let m = _ts.m = {
|
19 | fs:require('fs-extra'),
|
20 | path:require('path'),
|
21 | pathInfo:require('./getPathInfo'),
|
22 | tip:require('./tip')
|
23 | },
|
24 | config = _ts.config = {};
|
25 |
|
26 |
|
27 | for(let i in option){
|
28 | config[i] = option[i];
|
29 | };
|
30 |
|
31 |
|
32 | config.debug = config.debug === undefined ? true : config.debug;
|
33 |
|
34 |
|
35 | if(m.pathInfo(config.src).type === 'file'){
|
36 | _ts.init();
|
37 | }else{
|
38 | m.tip.error(config.src + ' 文件不存在');
|
39 | };
|
40 | }
|
41 |
|
42 | init(){
|
43 | const _ts = this,
|
44 | m = _ts.m,
|
45 | config = _ts.config;
|
46 |
|
47 | let fileType = m.path.extname(config.src).toLowerCase(),
|
48 | fileName = m.path.basename(config.src,fileType),
|
49 | filePrefix = fileName ? fileName.substr(0,1) : undefined;
|
50 |
|
51 |
|
52 |
|
53 | let distPath = _ts.distPath(fileName,fileType);
|
54 | let a = _ts.compile(config.src,distPath,fileType);
|
55 | console.log(a);
|
56 |
|
57 | }
|
58 | |
59 |
|
60 |
|
61 |
|
62 | compile(src,dist,fileType){
|
63 | const _ts = this,
|
64 | m = _ts.m,
|
65 | config = _ts.config;
|
66 |
|
67 | let option = {
|
68 | src:src,
|
69 | dist:dist,
|
70 | debug:config.debug
|
71 | };
|
72 |
|
73 | let fns = {
|
74 | '.pug':_ts.getCompileFn(option,'pug'),
|
75 | '.jade':_ts.getCompileFn(option,'pug'),
|
76 | '.scss':_ts.getCompileFn(option,'sass'),
|
77 | '.sass':_ts.getCompileFn(option,'sass'),
|
78 | '.ts':_ts.getCompileFn(option,'ts'),
|
79 | '.tsx':_ts.getCompileFn(option,'ts'),
|
80 | '.jsx':_ts.getCompileFn(option,'jsx'),
|
81 |
|
82 |
|
83 | '.es':_ts.getCompileFn(option,'ts'),
|
84 | '.es6':_ts.getCompileFn(option,'ts'),
|
85 | '.png':_ts.getCompileFn(option,'sprite'),
|
86 | '.svg':_ts.getCompileFn(option,'sprite')
|
87 | };
|
88 |
|
89 | switch (fns[fileType]) {
|
90 | case undefined:
|
91 | return _ts.copy(option);
|
92 | break;
|
93 | case null:
|
94 | return undefined;
|
95 | break;
|
96 | default:
|
97 | return fns[fileType]();
|
98 | break;
|
99 | };
|
100 | }
|
101 |
|
102 |
|
103 | getCompileFn(option,compileType){
|
104 | const _ts = this,
|
105 | m = _ts.m,
|
106 | config = _ts.config;
|
107 | let apis = require('../api'),
|
108 | promises = {
|
109 | pug:()=>{
|
110 | return new apis.Pug2html(option);
|
111 | },
|
112 | sass:()=>{
|
113 | return new apis.Sass2css(option);
|
114 | },
|
115 | ts:()=>{
|
116 | return new apis.Ts2(option);
|
117 | },
|
118 | jsx:()=>{
|
119 | return new apis.Jsx2js(option);
|
120 | },
|
121 | _sprite:()=>{
|
122 | return new apis.OutSprite(option);
|
123 | }
|
124 | };
|
125 | return promises[compileType];
|
126 | }
|
127 |
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | copy(option){
|
135 | const _ts = this,
|
136 | m = _ts.m,
|
137 | config = _ts.config;
|
138 |
|
139 | option = option || {};
|
140 |
|
141 | let src = option.src,
|
142 | dist = option.dist;
|
143 |
|
144 | return new Promise((resolve,reject)=>{
|
145 | m.fs.copy(src,dist,(err) => {
|
146 | if(err){
|
147 | reject({
|
148 | status:'error',
|
149 | msg:`拷贝失败 ${src}`,
|
150 | info:err
|
151 | });
|
152 | }else{
|
153 | resolve({
|
154 | status:'success',
|
155 | msg:`拷贝成功 ${dist}`
|
156 | });
|
157 | };
|
158 | })
|
159 | });
|
160 | }
|
161 |
|
162 | |
163 |
|
164 |
|
165 |
|
166 | distPath(fileName,type){
|
167 | const _ts = this,
|
168 | m = _ts.m,
|
169 | config = _ts.config;
|
170 |
|
171 |
|
172 | let distPath = '',
|
173 |
|
174 |
|
175 | inOutFileType = {
|
176 | '.pug':'.html',
|
177 | '.jade':'.html',
|
178 | '.scss':'.css',
|
179 | '.ts':'.js',
|
180 | '.tsx':'.jsx',
|
181 | '.jsx':'.js',
|
182 | '.es':'.js',
|
183 | '.es6':'.js'
|
184 | },
|
185 |
|
186 |
|
187 | outExtName = inOutFileType[type] === undefined ? type : inOutFileType[type];
|
188 |
|
189 |
|
190 | if(config.dist === undefined){
|
191 | distPath =
|
192 |
|
193 |
|
194 | config.debug ?
|
195 | m.path.join(
|
196 | m.path.dirname(config.src.replace(fws.srcPath,fws.devPath)),
|
197 | fileName + outExtName
|
198 |
|
199 | ):
|
200 |
|
201 |
|
202 | m.path.join(
|
203 | m.path.dirname(config.src.replace(fws.srcPath,fws.distPath)),
|
204 | fileName + outExtName
|
205 | )
|
206 | }else{
|
207 | distPath = config.dist;
|
208 | };
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | return distPath;
|
221 | }
|
222 | };
|
223 |
|
224 | module.exports = Compile;
|