缩小/优化NextJS网站

时间:2019-02-19 19:59:52

标签: reactjs webpack babel next.js

我有一个nextjs网站。

下一步不会缩小我的common.js和custom.scss。

我在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()))

我的.babelrc

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

我的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"
    }
}

它应该可以工作,或者我还需要执行更多操作?

2 个答案:

答案 0 :(得分:0)

缩小版本仅在production模式下创建,因为缩小代码花费时间。

为了最小化生产模式,运行NODE_ENV时应将next build设置为生产

您可以通过将npm构建脚本更改为NODE_ENV=production next build来实现。

答案 1 :(得分:0)

我的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;
     }
   });