Laravel-mix: Unable to remove comments in CSS and/or JS during compiling

Created on 29 Apr 2017  路  5Comments  路  Source: JeffreyWay/laravel-mix

  • Laravel Mix Version: 0.11.3 (npm list --depth=0)
  • Node Version (node -v): 7.9.0
  • NPM Version (npm -v): 4.5.0
  • OS:

Description:

Unable to remove comments in CSS and/or JS during compiling. The only way is to manually edit webpack.config.js to add the parameters:

{ comments: false }

However, webpack.config.js is inside the node_modules directory, so the changes will not be pushed to git.

Steps To Reproduce:

Just run any npm run <script> and comments are not removed.

Most helpful comment

If you want to remove all comments add this to your mix options (laravel-mix: 4)

mix.options({
        cssNano: {
            discardComments: {
                removeAll: true,
            },
        },
    })

P.S. @ruchern this comments not removed because starts with *!, see docs

All 5 comments

@JeffreyWay Any clue to this?

Comments will automatically be stripped when you compile for production. npm run production.

It appears that Font Awesome comments, in the form of this chunk is not being removed.

*!
 *  Font Awesome 4.7.0 by @davegandy - http://fontawesome.io - @fontawesome
 *  License - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License)
 */

However, the one in SimpleMDE is being removed.

/**
 * simplemde v1.11.2
 * Copyright Next Step Webs, Inc.
 * @link https://github.com/NextStepWebs/simplemde-markdown-editor
 * @license MIT
 */

This behaviour is being weird.

@ruchern To anyone that needs a solution to this.
You can provide custom options to the webpack modules in webpack.mix.js, the original options are displayed in /node_modules/laravel-mix/src/Options.js

In laravel 5.4 i provided additional config option like below and all comments were stripped from my minified code, js and css alike.

mix.options({
    postCss: [
        require('postcss-discard-comments')({
            removeAll: true
        })
    ],
    uglify: {
        comments: false
    }
})

You do need the postcss-discard-comments module for this.

I think in the current version of laravel-mix it is done like this (not tested):

mix.options({
    postCss: [
        require('postcss-discard-comments')({
            removeAll: true
        })
    ],
    uglify: {
        uglifyOptions: {
            comments: false
        },
    }
})

If you want to remove all comments add this to your mix options (laravel-mix: 4)

mix.options({
        cssNano: {
            discardComments: {
                removeAll: true,
            },
        },
    })

P.S. @ruchern this comments not removed because starts with *!, see docs

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RomainGoncalves picture RomainGoncalves  路  3Comments

kpilard picture kpilard  路  3Comments

mstralka picture mstralka  路  3Comments

Micaso picture Micaso  路  3Comments

jpmurray picture jpmurray  路  3Comments