【问题标题】:Remove LESS // comments on compile删除 LESS // 编译注释
【发布时间】:2014-01-21 06:41:06
【问题描述】:

是否可以配置LESS通过JS编译时去掉“// cmets”?

我想从输出的less文件中删除它们。

【问题讨论】:

  • @Ennui 抱歉,我不知道 // 是有效的 LESS cmets。虽然我认为这很危险。 @RaphaelDDL 我知道 LESS 是什么。冷静点。
  • @kleinfreund 没问题,事实证明我也错了,因为// 应该是“静默”的 cmets(编译代码中不存在)。虽然如果这是真的,我不知道为什么这个问题首先存在。我习惯了 SASS,它只是将它们转换为普通的 cmets。
  • @Ennui 在 SCSS 上,// 不要转向普通的 CSS cmets。不知道关于旧的 SASS .. || @kleinfreund 为什么 // 很危险?因为它们也是 JS 评论?但是, /* */ 也是一个 JS 注释,所以它也很危险:P?

标签: css less


【解决方案1】:

根据文档所述,Less 的单行 cmets // 应该是静音的:

单行 cmets 在 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 cmets),但无论如何,双斜杠不应出现在 css 上。请参阅“命令行使用”主题中的 http://lesscss.org/#usage

【讨论】:

    【解决方案2】:

    关于没有 CLI 工具的 JS 使用,docs say:

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

    不幸的是,这些选项没有在任何地方指定。你必须知道在哪里看代码,这里是:https://github.com/less/less.js/blob/0c2c1b2ba3036c62be5fc4e6232d3c0493559764/lib/less/env.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
    ];
    

    通过 JS 使用 LESS 的各种 Grunt 和 Gulp 插件也记录了它们。

    因此,要回答您的问题,您不能在不使用 compress 的情况下真正去除 /**/ cmets。它也可以处理所有其他类型的事情。

    【讨论】:

      【解决方案3】:

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

      压缩选项已被弃用。我们建议您使用专用的 css minifier,例如参阅 less-plugin-clean-css。

      你可以安装the plugin

      npm install -g less-plugin-clean-css
      

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

      【讨论】:

      • 好吧,不管-x 是否被弃用,Q 本身只不过是 OP 错误,因为(任何)Less compiler never 输出 // cmets 不管任何选项。
      【解决方案4】:

      在您的lessc 编译命令中添加-x 选项将缩小CSS,这应该会去除cmets。如果不是这样,您可以通过在编译命令中添加--yui-compress 选项来使用YUI CSS compressor 来更好地控制缩小选项,这肯定会去除cmets。

      就像 Raphael 在他的回答中所说的 // 样式 cmets 无论如何都不应该出现在编译的 CSS 中,所以你的问题首先没有多大意义。

      所有这些信息都在LESS homepage / documentation 上明确说明,所以也许你应该先阅读一下。

      【讨论】:

      • 虽然 LESS 很棒,但 LESS 主页/文档却很糟糕。
      • 只有 /very/ 有限的文档说明如何使用 JavaScrtipt 中的编译器,因此您必须了解查看 github.com/less/less.js/blob/master/lib/less/env.js 的所有选项。你告诉我,你有没有在 LESS 主页的任何地方看到 JS 中的 -x 等价物?不,这可能就是为什么你没有真正回答 OP 的问题,这是关于 JS 的。
      • 这几乎是在 less 主页上:lesscss.org/#using-less-configuration
      猜你喜欢
      • 1970-01-01
      • 2012-06-20
      • 2016-03-27
      • 2016-03-05
      • 1970-01-01
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      相关资源
      最近更新 更多