diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/stylus/lib/functions/use.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/stylus/lib/functions/use.js')
| -rw-r--r-- | node_modules/stylus/lib/functions/use.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/node_modules/stylus/lib/functions/use.js b/node_modules/stylus/lib/functions/use.js new file mode 100644 index 00000000..da28e4d0 --- /dev/null +++ b/node_modules/stylus/lib/functions/use.js @@ -0,0 +1,74 @@ +var utils = require('../utils') + , path = require('path'); + +/** +* Use the given `plugin` +* +* Examples: +* +* use("plugins/add.js") +* +* width add(10, 100) +* // => width: 110 +*/ + +module.exports = function use(plugin, options){ + utils.assertString(plugin, 'plugin'); + + if (options) { + utils.assertType(options, 'object', 'options'); + options = parseObject(options); + } + + // lookup + plugin = plugin.string; + var found = utils.lookup(plugin, this.options.paths, this.options.filename); + if (!found) throw new Error('failed to locate plugin file "' + plugin + '"'); + + // use + var fn = require(path.resolve(found)); + if ('function' != typeof fn) { + throw new Error('plugin "' + plugin + '" does not export a function'); + } + this.renderer.use(fn(options || this.options)); +}; + +/** + * Attempt to parse object node to the javascript object. + * + * @param {Object} obj + * @return {Object} + * @api private + */ + +function parseObject(obj){ + obj = obj.vals; + for (var key in obj) { + var nodes = obj[key].nodes[0].nodes; + if (nodes && nodes.length) { + obj[key] = []; + for (var i = 0, len = nodes.length; i < len; ++i) { + obj[key].push(convert(nodes[i])); + } + } else { + obj[key] = convert(obj[key].first); + } + } + return obj; + + function convert(node){ + switch (node.nodeName) { + case 'object': + return parseObject(node); + case 'boolean': + return node.isTrue; + case 'unit': + return node.type ? node.toString() : +node.val; + case 'string': + case 'literal': + return node.val; + default: + return node.toString(); + } + } +} |
