reactjs - 导入 axios 后的 Webpack 5 - "Unexpected token: punc (.)"

标签 reactjs webpack axios

我在为生产环境捆绑 webpack 时遇到一个奇怪的问题。

Unexpected token: punc (.)
发生这种情况 只有当 React 组件导入时 axios
import React from "react";
import axios from "axios"; // <---------------

class SimpleComponent extends React.Component {
  render() {

    return (
      <section className="bg-white py-16">
        Simple
      </section>
    )
  }
}

export default SimpleComponent
这会导致以下错误:
$ npm run build

ERROR in static/main.b394a534fa5736fe90cc.js from Terser
Unexpected token: punc (.) [static/main.b394a534fa5736fe90cc.js:18978,6]
    at js_error (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:546:11)
    at croak (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1264:9)
    at token_error (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1272:9)
    at unexpected (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1278:9)
    at statement (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1397:17)
    at _embed_tokens_wrapper (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1322:26)
    at block_ (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:2155:20)
    at statement (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1386:29)
    at _embed_tokens_wrapper (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:1322:26)
    at if_ (/home/franciscocarvalho/code/oss/axios-issue-example-with-webpack5/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:2141:21)
再现
  • 请 fork 以下axios-issue-example-with-webpack5存储库
  • 执行 npm install
  • 运行 npm run build

  • 预期行为
    捆绑后无错误
    环境
  • Axios 版本 [0.21.1]
  • Node.js 版本 [v12.20.1]
  • 其他库版本 [React 17.0.1, Webpack 5.15.0]

  • 配置文件
    const path = require("path")
    const MiniCssExtractPlugin = require('mini-css-extract-plugin');
    const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
    const {CleanWebpackPlugin} = require("clean-webpack-plugin")
    const HtmlWebpackPlugin = require("html-webpack-plugin")
    const Dotenv = require("dotenv-webpack")
    
    const ROOT_DIR = path.resolve(__dirname, "..")
    
        module.exports = {
            mode: "production",
            entry: [
                path.resolve(ROOT_DIR, "./src/index.js"),
                path.resolve(ROOT_DIR, "./src/styles/styles.scss")
            ],
            module: {
                rules: [
                    {
                        test: /\.(js|jsx)$/,
                        exclude: /node_modules/,
                        use: [
                            "babel-loader",
                            {
                                loader: "eslint-loader",
                                options: {
                                    configFile: path.resolve(ROOT_DIR, ".eslintrc")
                                }
                            }
                        ]
                    },
                    {
                        test: /\.(jpe?g|png|gif|ico|svg|woff|woff2|webp)$/i,
                        use: [
                            {
                                loader: "file-loader",
                                options: {
                                    name: "static/[name].[ext]"
                                }
                            }
                        ]
                    },
                    {
                        test: /\.(sa|sc|c)ss$/,
                        use: [
                            MiniCssExtractPlugin.loader,
                            {
                                loader: "css-loader",
                                options: {
                                    importLoaders: true
                                }
                            },
                            'postcss-loader',
                            {
                                loader: "sass-loader",
                                options: {
                                    sassOptions: {
                                        includePaths: [
                                            path.resolve(ROOT_DIR, "node_modules"),
                                            path.resolve(ROOT_DIR, "src/styles/")
                                        ]
                                    }
                                }
                            }
                        ],
                    }
                ]
            },
            resolve: {
                extensions: ["*", ".js", ".jsx", ".scss"],
                fallback: {
                    assert: false,
                    http: false,
                    https: false,
                    zlib: false,
                    tty: false,
                    util: false,
                    fs: false,
                    net: false,
                    stream: false
                }
            },
            plugins: [
                new Dotenv({
                    path: path.resolve(__dirname, "..", "./.env")
                }),
                new CleanWebpackPlugin(),
                new HtmlWebpackPlugin({
                    title: "Advanced React with Webpack Setup",
                    template: path.resolve(__dirname, "..", "./src/index.ejs")
                }),
                new MiniCssExtractPlugin({
                    filename: "static/[name]-[contenthash].css",
                }),
                new PurgeCSSPlugin({
                    paths: glob.sync(`${ROOT_DIR}/src/**/*`, {nodir: true}),
                }),
            ],
            stats: {
                modules: true,
                hash: true,
                assetsSort: "!size",
                children: true
            },
            output: {
                path: path.resolve(__dirname, "..", "dist"),
                chunkFilename: 'static/[name].[hash].js',
                filename: "static/[name].[hash].js",
                chunkLoading: false,
                wasmLoading: false
            },
            optimization: {
                minimize: true,
                minimizer: [
                    // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
                    // `...`,
                    new CssMinimizerPlugin(),
                    /*new UglifyJsPlugin({
                      test: /\.js(\?.*)?$/i,
                      parallel: true,
                    })*/
                    new TerserPlugin({
                        terserOptions: {
                            ecma: 6,
                            compress: {
                                warnings: true
                            },
                            output: {
                                comments: false,
                                beautify: false
                            }
                        }
                    })
                ],
            },
        }
    

    最佳答案

    问题
    就像我说的问题来自 debug已被 webpack 收录在您的构建文件中(node 代码部分)。该代码如下所示:

    function save(namespaces) {
      if (null == namespaces) {
        delete {}.DEBUG;
      } else {    
        {}.DEBUG = namespaces; // that is the invalid statement that `terser` complains about
      }
    }
    
    // Plus, the main file `index.js` of `debug`:
    if (typeof process !== 'undefined' && process.type === 'renderer') {
      module.exports = require('./browser.js');
    } else {
      // Above code is from here
      module.exports = require('./node.js');
    }
    
    
    解决方案
    以上代码 debugnode已添加,因为您还没有告诉 webpack您为 Web 应用程序构建,因此它不会包含该代码。
    所以只要你设置targetweb在您的配置中,它会起作用:webpack.common.js
    module.exports = {  
      target: 'web',
      // ...
    }
    
    还有一件事我发现你的 css typeface-roboto您导入的需要您设置 publicPath作为输出:webpack.prod.js
    module.exports = {
      // ...
      output: {
        // ...
        publicPath: '/',
      },
    }
    

    关于reactjs - 导入 axios 后的 Webpack 5 - "Unexpected token: punc (.)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65744545/

    相关文章:

    javascript - 通过 UI 更改数据库时刷新前端 UI 的最佳实践是什么?

    css - 如何使用 React 组件避免 z-index 错误

    javascript - 在 Gatsby.js 中以编程方式创建多种类型的页面

    javascript - 使用 Jest 和 React JS TestUtils 测试表单

    reactjs - 在 webpack 应用程序中引用 amd 模块(arcgis)

    javascript - React ES6 onSubmit() 不起作用

    webpack - scripts-webpack-plugin✖「wdm」: Error: ENOENT: '/node_modules/jquery.flot.spline/jquery.flot.spline.js'

    javascript - Axios - 如何读取 JSON 响应?

    javascript - Firebase 登录问题

    javascript - axios响应错误: certificate has expired