Remove LESS // comments on compile

前端 未结 4 654
心在旅途
心在旅途 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: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.

提交回复
热议问题