next.js - Nextjs 未缩小(生产中的事件)

标签 next.js minifiedjs

我真的快疯了,因为我到处都读到 nextjs 会缩小 js(我认为还有 SCSS/CSS)文件,但我似乎不可能看到我的文件被缩小。

我在这里复制我的“next.config.js”文件:

const withPlugins = require('next-compose-plugins');
const withSass = require('@zeit/next-sass');
const withImages = require('next-images');
const withFonts = require('next-fonts');

const nextConfig = {
  compress: true,
  serverRuntimeConfig: {
  },
  publicRuntimeConfig: {
    // Will be available on both server and client
    nodeENV: process.env.NODE_ENV
  },

};

module.exports = withPlugins(
  [
    withSass({
      cssModules: true
    }),
    withImages,
    withFonts
  ], nextConfig
);

在我的“.bash_profile”文件中,我将 NODE_ENV 设置为 production 并验证了它
echo $NOVE_ENV

这在我的服务器/远程以及我的本地机器上......只是为了确定。

我还在此处添加了我的 package.json 文件:
{
  "name": "docker-nextjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "start": "NODE_PATH=. NODE_ENV=production ENV=production next build && node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@react-google-maps/api": "^1.8.6",
    "@zeit/next-css": "^1.0.1",
    "@zeit/next-sass": "^1.0.1",
    "axios": "^0.19.2",
    "express": "^4.17.1",
    "flatpickr": "^4.6.3",
    "gsap": "^3.2.6",
    "include-media": "^1.4.9",
    "next": "^9.3.1",
    "next-compose-plugins": "^2.2.0",
    "next-fonts": "^1.0.3",
    "next-images": "^1.3.1",
    "next-routes": "^1.4.2",
    "node-sass": "^4.13.1",
    "paper": "^0.12.4",
    "react": "^16.13.1",
    "react-async-script-loader": "^0.3.0",
    "react-dom": "^16.13.1",
    "react-ga": "^2.7.0",
    "react-loadable": "^5.5.0",
    "sass": "^1.26.3",
    "simplex-noise": "^2.4.0",
    "swiper": "^5.3.6",
    "three": "^0.115.0",
    "uglifyjs-webpack-plugin": "^2.2.0"
  }
}


我正在使用自定义 SSR 服务器运行我的网站:
const express = require( 'express' );
const next    = require( 'next' );

// Import middleware.
const routes = require( './routes' );

// Setup app.
const app     = next( { dev: 'production' !== process.env.NODE_ENV } );
const handle  = app.getRequestHandler();
const handler = routes.getRequestHandler( app );
app.prepare()
  .then( () => {

    // Create server.
    const server = express();
    server.use('/static', express.static('/next'))

    // Use our handler for requests.
    server.use( handler );

    // Don't remove. Important for the server to work. Default route.
    server.get( '*', ( req, res ) => {
      return handle( req, res );
    } );

    // Get current port.
    const port = process.env.PORT || 3000;

    // Error check.
    server.listen( port, err => {
      if ( err ) {
        throw err;
      }

      // Where we starting, yo!
      console.log( `Ready on port ${port}...` );
    } );
  } );

我真的很困惑....

最佳答案

使用下面的代码缩小。

const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const TerserPlugin = require('terser-webpack-plugin')
webpack: (config, options) => 
{
    config.optimization.minimizer = [];
    config.optimization.minimizer.push(new OptimizeCSSAssetsPlugin({}));
    config.optimization.minimizer.push(new TerserPlugin());
}

关于next.js - Nextjs 未缩小(生产中的事件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60932621/

相关文章:

javascript - Nextjs 中带有组件的样式化组件

express - 无法使用 nextjs 代理到 nodejs/express 套接字 io 服务器

javascript - 如何创建源代码中没有 HTML 的网站

javascript - 使用 minified.js 更新文本内容

reactjs - 配置多个下一个插件 : withMDX, withBundleAnalyzer

reactjs - Next JS 导出到静态 HTML 总是在刷新时重定向到主页 "/"

javascript - 我可以使用 sourcemaps 将缩小代码的堆栈跟踪转换为人类可读的堆栈跟踪吗?

node.js - NextJS 组件

javascript - 什么是 'double-dot' [例如。 5..toFixed()] 我在缩小的 js 中看到了吗?