Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function parseStylus(stylusCode) {
let AST, rules, stylesheet, hash, selectors, element;
stylus.render(stylusCode, { filename: 'source.css' }, (err, css) => {
// throws parse errors
if (err) {
throw new Error('Indentation error');
}
// Generate css AST
AST = parser.parse(css, { source: 'css' });
// Get the root selector
selectors = AST.stylesheet.rules[0] !== undefined ? AST.stylesheet.rules[0].selectors : null;
// Set the root element
element = getParentNode(selectors);
// Style rules
rules = AST.stylesheet.rules;
// Browserify + Transforms
if (path.extname(file) == '.js' || path.extname(file) == '.coffee') {
var b = browserify().add(assetsDir + file);
try { b.transform(require('caching-coffeeify')) } catch (e) {};
try { b.transform(require('jadeify')) } catch (e) {};
try { b.transform(require('uglifyify')) } catch (e) {};
b.bundle(function(err, buf) {
if (err) return errExit(err);
fs.writeFileSync(publicDir + file.split('.')[0] + '.js', buf);
});
}
// Stylus + Sqwish
try { var stylus = require('stylus'); } catch (e) {};
if (stylus && path.extname(file) == '.styl') {
stylus.render(fs.readFileSync(assetsDir + file).toString(), {
filename: assetsDir + file
}, function(err, css) {
if (err) return errExit(err);
try { var css = require('sqwish').minify(css) } catch (e) {};
fs.writeFileSync(publicDir + file.split('.')[0] + '.css', css);
});
}
});
var convertToStylus = function (resolve, reject, data) {
stylus.render(data.source, function (error, css) {
if (error) {
// index starts at 1
var lineMatch = error.message.match(/stylus:(\d+)/);
var line = parseInt(lineMatch[1], 10) || 0;
var msg = error.message.match(/\n\n(.+)\n$/);
if (line > 0) {
line = line - 1;
}
var errors = {
line: line,
ch: null,
msg: msg[1]
};
resolve({
errors: [errors],
result: null
files.forEach(function (fileName) {
var fileContents = grunt.file.read(fileName);
var newFileName = fileName.match(/(.*).styl/)[1] + ".css";
var newFilePath = path.join(config.dest, newFileName);
// NB: this isn't actually asynchronous; it just uses callbacks for some reason. That's what makes it possible
// to push onto `newFileUrls`.
stylus.render(fileContents, { filename: newFileName }, function (err, css) {
if (err) {
grunt.warn(err.message);
} else {
grunt.file.write(newFilePath, css);
grunt.log.writeln("CSS file created at \"" + newFilePath + "\".");
newFileUrls.push(urlize(newFilePath));
}
});
});
transform: function (path, styl, send) {
stylus.render(styl, {filename: path, compress: true}, function (err, css) {
if (err) {
throw err;
}
// Stylus keeps newlines and inline comments, even when compressing. Let's get rid of them:
css = css.replace(/\n/g, '');
css = css.replace(/\/\*.+\*\//g, '');
// Send css to client
send(css, {'Content-Type': 'text/css'});
});
}
});
load(id) {
if (id.endsWith('.styl')) {
const style = readFileSync(id, { encoding: 'utf8' });
stylusLib.render(style, function(err, css) {
if (err) throw err;
result = css;
});
return 'var stylus=1;';
}
return null; // other ids should be handled as usually
},
writeBundle(bundle) {
root_locals.stylus = function(obj) {
var css, k, v, _results;
_results = [];
for (k in obj) {
v = obj[k];
_results.push(css = require('stylus').render(v, {
filename: k
}, function(err, css) {
if (err) {
throw err;
}
return routes.push({
verb: 'get',
path: k,
handler: css,
contentType: 'css'
});
}));
}
return _results;
};
root_locals.helper = function(obj) {
module.exports = function (raw, cb) {
try {
var stylus = require('stylus')
} catch (err) {
return cb(err)
}
stylus.render(raw, {}, cb)
}
function renderToCSS({ src, filename, options }) {
return stylus.render(src, { filename });
}