forked from plentymarkets/plugin-ceres
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundleSass.js
139 lines (123 loc) · 3.55 KB
/
bundleSass.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
var fs = require('fs');
var path = require('path');
var autoprefixer = require('autoprefixer');
var postcss = require('postcss');
var postcssSCSS = require('postcss-scss');
/*
CLASS ImportTree
*/
var ImportTree = function( filename )
{
this.imports = [];
this.filename = filename;
this.fileContent = [];
};
ImportTree.prototype.parseImports = function()
{
var fileContent = "";
try {
fileContent = fs.readFileSync( this.filename, {encoding: 'utf-8'});
} catch( e )
{
console.log( e );
}
if ( fileContent && fileContent.length > 0 )
{
var _this = this;
this.fileContent = fileContent.split("\n");
this.fileContent
.forEach(function(line, index)
{
var match = /^[^@]?(@import\s?(?:"([^"]+)"|'([^']+)');).*$/.exec(line);
if ( match )
{
var importStmt = match[1];
var importFile = _this._resolveImportPath( match[2] );
_this.imports.push({
line: index,
start: line.indexOf(importStmt),
end: line.indexOf(importStmt) + importStmt.length,
tree: new ImportTree( importFile )
});
}
});
this.imports.forEach(function(importEntry)
{
importEntry.tree.parseImports();
});
}
};
ImportTree.prototype._resolveImportPath = function( importPath )
{
var resolvedPath = path.resolve(
path.dirname(this.filename),
importPath
);
if ( !fs.existsSync(resolvedPath) || !fs.lstatSync(resolvedPath).isFile() )
{
if ( resolvedPath.substr(-5, 5) !== ".scss" )
{
resolvedPath += ".scss";
}
if ( !fs.existsSync(resolvedPath) )
{
resolvedPath = path.resolve(
path.dirname( resolvedPath ),
"_" + path.basename( resolvedPath )
);
}
}
return resolvedPath;
};
ImportTree.prototype.toString = function()
{
if ( this.imports.length > 0 )
{
var self = this;
this.imports.forEach(function(importEntry)
{
var originalLine = self.fileContent[importEntry.line];
var prefix = originalLine.substr(0, importEntry.start);
var affix = originalLine.substr( importEntry.end );
self.fileContent[importEntry.line] = prefix + importEntry.tree.toString() + affix;
});
}
return this.fileContent.join("\n");
};
/*
CLASS: SassResolver
*/
var SassResolver = function ( rootSassFile )
{
this.rootSassFile = path.resolve(
__dirname,
rootSassFile
);
};
SassResolver.prototype.bundle = function( targetFile )
{
targetFile = path.resolve(
__dirname,
targetFile
);
var importTree = new ImportTree( this.rootSassFile );
importTree.parseImports();
var bundleContent = importTree.toString();
var prefixOptions = {
browsers: [
"last 2 versions",
"> 5%",
"Firefox ESR"
]
};
postcss( [autoprefixer(prefixOptions)] )
.process( bundleContent, { from: targetFile, syntax: postcssSCSS })
.then(function( result )
{
fs.writeFileSync( targetFile, result.content );
});
};
console.log("Start bundling scss");
var resolverBootstrap = new SassResolver('resources/scss/Ceres.scss');
resolverBootstrap.bundle('resources/css/ceres.scss');
console.log("=> done!");