Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minify / Optimize NextJS site

I have a nextjs site.

My common.js and my custom.scss don't be minified by next.

I tried the next one in next.config.js:

const withSass = require('@zeit/next-sass')
const withOptimizedImages = require('next-optimized-images');
const withTypescript = require('@zeit/next-typescript')
module.exports = withSass({minified:true},withOptimizedImages(withTypescript()))

My .babelrc

{
    "presets": [
        "next/babel",
        "@zeit/next-typescript/babel",
        "minify"
    ]
}

My tsconfig.json

{
    "compilerOptions": {
        "allowJs": true,
        "allowSyntheticDefaultImports": true,
        "jsx": "preserve",
        "lib": [
            "dom",
            "es2017"
        ],
        "module": "esnext",
        "moduleResolution": "node",
        "noEmit": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "preserveConstEnums": true,
        "removeComments": true,
        "skipLibCheck": true,
        "sourceMap": true,
        "strict": true,
        "target": "esnext"
    }
}

It should work or i have to implement some thing more?

like image 771
Jordi Castillo Avatar asked Feb 19 '19 19:02

Jordi Castillo


2 Answers

my next.config.js:

const withCSS = require("@zeit/next-css");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

module.exports = 
    withCSS({
      webpack(config, options) {
         config.optimization.minimizer = [];
         config.optimization.minimizer.push(new OptimizeCSSAssetsPlugin({}));

      return config;
     }
   });
like image 113
JIm Avatar answered Sep 26 '22 16:09

JIm


Minified version is created only on production mode, because it take time to minify the code.

In order to minified your production mode, you should set NODE_ENV to production when running next build.

You can accomplish it by changing the npm build script to: NODE_ENV=production next build.

like image 25
felixmosh Avatar answered Sep 23 '22 16:09

felixmosh