正如@harry 已经明确指出的-x
and clean-css
选项也会删除评论。从版本 2 开始,clean-css 选项已移至插件中(npm install -g less-plugin-clean-css
).
从 Less 2 开始你可以使用插件,另请参阅http://lesscss.org/usage/#plugins,这样您就可以编写并使用一个插件来删除多行注释。
Example:
下载 clean-css 并将其解压到您的工作目录中。你可以在以下位置找到 clean-csshttps://github.com/jakubpawlowicz/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());
}
};
Your 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
前面的命令应该为您提供编译后的 CSS,不带注释。