-
Notifications
You must be signed in to change notification settings - Fork 12
/
compile.js
243 lines (185 loc) · 6.06 KB
/
compile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* 接管sass的@import内嵌, 主要用来解决 编译缓存依赖问题。
*/
var map = (function() {
return {
reg: /\/\*embed:(.*?)\*\//ig,
wrap: function( value ) {
return '/*embed:' + value + '*/';
}
};
})();
var sass = require('fis-sass');
var stack = {};
var required;
function fixLineBreak( content ) {
return content.replace(/\r\n|\r|\n/g, '\n');
}
function isEmpty(obj) {
if (obj) {
for (var key in obj) {
return false;
}
}
return true;
}
// 匹配scss内嵌代码:@import 'xxx';
// @import 'xxx', 'xxx';
function _process( content ) {
var reg = /((?:\/\/.*?\n)|(?:\/\*[\s\S]*?\*\/))|(?:@import\s([\s\S]*?)(?:\n|$)(?!\s+[^{@]*\n))/ig;
// var reg = /@import[\s\S]*?;/ig;
var rUrl = /^url/i;
var rEnd = /;$/;
content = fixLineBreak( content );
return content.replace( reg, function( all, comments, value ) {
var files;
value = value ? value.trim().replace( rEnd, '' ) : '';
// If it's comments or If the @import has any media queries.
if ( comments || /('|").*?\1\s+[^'"]+$/.exec(value) ) {
return all;
}
files = value.split(/\s*,?\s+/m).map(function( value ) {
var quote = '',
match;
value = value.trim();
match = /^('|")(.*)\1$/.exec( value );
if ( match ) {
value = match[ 2 ];
quote = match[ 1 ];
}
// If the file’s extension is .css.
if ( /\.css$/i.exec( value ) ||
// If the filename begins with http://.
/^http/i.exec( value ) ||
// If the filename is a url().
/^url/i.exec( value ) ) {
return '@import ' + quote + value + quote + ';';
}
return map.wrap( value );
});
return files.join('\n');
});
}
// 查找文件。
function lookup( name, ext, paths ) {
var files = [],
filename = name,
basename = filename,
dirname = '',
mapping,
i, j, len, len2, path, info;
files.push( filename );
if ( /^(.*(?:\/|\\))([^\/\\]*?)$/.exec( filename ) ) {
dirname = RegExp.$1;
basename = RegExp.$2;
}
// 自动加后缀。
if ( !/\.(scss|sass)$/.exec( basename ) ) {
filename = dirname + basename + ext;
files.push( filename );
if ( !/^_/.exec( basename ) ) {
filename = dirname + '_' + basename + ext;
files.push( filename );
}
mapping = {
'.sass': '.scss',
'.scss': '.sass'
};
if ( mapping[ ext ] ) {
filename = dirname + basename + mapping[ ext ];
files.push( filename );
if ( !/^_/.exec( basename ) ) {
filename = dirname + '_' + basename + mapping[ ext ];
files.push( filename );
}
}
}
len = files.length;
len2 = paths.length;
for ( i = 0; i < len; i++ ) {
name = files[ i ];
for ( j = 0; j < len2; j++ ) {
info = fis.uri( name, paths[ j ] );
if( info.file && info.file.isFile() ) {
return info.file;
}
}
}
// console.log( paths, files );
}
function isSassSyntax( content, file ) {
return file.ext === '.sass'/* && !~content.indexOf('{')*/;
}
function unique( arr ) {
return arr.filter(function( item, index, arr ) {
return arr.indexOf( item ) === index;
});
}
var compile = module.exports = function( content, file, opts ) {
if ( isSassSyntax( content, file ) ) {
content = sass.sass2scss( content );
}
required = {};
opts.data = before( content, file, unique( opts.includePaths ) );
try {
content = sass.renderSync( opts );
} catch (ex) {
fis.log.error(ex);
}
content = after( content, file, opts.includePaths );
return content;
};
var before = compile.before = function( content, currentFile, paths) {
// @todo use cache of this progress.
var ext = currentFile.ext;
paths = unique( paths );
return _process( content ).replace( map.reg, function( all, value ) {
var file = lookup( value, ext, paths ),
content;
if ( file ) {
// 已经在堆里面了
if ( stack[ file.realpath ] ) {
fis.log.error( 'can\'t embed file ' + file.basename + '.' );
}
// 如果已经引入过了,就不再引入。
// 类似与include_once.
if ( required[ file.realpath ] ) {
return '';
}
// 有可能当前文件并没有经过 fis 的compile,以至于没有 cahce 对象。
if (currentFile.cache) {
currentFile.cache.addDeps(file.realpath);
}
stack[ file.realpath ] = true;
required[ file.realpath ] = true;
content = file.getContent();
if ( isSassSyntax( content, file ) ) {
content = sass.sass2scss( content );
}
content = before( content, file, [ file.dirname ].concat( paths ) );
content = fixPath(content, file);
delete stack[ file.realpath ];
return content;
} else {
fis.log.error( value + ' not found!' );
return '';
}
});
};
var after = compile.after = function( content, file, paths ) {
return content;
};
var fixPath = compile.fixPath = function(content, file) {
// 处理,解决资源引用路径问题。
content = fis.compile.extCss(content);
return content.replace(fis.compile.lang.reg, function(all, type, value) {
var info = fis.uri(value, file.dirname);
if (info.file) {
value = info.quote + info.file.subpath + info.query + info.quote;
}
if (type === 'embed' || type === 'jsEmbed') {
value = fis.compile.lang[type].ld + value + fis.compile.lang[type].rd;
}
return value;
});
}