-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
171 lines (140 loc) · 6.49 KB
/
index.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
'use strict';
var isString = require("lodash.isstring");
var isFunction = require("lodash.isfunction");
var isUndefined = require("lodash.isundefined");
var through = require('through2');
var path = require('path');
var bless = require('bless');
var PluginError = require('plugin-error');
var Vinyl = require('vinyl');
var fancyLog = require('fancy-log');
var merge = require('merge');
var applySourcemap = require('vinyl-sourcemaps-apply');
var Concat = require('concat-with-sourcemaps');
var createSuffixFunctionFromString = function(configValue) {
var actualSuffix = configValue === undefined? "-blessed" : configValue;
return function(index) {
return actualSuffix + index;
}
}
var createSuffixFunction = function(configValue) {
if(isString(configValue) || isUndefined(configValue)) {
return createSuffixFunctionFromString(configValue);
} else if(isFunction(configValue)) {
return configValue;
} else {
throw new TypeError("suffix is neither a string nor function");
}
}
module.exports = function(options){
var pluginName = 'gulp-bless';
options = options || {};
options.imports = options.imports === undefined ? true : options.imports;
options.cacheBuster = options.cacheBuster === undefined ? true : options.cacheBuster;
options.suffix = createSuffixFunction(options.suffix);
return through.obj(function(file, enc, cb) {
if (file.isNull()) return cb(null, file); // ignore
if (file.isStream()) return cb(new PluginError(pluginName, 'Streaming not supported'));
var stream = this;
var shouldCreateSourcemaps = file.sourceMap;
if (file.contents && file.contents.toString()) {
var fileName = path.basename(file.path);
var outputFilePath = path.resolve(path.dirname(file.path), fileName);
var contents = file.contents.toString('utf8');
try {
var result = bless.chunk(contents, {
source: outputFilePath,
sourcemaps: shouldCreateSourcemaps
});
}
catch (err) {
return cb(new PluginError(pluginName, err));
}
var numberOfSplits = result.data.length;
if (options.log) {
// print log message
var msg = 'Found ' + result.totalSelectorCount + ' selector';
if (result.data.length > 1) {
msg += 's, splitting into ' + result.data.length + ' blessedFiles.';
} else {
msg += ', not splitting.';
}
fancyLog.info(msg);
}
var addSourcemap = function(fileToAddTo, blessOutputIndex) {
if(!shouldCreateSourcemaps) return fileToAddTo;
var map = result.maps[blessOutputIndex];
map.file = path.relative(fileToAddTo.base, fileToAddTo.path);
map.sources = [path.relative(file.base, file.path)];
//gotta assign fileToAddTo a source map so applyScourceMap can merge original
//and blessed sourcemap.
fileToAddTo.sourceMap = JSON.parse(JSON.stringify(file.sourceMap));
applySourcemap(fileToAddTo, map);
return fileToAddTo;
};
// get out early if the file isn't long enough
if(result.data.length === 1){
return cb(null, addSourcemap(new Vinyl({
cwd: file.cwd,
base: file.base,
path: outputFilePath,
contents: new Buffer(result.data[0])
}), 0));
}
var outputPathStart = path.dirname(outputFilePath);
var outputExtension = path.extname(outputFilePath);
var outputBasename = path.basename(outputFilePath, outputExtension);
var createBlessedFileName = function(index){
return outputBasename + options.suffix(index) + outputExtension;
};
var addImports = function(aFile, fileNamesOfPartsToImport){
var parameters = options.cacheBuster ? '?z=' + Math.round((Math.random() * 999)) : '';
var filePath = path.relative(aFile.base, aFile.path);
var concat = new Concat(shouldCreateSourcemaps, filePath, '\n');
for (var i = 0; i < fileNamesOfPartsToImport.length; i++) {
concat.add(null, "@import url('" + fileNamesOfPartsToImport[i] + parameters + "');\n");
}
concat.add(filePath, aFile.contents, JSON.stringify(aFile.sourceMap));
aFile.contents = concat.content;
if (shouldCreateSourcemaps) {
aFile.sourceMap = JSON.parse(concat.sourceMap);
}
};
var outputFiles = [];
var nonMasterPartFileNames = [];
for(var j = 0; j < numberOfSplits; j++) {
var oneBasedIndex = j + 1;
var isAtLastElement = oneBasedIndex === numberOfSplits;
//last element is the "master" file (the one with @import).
var outputPath = isAtLastElement
? outputFilePath
: path.resolve(path.join(outputPathStart, createBlessedFileName(oneBasedIndex)));
var outFile = addSourcemap(new Vinyl({
cwd: file.cwd,
base: file.base,
path: outputPath,
contents: new Buffer(result.data[j])
}), j);
if (options.imports) {
if (isAtLastElement) {
addImports(outFile, nonMasterPartFileNames);
} else {
nonMasterPartFileNames.push(path.basename(outputPath));
}
}
outputFiles[j] = outFile;
}
//We want to stream the "master" file first then split part1, part2, part3 ... this is mainly to maintain backward
//compatibility; at least for the file emitting order ...
for(var k = 0; k < numberOfSplits; k++){
var fileToPush = k == 0
? outputFiles[numberOfSplits - 1]
: outputFiles[k - 1];
stream.push(fileToPush);
}
cb()
} else {
cb(null, file);
}
});
};