-
Notifications
You must be signed in to change notification settings - Fork 20
/
scss-processor.js
145 lines (123 loc) · 4.97 KB
/
scss-processor.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
import path from 'path';
import fs from 'fs';
import IncludedFile from './included-file';
import ImportPathHelpers from './helpers/import-path-helpers';
import logger from './logger';
export default class ScssProcessor {
constructor(pluginOptions) {
this.fileCache = {};
this.filesByName = null;
this.pluginOptions = pluginOptions;
this.sass = pluginOptions.enableSassCompilation ? require('node-sass') : null;
}
isRoot(inputFile) {
const fileOptions = inputFile.getFileOptions();
if (fileOptions.hasOwnProperty('isImport')) {
return !fileOptions.isImport;
}
return !hasUnderscore(inputFile.getPathInPackage());
function hasUnderscore(file) {
return path.basename(file)[0] === '_';
}
}
shouldProcess(file) {
const sassCompilationExtensions = this.pluginOptions.enableSassCompilation;
if (!sassCompilationExtensions || typeof sassCompilationExtensions === 'boolean') {
return sassCompilationExtensions;
}
return sassCompilationExtensions.some((extension) => file.getPathInPackage().endsWith(extension));
}
process(file, filesByName) {
this.filesByName = filesByName;
try {
this._process(file);
} catch (err) {
const numberOfAdditionalLines = this.pluginOptions.globalVariablesTextLineCount
? this.pluginOptions.globalVariablesTextLineCount + 1
: 0;
const adjustedLineNumber = err.line - numberOfAdditionalLines;
logger.error(`\n/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`);
logger.error(`Processing Step: SCSS compilation`);
logger.error(`Unable to compile ${file.importPath}\nLine: ${adjustedLineNumber}, Column: ${err.column}\n${err}`);
logger.error(`\n/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`);
throw err;
}
}
_process(file) {
if (file.isPreprocessed) return;
if (this.pluginOptions.enableDebugLog) {
console.log(`***\nSCSS process: ${file.importPath}`);
}
const sourceFile = this._wrapFileForNodeSass(file);
const { css, sourceMap } = this._transpile(sourceFile);
file.contents = css;
file.sourceMap = sourceMap;
file.isPreprocessed = true;
}
_wrapFileForNodeSass(file) {
return { path: file.importPath, contents: file.rawContents, file: file };
}
_discoverImportPath(importPath) {
const potentialPaths = [importPath];
const potentialFileExtensions = this.pluginOptions.enableSassCompilation === true ? this.pluginOptions.extensions : this.pluginOptions.enableSassCompilation;
potentialFileExtensions.forEach(extension => potentialPaths.push(`${importPath}.${extension}`));
if (path.basename(importPath)[0] !== '_') {
[].concat(potentialPaths).forEach(potentialPath => potentialPaths.push(`${path.dirname(potentialPath)}/_${path.basename(potentialPath)}`));
}
for (let i = 0, potentialPath = potentialPaths[i]; i < potentialPaths.length; i++, potentialPath = potentialPaths[i]) {
if (this.filesByName.has(potentialPath) || (fs.existsSync(potentialPaths[i]) && fs.lstatSync(potentialPaths[i]).isFile())) {
return potentialPath;
}
}
throw new Error(`File '${importPath}' not found at any of the following paths: ${JSON.stringify(potentialPaths, null, 2)}`);
}
_transpile(sourceFile) {
const sassOptions = {
sourceMap: true,
sourceMapContents: true,
sourceMapEmbed: false,
sourceComments: false,
sourceMapRoot: '.',
omitSourceMapUrl: true,
indentedSyntax: sourceFile.file.getExtension() === 'sass',
outFile: `.${sourceFile.file.getBasename()}`,
importer: this._importFile.bind(this, sourceFile),
includePaths: [],
file: sourceFile.path,
data: sourceFile.contents
};
/* Empty options.data workaround from fourseven:scss */
if (!sassOptions.data.trim()) {
sassOptions.data = '$fakevariable : blue;';
}
const output = this.sass.renderSync(sassOptions);
return { css: output.css.toString('utf-8'), sourceMap: JSON.parse(output.map.toString('utf-8')) };
}
_importFile(rootFile, sourceFilePath, relativeTo) {
try {
if (this.pluginOptions.enableDebugLog) {
console.log(`***\nImport: ${sourceFilePath}\n rootFile: ${rootFile}`);
}
let importPath = ImportPathHelpers.getImportPathRelativeToFile(sourceFilePath, relativeTo);
importPath = this._discoverImportPath(importPath);
let inputFile = this.filesByName.get(importPath);
if (inputFile) {
rootFile.file.referencedImportPaths.push(importPath);
} else {
inputFile = this._createIncludedFile(importPath, rootFile);
}
return this._wrapFileForNodeSassImport(inputFile, importPath);
} catch (err) {
return err;
}
}
_createIncludedFile(importPath, rootFile) {
const file = new IncludedFile(importPath, rootFile);
file.prepInputFile();
this.filesByName.set(importPath, file);
return file;
}
_wrapFileForNodeSassImport(file, importPath) {
return { contents: file.rawContents, file: file.importPath || importPath };
}
};