inliner.js
11.6 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
var fs = require('fs');
var path = require('path');
var http = require('http');
var https = require('https');
var url = require('url');
var rewriteUrls = require('../urls/rewrite');
var split = require('../utils/split');
var override = require('../utils/object.js').override;
var MAP_MARKER = /\/\*# sourceMappingURL=(\S+) \*\//;
var REMOTE_RESOURCE = /^(https?:)?\/\//;
var NO_PROTOCOL_RESOURCE = /^\/\//;
function ImportInliner (context) {
this.outerContext = context;
}
ImportInliner.prototype.process = function (data, context) {
var root = this.outerContext.options.root;
context = override(context, {
baseRelativeTo: this.outerContext.options.relativeTo || root,
debug: this.outerContext.options.debug,
done: [],
errors: this.outerContext.errors,
left: [],
inliner: this.outerContext.options.inliner,
rebase: this.outerContext.options.rebase,
relativeTo: this.outerContext.options.relativeTo || root,
root: root,
sourceReader: this.outerContext.sourceReader,
sourceTracker: this.outerContext.sourceTracker,
warnings: this.outerContext.warnings,
visited: []
});
return importFrom(data, context);
};
function importFrom(data, context) {
if (context.shallow) {
context.shallow = false;
context.done.push(data);
return processNext(context);
}
var nextStart = 0;
var nextEnd = 0;
var cursor = 0;
var isComment = commentScanner(data);
for (; nextEnd < data.length;) {
nextStart = nextImportAt(data, cursor);
if (nextStart == -1)
break;
if (isComment(nextStart)) {
cursor = nextStart + 1;
continue;
}
nextEnd = data.indexOf(';', nextStart);
if (nextEnd == -1) {
cursor = data.length;
data = '';
break;
}
var noImportPart = data.substring(0, nextStart);
context.done.push(noImportPart);
context.left.unshift([data.substring(nextEnd + 1), override(context, { shallow: false })]);
context.afterContent = hasContent(noImportPart);
return inline(data, nextStart, nextEnd, context);
}
// no @import matched in current data
context.done.push(data);
return processNext(context);
}
function rebaseMap(data, source) {
return data.replace(MAP_MARKER, function (match, sourceMapUrl) {
return REMOTE_RESOURCE.test(sourceMapUrl) ?
match :
match.replace(sourceMapUrl, url.resolve(source, sourceMapUrl));
});
}
function nextImportAt(data, cursor) {
var nextLowerCase = data.indexOf('@import', cursor);
var nextUpperCase = data.indexOf('@IMPORT', cursor);
if (nextLowerCase > -1 && nextUpperCase == -1)
return nextLowerCase;
else if (nextLowerCase == -1 && nextUpperCase > -1)
return nextUpperCase;
else
return Math.min(nextLowerCase, nextUpperCase);
}
function processNext(context) {
return context.left.length > 0 ?
importFrom.apply(null, context.left.shift()) :
context.whenDone(context.done.join(''));
}
function commentScanner(data) {
var commentRegex = /(\/\*(?!\*\/)[\s\S]*?\*\/)/;
var lastStartIndex = 0;
var lastEndIndex = 0;
var noComments = false;
// test whether an index is located within a comment
return function scanner(idx) {
var comment;
var localStartIndex = 0;
var localEndIndex = 0;
var globalStartIndex = 0;
var globalEndIndex = 0;
// return if we know there are no more comments
if (noComments)
return false;
do {
// idx can be still within last matched comment (many @import statements inside one comment)
if (idx > lastStartIndex && idx < lastEndIndex)
return true;
comment = data.match(commentRegex);
if (!comment) {
noComments = true;
return false;
}
// get the indexes relative to the current data chunk
lastStartIndex = localStartIndex = comment.index;
localEndIndex = localStartIndex + comment[0].length;
// calculate the indexes relative to the full original data
globalEndIndex = localEndIndex + lastEndIndex;
globalStartIndex = globalEndIndex - comment[0].length;
// chop off data up to and including current comment block
data = data.substring(localEndIndex);
lastEndIndex = globalEndIndex;
} while (globalEndIndex < idx);
return globalEndIndex > idx && idx > globalStartIndex;
};
}
function hasContent(data) {
var isComment = commentScanner(data);
var firstContentIdx = -1;
while (true) {
firstContentIdx = data.indexOf('{', firstContentIdx + 1);
if (firstContentIdx == -1 || !isComment(firstContentIdx))
break;
}
return firstContentIdx > -1;
}
function inline(data, nextStart, nextEnd, context) {
context.shallow = data.indexOf('@shallow') > 0;
var importDeclaration = data
.substring(nextImportAt(data, nextStart) + '@import'.length + 1, nextEnd)
.replace(/@shallow\)$/, ')')
.trim();
var viaUrl = importDeclaration.indexOf('url(') === 0;
var urlStartsAt = viaUrl ? 4 : 0;
var isQuoted = /^['"]/.exec(importDeclaration.substring(urlStartsAt, urlStartsAt + 2));
var urlEndsAt = isQuoted ?
importDeclaration.indexOf(isQuoted[0], urlStartsAt + 1) :
split(importDeclaration, ' ')[0].length - (viaUrl ? 1 : 0);
var importedFile = importDeclaration
.substring(urlStartsAt, urlEndsAt)
.replace(/['"]/g, '')
.replace(/\)$/, '')
.trim();
var mediaQuery = importDeclaration
.substring(urlEndsAt + 1)
.replace(/^\)/, '')
.trim();
var isRemote = context.isRemote || REMOTE_RESOURCE.test(importedFile);
if (isRemote && (context.localOnly || !allowedResource(importedFile, true, context.imports))) {
if (context.afterContent || hasContent(context.done.join('')))
context.warnings.push('Ignoring remote @import of "' + importedFile + '" as no callback given.');
else
restoreImport(importedFile, mediaQuery, context);
return processNext(context);
}
if (!isRemote && !allowedResource(importedFile, false, context.imports)) {
if (context.afterImport)
context.warnings.push('Ignoring local @import of "' + importedFile + '" as after other inlined content.');
else
restoreImport(importedFile, mediaQuery, context);
return processNext(context);
}
if (!isRemote && context.afterContent) {
context.warnings.push('Ignoring local @import of "' + importedFile + '" as after other CSS content.');
return processNext(context);
}
var method = isRemote ? inlineRemoteResource : inlineLocalResource;
return method(importedFile, mediaQuery, context);
}
function allowedResource(importedFile, isRemote, rules) {
if (rules.length === 0)
return false;
if (isRemote && NO_PROTOCOL_RESOURCE.test(importedFile))
importedFile = 'http:' + importedFile;
var match = isRemote ?
url.parse(importedFile).host :
importedFile;
var allowed = true;
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (rule == 'all')
allowed = true;
else if (isRemote && rule == 'local')
allowed = false;
else if (isRemote && rule == 'remote')
allowed = true;
else if (!isRemote && rule == 'remote')
allowed = false;
else if (!isRemote && rule == 'local')
allowed = true;
else if (rule[0] == '!' && rule.substring(1) === match)
allowed = false;
}
return allowed;
}
function inlineRemoteResource(importedFile, mediaQuery, context) {
var importedUrl = REMOTE_RESOURCE.test(importedFile) ?
importedFile :
url.resolve(context.relativeTo, importedFile);
var originalUrl = importedUrl;
if (NO_PROTOCOL_RESOURCE.test(importedUrl))
importedUrl = 'http:' + importedUrl;
if (context.visited.indexOf(importedUrl) > -1)
return processNext(context);
if (context.debug)
console.error('Inlining remote stylesheet: ' + importedUrl);
context.visited.push(importedUrl);
var proxyProtocol = context.inliner.request.protocol || context.inliner.request.hostname;
var get =
((proxyProtocol && proxyProtocol.indexOf('https://') !== 0 ) ||
importedUrl.indexOf('http://') === 0) ?
http.get :
https.get;
var errorHandled = false;
function handleError(message) {
if (errorHandled)
return;
errorHandled = true;
context.errors.push('Broken @import declaration of "' + importedUrl + '" - ' + message);
restoreImport(importedUrl, mediaQuery, context);
process.nextTick(function () {
processNext(context);
});
}
var requestOptions = override(url.parse(importedUrl), context.inliner.request);
if (context.inliner.request.hostname !== undefined) {
//overwrite as we always expect a http proxy currently
requestOptions.protocol = context.inliner.request.protocol || 'http:';
requestOptions.path = requestOptions.href;
}
get(requestOptions, function (res) {
if (res.statusCode < 200 || res.statusCode > 399) {
return handleError('error ' + res.statusCode);
} else if (res.statusCode > 299) {
var movedUrl = url.resolve(importedUrl, res.headers.location);
return inlineRemoteResource(movedUrl, mediaQuery, context);
}
var chunks = [];
var parsedUrl = url.parse(importedUrl);
res.on('data', function (chunk) {
chunks.push(chunk.toString());
});
res.on('end', function () {
var importedData = chunks.join('');
if (context.rebase)
importedData = rewriteUrls(importedData, { toBase: originalUrl }, context);
context.sourceReader.trackSource(importedUrl, importedData);
importedData = context.sourceTracker.store(importedUrl, importedData);
importedData = rebaseMap(importedData, importedUrl);
if (mediaQuery.length > 0)
importedData = '@media ' + mediaQuery + '{' + importedData + '}';
context.afterImport = true;
var newContext = override(context, {
isRemote: true,
relativeTo: parsedUrl.protocol + '//' + parsedUrl.host + parsedUrl.pathname
});
process.nextTick(function () {
importFrom(importedData, newContext);
});
});
})
.on('error', function (res) {
handleError(res.message);
})
.on('timeout', function () {
handleError('timeout');
})
.setTimeout(context.inliner.timeout);
}
function inlineLocalResource(importedFile, mediaQuery, context) {
var relativeTo = importedFile[0] == '/' ?
context.root :
context.relativeTo;
var fullPath = path.resolve(path.join(relativeTo, importedFile));
if (!fs.existsSync(fullPath) || !fs.statSync(fullPath).isFile()) {
context.errors.push('Broken @import declaration of "' + importedFile + '"');
return processNext(context);
}
if (context.visited.indexOf(fullPath) > -1)
return processNext(context);
if (context.debug)
console.error('Inlining local stylesheet: ' + fullPath);
context.visited.push(fullPath);
var importRelativeTo = path.dirname(fullPath);
var importedData = fs.readFileSync(fullPath, 'utf8');
if (context.rebase) {
var rewriteOptions = {
relative: true,
fromBase: importRelativeTo,
toBase: context.baseRelativeTo
};
importedData = rewriteUrls(importedData, rewriteOptions, context);
}
var relativePath = path.relative(context.root, fullPath);
context.sourceReader.trackSource(relativePath, importedData);
importedData = context.sourceTracker.store(relativePath, importedData);
if (mediaQuery.length > 0)
importedData = '@media ' + mediaQuery + '{' + importedData + '}';
context.afterImport = true;
var newContext = override(context, {
relativeTo: importRelativeTo
});
return importFrom(importedData, newContext);
}
function restoreImport(importedUrl, mediaQuery, context) {
var restoredImport = '@import url(' + importedUrl + ')' + (mediaQuery.length > 0 ? ' ' + mediaQuery : '') + ';';
context.done.push(restoredImport);
}
module.exports = ImportInliner;