Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 (Cli) not minifying asstet/css files

I created a project with Angular 2 using Angular-cli. When I run

    ng build --prod

I don't have my js and css files minifyed. These files are located in assets/js/ and assets/css/.

How do I minify them? Thanks!

like image 692
mi-ho Avatar asked Dec 10 '16 16:12

mi-ho


People also ask

How do you minify CSS and JS files?

To minify CSS, try CSSNano and csso. To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.

Does ng build minify?

ng build --prod still does NOT minify / uglify / remove comments using angular-cli 9 (and 8) #17550.

How does Minifying a file CSS JS HTML?

To minify JS, CSS and HTML files, comments and extra spaces need to be removed, as well as crunch variable names so as to minimize code and reduce file size. The minified file version provides the same functionality while reducing the bandwidth of network requests.


Video Answer


2 Answers

the assets-folder is included in your build because the folder is listed in the assets-array in angular-cli.json.

everything in the assets-array is copied "as-is" on build.

if you add your css and scripts to the styles and scripts -arrays in angular-cli.json they will be bundled on build so that all your css will be in styles.bundle.js (yes js).

you should not reference the css or js files in your index.html, angular-cli will insert the bundles in index.html.

If you still want the files to live inside the assets folder, remember it is copied "as is" so you can use any tool you like to minify whats in that folder, (e.g. https://www.npmjs.com/package/minifier) angular-cli will not touch it, just copy it on build.

like image 152
Johan Blomgren Avatar answered Oct 01 '22 11:10

Johan Blomgren


Add your styles & js in angular-cli.json

"styles": [
    "assets/css/style.css",
    "assets/css/fonts.css"
],
"scripts": [
    "assets/js/app.js",
    "assets/js/other.js"
]
like image 26
manish.nith Avatar answered Oct 01 '22 13:10

manish.nith