让前端飞

webpack开发- less 插件

2018-11-22  本文已影响0人  Ricoywang

虽然Less 已经用了很长时间,但主要使用的还是他的嵌套功能,最近负责构建相关的工作,稍微了解了些,但到Less定制化插件的部分后,相关的文档少的可怜,只好自己对着源码慢慢尝试。

更新时间
第一次: 2018.11.22 less作为插件时的使用方式
第二次: 2018.12.17 less 内部插件的介绍
class VariablesOutputPlugin {
  constructor(options) {
  }
  install(less, pluginManager, functions) {
    functions.add('pi', function() {
      console.log('xxxx')
      return Math.PI;
    });
  }
}

module.exports = VariablesOutputPlugin;
{
// pluginManager提供一个可以添加文件管理器,后处理器或访问者的持有者。
   install: function(less, pluginManager) {
   },
   // setOptions函数传递字符串。
   setOptions: function(argumentString) {
   },
   // printUsage函数用于解释选项。
   printUsage: function() {
   },
   
   minVersion: [2, 0, 0]
}
new visitors.ImportVisitor(imports, finish).run(root)
less.render
➣ parse.js(addPlugin)
➣ parse.js--end(new Parser)
➣ parser.js(preProcessors)
➣ parser.js--end(new Visitors)
➣ pares-tree.js(postProcessors)
PreProcessor.prototype = {
  var PreProcessor = function () {}
  process: function (src, extra) {
    // src 为文本信息,为内存文本信息,非真实文件信息 例如:
    // @import "../variable.less"; .classname {.mixins()}
    let result = ''
    let currentDirectory = extra.fileInfo.currentDirectory // 当前文件所在文件夹
    let filename = extra.fileInfo.filename // 当前文件地址
    result = src + injected
    return result // 返回新的内存文本信息,本地文件未改变
  }
}
    var RemoveProperty = function(less) {
        this._visitor = new less.visitors.Visitor(this);
    };

    RemoveProperty.prototype = {
        isReplacing: true,
        run: function (root) {
            const variables = root.variables(); // 获取全部变量
            return this._visitor.visit(root);
        },
        visitDeclaration: function (ruleNode, visitArgs) {
            if (ruleNode.name != '-some-aribitrary-property') {
                return ruleNode;
            } else {
                return [];
            }
        }
    };
更新中···
上一篇 下一篇

猜你喜欢

热点阅读