移除编译时的LESS注释

28

是否可以通过JS配置LESS以删除“//注释”?

我希望从输出的less文件中将它们移除。


1
@Ennui 对不起,我不知道//可以作为有效的LESS注释。虽然我认为这很危险。 @RaphaelDDL 我知道LESS是什么。冷静点。 - kleinfreund
@kleinfreund 没问题,事实证明我也错了,因为 // 应该是“静默”注释(不在编译代码中存在)。虽然如果这是真的,我不知道为什么这个问题一开始会存在。我习惯于使用 SASS,它只会将它们转换为普通注释。 - Ennui
1
@Ennui 在 SCSS 中,// 不会转换成普通的 CSS 注释。不确定旧版 SASS 是否也是如此。|| @kleinfreund 为什么 // 会有危险?因为它们也是 JS 的注释吗?但是,/* */ 也是 JS 的注释,那么它也会有危险吗 :P? - RaphaelDDL
4个回答

44

根据文档所述,Less的单行注释//应该保持静默:

单行注释在LESS中也是有效的,但它们是“静默”的,不会出现在编译后的CSS输出中:

// Hi, I'm a silent comment, I won't show up in your CSS
.class { color: white }

请查看LESS官网:http://lesscss.org/#-comments 使用命令行时,-x标志可用于输出压缩后的CSS(这将删除CSS注释),但无论如何,双斜杠不应出现在CSS中。请参见“命令行用法”主题下的http://lesscss.org/#usage

7
关于不使用CLI工具的JS用法,文档中提到:

您可以向编译器传递一些选项:

不幸的是,这些选项没有在任何地方指定。您必须知道在代码中查找的位置,即此处

各种选项(截至本篇撰写时,来自上述链接的blob):

var parseCopyProperties = [
    'paths',            // option - unmodified - paths to search for imports on
    'optimization',     // option - optimization level (for the chunker)
    'files',            // list of files that have been imported, used for import-once
    'contents',         // map - filename to contents of all the files
    'contentsIgnoredChars', // map - filename to lines at the begining of each file to ignore
    'relativeUrls',     // option - whether to adjust URL's to be relative
    'rootpath',         // option - rootpath to append to URL's
    'strictImports',    // option -
    'insecure',         // option - whether to allow imports from insecure ssl hosts
    'dumpLineNumbers',  // option - whether to dump line numbers
    'compress',         // option - whether to compress
    'processImports',   // option - whether to process imports. if false then imports will not be imported
    'syncImport',       // option - whether to import synchronously
    'javascriptEnabled',// option - whether JavaScript is enabled. if undefined, defaults to true
    'mime',             // browser only - mime type for sheet import
    'useFileCache',     // browser only - whether to use the per file session cache
    'currentFileInfo'   // information about the current file - for error reporting and importing and making urls relative etc.
];

并且:

var evalCopyProperties = [
    'silent',         // whether to swallow errors and warnings
    'verbose',        // whether to log more activity
    'compress',       // whether to compress
    'yuicompress',    // whether to compress with the outside tool yui compressor
    'ieCompat',       // whether to enforce IE compatibility (IE8 data-uri)
    'strictMath',     // whether math has to be within parenthesis
    'strictUnits',    // whether units need to evaluate correctly
    'cleancss',       // whether to compress with clean-css
    'sourceMap',      // whether to output a source map
    'importMultiple', // whether we are currently importing multiple copies
    'urlArgs'         // whether to add args into url tokens
];

在使用LESS通过JS的各种Grunt和Gulp插件中,也有一些文档记录它们。

因此,为了回答你的问题,如果不使用compress,就不能真正地去除掉/**/注释,而compress还会进行其他种类的操作。


6

2015年7月更新:尝试使用其他答案建议的-x选项时,我收到了此警告:

压缩选项已被弃用。我们建议您使用专门的CSS压缩器,例如Less-plugin-clean-css。

您可以通过以下方式安装插件

npm install -g less-plugin-clean-css

然后使用--clean-css参数运行lessc命令。


2
无论 -x 是否已被弃用,Q 本身只是 OP 的错误,因为任何 Less 编译器都不会输出 // 注释,无论任何选项。 - seven-phases-max

2
-x选项添加到您的lessc编译命令中将缩小CSS,这应该删除注释。如果没有,您可以通过在编译命令中添加--yui-compress选项来使用YUI CSS压缩器来更好地控制缩小选项,这肯定会删除注释。
就像Raphael在他的回答中所说,//风格的注释不应该出现在编译后的CSS中,因此您的问题一开始就没有太多意义。
所有这些信息都在LESS主页/文档上清楚地说明,因此您可能应该先阅读它们。

虽然LESS很棒,但是它的主页/文档非常糟糕。 - Dmitry Minkovsky
使用JavaScript编译器的文档非常有限,因此您必须知道查看https://github.com/less/less.js/blob/master/lib/less/env.js以获取所有选项。告诉我,您是否在LESS主页上看到任何JS中的“-x”等效内容?没有。这可能就是为什么您实际上没有回答OP的问题,而他的问题是关于JS的。 - Dmitry Minkovsky
这就是关于Less主页的全部内容:http://lesscss.org/#using-less-configuration - Dmitry Minkovsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接