-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 1.91 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
const PluginError = require("plugin-error");
const Transform = require("stream").Transform;
const path = require("path");
const applySourceMap = require("vinyl-sourcemaps-apply");
const dartSass = require("sass");
const PLUGIN_NAME = "gulp-dart-scss";
module.exports = (options = {}) => {
return new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
// Return chunk if not scss extension
if (path.extname(chunk.path) !== ".scss") {
return callback(null, chunk);
}
// Ignore if empty or null
if (chunk._contents.length === 0 || chunk.isNull()) {
return callback(null);
}
// Ignore _name.scss files
if (path.basename(chunk.path).indexOf("_") === 0) {
return callback(null);
}
if (chunk.isBuffer()) {
options.sourceMap = chunk.sourceMap ? chunk.path : false;
options.file = chunk.path || options.file;
// Rename extension
chunk.path = path.join(
path.dirname(chunk.path),
path.basename(chunk.path, path.extname(chunk.path)) + ".css"
);
try {
const renderResult = dartSass.renderSync(options);
chunk._contents = Buffer.from(renderResult.css);
if (renderResult.map && chunk.sourceMap) {
const map = JSON.parse(renderResult.map);
map.file = chunk.relative;
map.sources = map.sources.map((source) =>
path.relative(chunk.base, source)
);
applySourceMap(chunk, map);
}
return callback(null, chunk);
} catch (error) {
return callback(
new PluginError(PLUGIN_NAME, error.message, {
fileName: chunk.relative,
})
);
}
} else if (chunk.isStream()) {
return callback(
new PluginError(PLUGIN_NAME, "Streaming is not supported.")
);
}
},
});
};