forked from joliss/broccoli-sass
-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.js
155 lines (131 loc) · 5.2 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
var path = require('path');
var includePathSearcher = require('include-path-searcher');
var CachingWriter = require('broccoli-caching-writer');
var fs = require('fs');
var rsvp = require('rsvp');
module.exports = function(sass) {
SassCompiler.prototype = Object.create(CachingWriter.prototype);
SassCompiler.prototype.constructor = SassCompiler;
function SassCompiler (inputNodes, inputFile, outputFile, options) {
if (!(this instanceof SassCompiler)) { return new SassCompiler(inputNodes, inputFile, outputFile, options); }
if (!Array.isArray(inputNodes)) { throw new Error('Expected array for first argument - did you mean [tree] instead of tree?'); }
options = options || {};
CachingWriter.call(this, inputNodes, {
annotation: options.annotation,
cacheInclude: options.cacheInclude,
cacheExclude: options.cacheExclude
});
this.inputFile = inputFile;
this.outputFile = outputFile;
this.useRender = false;
if (!sass.compileAsync) {
this.useRender = true;
this.renderSass = rsvp.denodeify(sass.render);
this.sassOptions = {
importer: options.importer,
functions: options.functions,
indentedSyntax: options.indentedSyntax,
omitSourceMapUrl: options.omitSourceMapUrl,
outputStyle: options.outputStyle,
precision: options.precision,
quietDeps: options.quietDeps,
silenceDeprecations: options.silenceDeprecations,
sourceComments: options.sourceComments,
sourceMap: options.sourceMap,
sourceMapEmbed: options.sourceMapEmbed,
sourceMapContents: options.sourceMapContents,
sourceMapRoot: options.sourceMapRoot,
fiber: options.fiber
};
return;
}
this.renderSass = sass.compileAsync;
this.sassOptions = {
importer: options.importer,
functions: options.functions,
indentedSyntax: options.indentedSyntax,
omitSourceMapUrl: options.omitSourceMapUrl,
style: options.outputStyle,
precision: options.precision,
quietDeps: options.quietDeps,
silenceDeprecations: options.silenceDeprecations,
sourceComments: options.sourceComments,
sourceMap: options.sourceMap,
sourceMapEmbed: options.sourceMapEmbed,
sourceMapIncludeSources: options.sourceMapIncludeSources === undefined ? true : options.sourceMapIncludeSources,
sourceMapContents: options.sourceMapContents,
sourceMapRoot: options.sourceMapRoot,
fiber: options.fiber
};
}
/**
* Mutates the error to include properties expected by Ember CLI.
* See https://github.com/ember-cli/ember-cli/blob/master/docs/ERRORS.md#error-object
* @param {Error} error
*/
function rethrowBuildError(error) {
if (typeof error === 'string') {
throw new Error('[string exception] ' + error);
} else {
error.type = 'Sass Syntax Error';
error.message = error.formatted || error.message;
error.location = {
line: error.line,
column: error.column
};
throw error;
}
}
SassCompiler.prototype.build = function() {
var destFile = path.join(this.outputPath, this.outputFile);
var sourceMapFile = this.sassOptions.sourceMap;
if (typeof sourceMapFile !== 'string') {
sourceMapFile = destFile + '.map';
}
fs.mkdirSync(path.dirname(destFile), { recursive: true });
// old api
if (this.useRender) {
var sassOptions = {
file: includePathSearcher.findFileSync(this.inputFile, this.inputPaths),
includePaths: this.inputPaths,
outFile: destFile
};
Object.assign(sassOptions, this.sassOptions);
return this.renderSass(sassOptions).then(function(result) {
var files = [
fs.promises.writeFile(destFile, result.css)
];
if (this.sassOptions.sourceMap && !this.sassOptions.sourceMapEmbed) {
files.push(fs.promises.writeFile(sourceMapFile, result.map));
}
return Promise.all(files);
}.bind(this)).catch(rethrowBuildError);
}
var sassOptions = {
file: includePathSearcher.findFileSync(this.inputFile, this.inputPaths),
loadPaths: [...this.inputPaths, process.cwd(), path.join(process.cwd(), 'node_modules')],
outFile: destFile
};
Object.assign(sassOptions, this.sassOptions);
// new api
return this.renderSass(sassOptions.file, sassOptions).then(function(result) {
let sourceMapComment = '';
if (this.sassOptions.sourceMap) {
if (this.sassOptions.sourceMapEmbed) {
sourceMapComment = '\n' + '/*# sourceMappingURL=data:application/json;base64,' + Buffer.from(JSON.stringify(result.sourceMap)).toString('base64') + '*/';
} else {
const name = sourceMapFile.replace(/\\/g, '/').split('/').slice(-1)[0];
sourceMapComment = '\n' + '/*@ sourceMappingURL=' + name + '*/';
}
}
var files = [
fs.promises.writeFile(destFile, result.css + sourceMapComment)
];
if (this.sassOptions.sourceMap && !this.sassOptions.sourceMapEmbed) {
files.push(fs.promises.writeFile(sourceMapFile, JSON.stringify(result.sourceMap)));
}
return Promise.all(files);
}.bind(this)).catch(rethrowBuildError);
};
return SassCompiler;
};