【发布时间】:2014-11-22 07:25:45
【问题描述】:
有没有办法在 LESS 中创建 silent 多行 cmets?我想要与 //comment 相同的行为,但对于多行字符串。
【问题讨论】:
-
您是否尝试使用
-x选项或clean-css进行压缩/缩小? -
我很想知道是否也有无声的多行评论
有没有办法在 LESS 中创建 silent 多行 cmets?我想要与 //comment 相同的行为,但对于多行字符串。
【问题讨论】:
-x 选项或clean-css 进行压缩/缩小?
正如@harry 已经阐明的那样,-x 和clean-css 选项也删除了 cmets。从版本 2 开始,clean-css 选项已移至插件 (npm install -g less-plugin-clean-css)。
从 Less 2 开始,您可以使用插件,另请参阅 http://lesscss.org/usage/#plugins,因此您可以编写和使用删除多行 cmets 的插件。
示例:
下载 clean-css 并将其解压缩到您的工作目录中。您可以在 https://github.com/jakubpawlowicz/clean-css 找到 clean-css(这将创建一个名为 clean-css-master 的子旧版本)
比创建你的插件,调用这个文件less-plugin-remove-comments.js:
var getCommentsProcessor = require("./comments-processor");
module.exports = {
install: function(less, pluginManager) {
var CommentsProcessor = getCommentsProcessor(less);
pluginManager.addPostProcessor(new CommentsProcessor());
}
};
您的comment-processor.js 可能包含以下内容:
var cleaner = require('./clean-css-master/lib/text/comments-processor');
module.exports = function(less) {
function CommentProcessor(options) {
this.options = options || {};
};
CommentProcessor.prototype = {
process: function (css) {
var commentsProcessor = new cleaner('*', false);
css = commentsProcessor.escape(css);
return css;
}
};
return CommentProcessor;
};
最后你应该能够运行以下命令:
lessc --plugin=./less-plugin-remove-comments.js index.less
前面的命令应该会给你编译后的没有 cmets 的 CSS。
【讨论】: