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-loader/lib/listimports.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/stylus-loader/lib/listimports.js')
| -rw-r--r-- | node_modules/stylus-loader/lib/listimports.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/node_modules/stylus-loader/lib/listimports.js b/node_modules/stylus-loader/lib/listimports.js new file mode 100644 index 00000000..bcf9540f --- /dev/null +++ b/node_modules/stylus-loader/lib/listimports.js @@ -0,0 +1,80 @@ +var Parser = require('stylus/lib/parser'); +var Visitor = require('stylus/lib/visitor'); +var nodes = require('stylus/lib/nodes'); + +module.exports = listImports; + +// ImportVisitor is a simple stylus ast visitor that navigates the graph +// building a list of imports in it. +function ImportVisitor() { + Visitor.apply(this, arguments); + this.importPaths = []; +} + +ImportVisitor.prototype = Object.create(Visitor.prototype); +ImportVisitor.prototype.constructor = ImportVisitor; + +ImportVisitor.prototype.visitImport = function(node) { + this.importPaths.push(node.path.first.string); + return node; +}; + +ImportVisitor.prototype.visitRoot = function(block){ + for (var i = 0; i < block.nodes.length; ++i) { + this.visit(block.nodes[i]); + } + return block; +}; + +ImportVisitor.prototype.visitExpression = function(expr) { + for (var i = 0; i < expr.nodes.length; ++i) { + this.visit(expr.nodes[i]); + } + return expr; +}; + +ImportVisitor.prototype.visitCall = function(fn) { + if (fn.name === 'use' || fn.name === 'json') { + this.importPaths.push(fn.args.first.string); + } + return fn; +}; + +ImportVisitor.prototype.visitSelector = function(sel) { + for (var i = 0; i < sel.block.nodes.length; i++) { + this.visit(sel.block.nodes[i]); + } + return sel; +} + +ImportVisitor.prototype.visitBlock = ImportVisitor.prototype.visitRoot; +ImportVisitor.prototype.visitGroup = ImportVisitor.prototype.visitRoot; + +// Returns a list of paths that given source imports. +function listImports(source, options) { + // Store source -> imports work in a cache. The Parser is the most expensive + // part of stylus and we can't use their cache without creating undesired side + // effects later during the actual render. In single run builds this will + // benefit repeated files imported like common styling. In multiple run builds + // this will help stylus import trees when a dependency changes, the higher up + // files won't need to be parsed again. + var cache = options.cache; + if (cache && cache[source]) { return cache[source]; } + + // Current idea here is to silence errors and let them rise in stylus's + // renderer which has more handling so that the error message is more + // meaningful and easy to understand. + try { + var ast = new Parser(source, { cache: false }).parse(); + } catch (e) { + return []; + } + var importVisitor = new ImportVisitor(ast, {}); + importVisitor.visit(ast); + + if (cache) { + cache[source] = importVisitor.importPaths; + } + + return importVisitor.importPaths; +} |
