是否可以通过JS配置LESS以删除“//注释”?
我希望从输出的less文件中将它们移除。
根据文档所述,Less的单行注释//
应该保持静默:
单行注释在LESS中也是有效的,但它们是“静默”的,不会出现在编译后的CSS输出中:
// Hi, I'm a silent comment, I won't show up in your CSS
.class { color: white }
-x
标志可用于输出压缩后的CSS(这将删除CSS注释),但无论如何,双斜杠不应出现在CSS中。请参见“命令行用法”主题下的http://lesscss.org/#usage。您可以向编译器传递一些选项:
不幸的是,这些选项没有在任何地方指定。您必须知道在代码中查找的位置,即此处。
各种选项(截至本篇撰写时,来自上述链接的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
还会进行其他种类的操作。
2015年7月更新:尝试使用其他答案建议的-x
选项时,我收到了此警告:
压缩选项已被弃用。我们建议您使用专门的CSS压缩器,例如Less-plugin-clean-css。
您可以通过以下方式安装插件:
npm install -g less-plugin-clean-css
然后使用--clean-css
参数运行lessc
命令。
-x
是否已被弃用,Q 本身只是 OP 的错误,因为任何 Less 编译器都不会输出 //
注释,无论任何选项。 - seven-phases-max-x
选项添加到您的lessc
编译命令中将缩小CSS,这应该删除注释。如果没有,您可以通过在编译命令中添加--yui-compress
选项来使用YUI CSS压缩器来更好地控制缩小选项,这肯定会删除注释。//
风格的注释不应该出现在编译后的CSS中,因此您的问题一开始就没有太多意义。
//
可以作为有效的LESS注释。虽然我认为这很危险。 @RaphaelDDL 我知道LESS是什么。冷静点。 - kleinfreund//
应该是“静默”注释(不在编译代码中存在)。虽然如果这是真的,我不知道为什么这个问题一开始会存在。我习惯于使用 SASS,它只会将它们转换为普通注释。 - Ennui//
不会转换成普通的 CSS 注释。不确定旧版 SASS 是否也是如此。|| @kleinfreund 为什么//
会有危险?因为它们也是 JS 的注释吗?但是,/* */
也是 JS 的注释,那么它也会有危险吗 :P? - RaphaelDDL