-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
99 lines (90 loc) · 4.04 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
'use strict';
var resolveSassPaths = require('./lib/resolve-sass-paths')
, scss = require('./lib/scss')
, path = require('path')
, fs = require('fs')
, xtend = require('xtend')
, convertSourceMap = require('convert-source-map')
;
var defaultOpts = { debug: true, inlineSourcesContent: true, inlineSourceMap: true };
function inspect(obj, depth) {
console.error(require('util').inspect(obj, false, depth || 5, true));
}
/**
* Resolves paths to all .scss files from the current package and its dependencies.
* The location of these sass files is indicated in the "main.scss" field inside packags.json.
* It then generates the css file including all the rules found in the resolved .scss files.
* Additionally it generates a .css.map file to enable sass source maps if so desired.
*
* @name sassResolve
* @function
* @param {string} root path to the current package
* @param {Object=} opts configure if and how source maps are created and if a css file is written
* @param {boolean=} opts.debug (default: true) generate source maps
*
* @param {boolean=} opts.inlineSourceMap (default: true) inline entire source map info into the .css file
* instead of referring to an external .scss.map file
*
* @param {function=} opts.imports allows overriding how imports are resolved (see: resolveScssFiles and importsFromScssFiles)
*
* @param {string=} opts.mapFileName (default: 'transpiled.css.map') name of the source map file to be included
* at the bottom of the generated css (not relevant when source maps are inlined)
*
* @param cb {Function} function (err, res]) {}, called when all scss files have been transpiled, when nowrite is true,
* res contains generated `css` and `map` (if sourcemaps were enabled and not inlined)
**/
exports = module.exports = function (root, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = xtend(defaultOpts, opts);
var mapFileName = opts.inlineSourceMap ? null : opts.mapFileName || 'transpiled.css.map';
(opts.imports || imports)(root, function (err, src) {
if (err) return cb(err);
scss(src, opts.debug, root, mapFileName, function (err, res) {
if (err) return cb(err);
cb(null, { css: res.css, map: res.conv && res.conv.toObject() });
})
})
}
/**
* Resolves paths to all .scss files from the current package and its dependencies.
* The location of these sass files is indicated in the "main.scss" field inside packags.json.
*
* @name sassResolve::resolveScssFiles
* @function
* @param root {String} full path to the project whose scss files to resolve
* @param cb {Function} called back with a list of paths to .scss files or an error if one occurred
*/
exports.resolveScssFiles = resolveSassPaths;
/**
* Resolves all paths of all .scss files of this project and its dependencies and
* generates the sass imports for them
*
* @name sassResolve::imports
* @function
* @param root {String} full path to the project whose sass files to resolve
* @param cb {Function} called back with imports for the .scss files or an error if one occurred
*/
var imports = exports.imports = function (root, cb) {
resolveSassPaths(root, function (err, scssFiles) {
if (err) return cb(err);
var imports = scssFilesToImports(scssFiles);
cb(null, imports);
});
}
/**
* Creates a .scss import string from the previously resolved sass paths (see: resolveScssFiles)
* This function is called by `imports` and exposed as an advanced api if more manual tweaking is needed.
*
* @name sassResolve::scssFilesToImports
* @function
* @param scssFiles {Array} paths to resolved `.scss` files
* @return {String} of @import statements for each `.scss` file
*/
var scssFilesToImports = exports.scssFilesToImports = function (scssFiles) {
return scssFiles.map(function (f) {
return '@import "' + f + '";';
}).join('\n');
}