UNPKG

node-sass-self-contained-importer

Version:

Used to execute sass/scss import and meanwhile resolve relative asset paths declared in the imported file as relative to the importing file. This plugin works as an importer for node-sass, so it supports gulp.js.

238 lines (202 loc) 4.75 kB
/** * 树结点 */ var TreeNode = function(id){ /** * 结点取值(import的字面量取值) */ var value; /** * 结点附加数据(import取值对应资源的绝对路径) */ var attachedData; /** * 引入该结点的结点 * @type {null} */ var previous = null; /** * 一个SCSS/SASS文件可能有多个import * @type {Array} */ var nextSet = []; /** * 获取结点的唯一ID */ this.getId = function(){ return id; }; /** * 设置结点取值(import的字面量取值) * @param {*} _value 结点取值(import的字面量取值) */ this.setValue = function(_value){ value = _value; return this; }; /** * 获取结点取值(import的字面量取值) */ this.getValue = function(){ return value; }; /** * 为结点附加数据,用于为结点关联数据(import取值对应资源的绝对路径) * @param {*} _attachedData 要附加的其它数据(import取值对应资源的绝对路径) */ this.setAttachedData = function(_attachedData){ attachedData = _attachedData; return this; }; /** * 获取为结点附加的数据(import取值对应资源的绝对路径) * @returns {*} 附加的其它数据(import取值对应资源的绝对路径) */ this.getAttachedData = function(){ return attachedData; }; /** * 判断当前结点是否为根结点 */ this.isRootNode = function(){ return null == previous; }; /** * 判断当前结点是否为叶子结点 */ this.isLeafNode = function(){ return null == nextSet || 0 == nextSet.length; }; /** * 获取所在树的根节点 * @returns {TreeNode|null} */ this.getRootNode = function(){ var pointer = this; while(true){ if(null === pointer) return null; if(pointer.isRootNode()) return pointer; pointer = pointer.getPrevious(); } }; /** * 获取上一个结点 */ this.getPrevious = function(){ return previous; }; /** * 设置上一个结点 * @param {TreeNode} _previous 上一个结点 */ this.setPrevious = function(_previous){ if(_previous != this) previous = _previous; return this; }; /** * 获取链表后续结点集合 */ this.getNextSet = function(){ return nextSet.concat(); }; /** * 设置链表后续结点集合 */ this.setNextSet = function(_set){ nextSet = _set || []; return this; }; /** * 插入下一个结点 * @param {TreeNode} _next 下一个结点 */ this.insertNext = function(_next){ if(_next != this){ _next.setNextSet(nextSet); nextSet.forEach(function(node){ node.setPrevious(_next); }); _next.setPrevious(this); nextSet = [_next]; } return this; }; /** * 添加下一个结点至后续结点集合中 * @param {TreeNode} _next 下一个结点 */ this.addNext = function(_next){ if(_next != this){ _next.setPrevious(this); nextSet.push(_next); } return this; }; /** * 获取自当前结点开始(包括当前结点)向前一至第一个结点的路径上的所有节点 * @returns {TreeNode[]} */ this.getNodeArrayFromCurrentNodeToRootNode = function(){ var arr = []; var pointer = this; while(true){ if(null == pointer) break; arr.push(pointer); pointer = pointer.getPrevious(); } return arr; }; /** * 获取自当前结点开始(包括当前结点)向前至第一个结点的路径上的所有节点的取值数组 * @returns {*[]} */ this.getValueArrayFromCurrentNodeToRootNode = function(){ var arr = this.getNodeArrayFromCurrentNodeToRootNode(); return arr.map(function(node){ return node.getValue(); }); }; /** * 根据给定的取值从当前结点向前查找第一个匹配的节点 * @param {*|null} value 要匹配的取值 */ this.findNodeByValueFromCurrentNodeToRootNode = function(_value){ var pointer = this; while(true){ if(null == pointer) return null; if(this.valueComparator(_value, pointer.getValue())) return pointer; pointer = pointer.getPrevious(); } }; /** * 获取自当前结点开始(包括当前结点)向前至第一个结点的路径上的所有节点的构成的路径字符串 * @returns {string} */ this.getPathString = function(){ var arr = this.getNodeArrayFromCurrentNodeToRootNode(); return arr.map(function(node){ return "[" + node.getValue() + "]"; }).join(" -> "); }; }; TreeNode.prototype.valueComparator = function(v1, v2){ return v1 == v2; }; /** * 创建新的树结点 * @param {*} value 结点取值 */ var newTreeNode = (function(){ var id = 0; return function(value){ return new TreeNode(id++).setValue(value); }; })(); TreeNode.newNode = newTreeNode; module.exports = TreeNode;