Remove LESS // comments on compile

前端 未结 4 647
心在旅途
心在旅途 2021-02-02 09:28

Is it possible to configure LESS to remove \"// comments\" when it compiles via JS?

I want to remove them from the outputted less file.

相关标签:
4条回答
  • 2021-02-02 10:05

    Less' single-line comments // are supposed to be silent, as per documentation states:

    Single-line comments are also valid in LESS, but they are ‘silent’, they don’t show up in the compiled CSS output:

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

    See at LESS' website: http://lesscss.org/#-comments

    -x flag works on command line to output minified CSS (which will strip CSS comments), but in any way, double slash shouldn't appear on css. See http://lesscss.org/#usage at 'Command-line usage' topic.

    0 讨论(0)
  • 2021-02-02 10:16

    Regarding JS usage without the CLI tool, the docs say:

    You may pass some options to the compiler:

    Unfortunately, those options aren't specified anywhere. You have to know where to look in the code, which is here: https://github.com/less/less.js/blob/0c2c1b2ba3036c62be5fc4e6232d3c0493559764/lib/less/env.js

    The various options are (at time of this writing, from the above-linked 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.
    ];
    

    and:

    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
    ];
    

    They are also documented a bit by the various Grunt and Gulp plugins that use LESS via JS.

    So, to answer your question, you can't really strip out /**/ comments without using compress. Which does all other sorts of things too.

    0 讨论(0)
  • 2021-02-02 10:19

    2015-July update: when trying the -x option as the other answers suggested, I got this warning:

    The compress option has been deprecated. We recommend you use a dedicated css minifier, for instance see less-plugin-clean-css.

    You can install the plugin with

    npm install -g less-plugin-clean-css
    

    Then run lessc with the --clean-css argument.

    0 讨论(0)
  • 2021-02-02 10:25

    Adding the -x option to your lessc compile command will minify the CSS, which should strip out comments. In the event it doesn't you can get more control over the minification options by using the YUI CSS compressor by adding the --yui-compress option to your compile command, which definitely strips out comments.

    Like Raphael said in his answer // style comments should not be present in compiled CSS anyway so your question doesn't make a lot of sense in the first place.

    All of this information is clearly stated on the LESS homepage / documentation, so maybe you should just read that first.

    0 讨论(0)
提交回复
热议问题