UNPKG

6.34 kBJavaScriptView Raw
1'use strict';
2/**
3 * 文件编译处理
4 *
5 * @class Compile
6 * {
7 * src:'', //输入文件
8 * dist:undefined, //输出模块,不指定由编译模块处理
9 * debug:true //开启debug模式,会生成map并编译到dev目录
10 * }
11 */
12class 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 //配置写入到_ts.config
27 for(let i in option){
28 config[i] = option[i];
29 };
30
31 //默认开启debug模式
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 * @memberOf Compile
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 // '.js':apis.JsPost, //js合并
82 // '.css':apis.CssPost, //前缀处理,压缩
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 * @param {object} option
131 * @returns
132 * @memberOf Compile
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 * @memberOf Compile
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 //开发环境,即debug为true的情况下
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 //.tsx文件则编译到与文件相同的目录
211 // switch (type) {
212 // case '.tsx':
213 // distPath =
214 // config.debug ?
215 // distPath.replace(fws.devPath,fws.srcPath):
216 // distPath.replace(fws.distPath,fws.srcPath)
217 // break;
218 // };
219
220 return distPath;
221 }
222};
223
224module.exports = Compile;