forked from sindresorhus/gulp-ruby-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
225 lines (183 loc) · 5.75 KB
/
index.js
File metadata and controls
225 lines (183 loc) · 5.75 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
'use strict';
var fs = require('fs');
var path = require('path');
var Readable = require('stream').Readable;
var assign = require('object-assign');
var convert = require('convert-source-map');
var dargs = require('dargs');
var eachAsync = require('each-async');
var glob = require('glob');
var gutil = require('gulp-util');
var osTmpdir = require('os-tmpdir');
var pathExists = require('path-exists');
var rimraf = require('rimraf');
var spawn = require('cross-spawn-async');
var logger = require('./lib/logger');
var utils = require('./lib/utils');
var emitErr = utils.emitErr;
var replaceLocation = utils.replaceLocation;
var createIntermediatePath = utils.createIntermediatePath;
var defaults = {
tempDir: path.join(osTmpdir(), 'gulp-ruby-sass'),
verbose: false,
sourcemap: false,
emitCompileError: false
};
if (typeof process.getuid === 'function') {
defaults.tempDir += '-' + process.getuid();
}
function gulpRubySass(sources, options) {
var stream = new Readable({objectMode: true});
// redundant but necessary
stream._read = function () {};
options = assign({}, defaults, options);
// alert user that `container` is deprecated
if (options.container) {
gutil.log(gutil.colors.yellow('The container option has been deprecated. Simultaneous tasks work automatically now!'));
}
// error if user tries to watch their files with the Sass gem
if (options.watch || options.poll) {
emitErr(stream, '`watch` and `poll` are not valid options for gulp-ruby-sass. Use `gulp.watch` to rebuild your files on change.');
}
// error if user tries to pass a Sass option to sourcemap
if (typeof options.sourcemap !== 'boolean') {
emitErr(stream, 'The sourcemap option must be true or false. See the readme for instructions on using Sass sourcemaps with gulp.');
}
options.sourcemap = options.sourcemap === true ? 'file' : 'none';
options.update = true;
// simplified handling of array sources, like gulp.src
if (!Array.isArray(sources)) {
sources = [sources];
}
var matches = [];
var bases = [];
sources.forEach(function (source) {
matches.push(glob.sync(source));
bases.push(options.base || utils.calculateBase(source));
});
// log and return stream if there are no file matches
if (matches[0].length < 1) {
gutil.log('No files matched your Sass source.');
stream.push(null);
return stream;
}
var intermediateDir = createIntermediatePath(sources, matches, options);
var compileMappings = [];
var baseMappings = {};
matches.forEach(function (matchArray, i) {
var base = bases[i];
matchArray.filter(function (match) {
// remove _partials
return path.basename(match).indexOf('_') !== 0;
})
.forEach(function (match) {
var dest = gutil.replaceExtension(
replaceLocation(match, base, intermediateDir),
'.css'
);
var relative = path.relative(intermediateDir, dest);
// source:dest mappings for the Sass CLI
compileMappings.push(match + ':' + dest);
// store base values by relative file path
baseMappings[relative] = base;
});
});
var args = dargs(options, [
'bundleExec',
'watch',
'poll',
'tempDir',
'verbose',
'emitCompileError',
'base',
'container'
]).concat(compileMappings);
var command;
if (options.bundleExec) {
command = 'bundle';
args.unshift('exec', 'sass');
}
else {
command = 'sass';
}
// plugin logging
if (options.verbose) {
logger.verbose(command, args);
}
var sass = spawn(command, args);
sass.stdout.setEncoding('utf8');
sass.stderr.setEncoding('utf8');
sass.stdout.on('data', function (data) {
logger.stdout(stream, intermediateDir, data);
});
sass.stderr.on('data', function (data) {
logger.stderr(stream, intermediateDir, data);
});
sass.on('error', function (err) {
logger.error(stream, err);
});
sass.on('close', function (code) {
if (options.emitCompileError && code !== 0) {
emitErr(stream, 'Sass compilation failed. See console output for more information.');
}
glob(path.join(intermediateDir, '**', '*'), function (err, files) {
if (err) {
emitErr(stream, err);
}
eachAsync(files, function (file, i, next) {
if (fs.statSync(file).isDirectory() || path.extname(file) === '.map') {
next();
return;
}
var relative = path.relative(intermediateDir, file);
var base = baseMappings[relative];
fs.readFile(file, function (err, data) {
if (err) {
emitErr(stream, err);
next();
return;
}
// rewrite file paths so gulp thinks the file came from cwd, not the
// intermediate directory
var vinylFile = new gutil.File({
cwd: process.cwd(),
base: base,
path: replaceLocation(file, intermediateDir, base)
});
// sourcemap integration
if (options.sourcemap === 'file' && pathExists.sync(file + '.map')) {
// remove sourcemap comment; gulp-sourcemaps will add it back in
data = new Buffer(convert.removeMapFileComments(data.toString()));
var sourceMapObject = JSON.parse(fs.readFileSync(file + '.map', 'utf8'));
// create relative paths for sources
sourceMapObject.sources = sourceMapObject.sources.map(function (sourcePath) {
var absoluteSourcePath = decodeURI(path.resolve(
'/',
sourcePath.replace('file:///', '')
));
return path.relative(base, absoluteSourcePath);
});
vinylFile.sourceMap = sourceMapObject;
}
vinylFile.contents = data;
stream.push(vinylFile);
next();
return;
});
}, function () {
stream.push(null);
});
});
});
return stream;
}
gulpRubySass.logError = function (err) {
var message = new gutil.PluginError('gulp-ruby-sass', err);
process.stderr.write(message + '\n');
this.emit('end');
};
gulpRubySass.clearCache = function (tempDir) {
tempDir = tempDir || defaults.tempDir;
rimraf.sync(tempDir);
};
module.exports = gulpRubySass;