首页
学习
活动
专区
圈层
工具
发布

web 5配置
EN

Stack Overflow用户
提问于 2022-02-28 15:11:51
回答 1查看 124关注 0票数 0

最近,我更新了我所有的依赖项,包括(Webpack 4.44 -> Webpack 5.69,react -router 5 -> react router dom 6和react 16 -> react 17)。

我试图配置webpack,但我遇到了一些错误,我无法弄清楚。我要提到的是,我正在尝试使用webpack 5的资产模块来替换我的旧加载程序(url-加载器、img-加载程序和文件加载程序)。

但是我得到了以下错误

/Users/shahid.ahmad/src/assets/favicon-32.ico: Html插件:错误:子编译失败:模块构建失败(来自./node_node/babel-

/lib/index.js):SyntaxError: SyntaxError意外字符‘。(1:0)

同时我也得到了这个

在资产asset/resource|/Users/shahid.ahmad/node_modules/babel-loader/lib/index.js!/Users/shahid.ahmad/src/assets/favicon-32.ico的呈现过程中无法读取未定义属性的

ChunkRenderError

还有这个

./src/

//FuturaPT-Demi.otfModuleBuild中的错误(来自./node_/babel-/lib/index.js):SyntaxError: SyntaxError意外字符'‘。(1:4)

这就是webpack配置的样子。

代码语言:javascript
复制
const path = require('path');
const HTML = require('html-webpack-plugin');
const webpack = require('webpack');
const dotenv = require('dotenv');
const fs = require('fs');

module.exports = (env = {}) => {
  
  const currentPath = path.join(__dirname);
  
  const basePath = `${currentPath}/.env`;
  
  const envPath = env.environment ? `${basePath}.${env.environment}` : basePath;
  
  const finalPath = fs.existsSync(envPath) ? envPath : basePath;
  
  const fileEnv = dotenv.config({ path: finalPath }).parsed;
  
  const envKeys = Object.keys(fileEnv).reduce((prev, next) => {
  
    prev[`process.env.${next}`] = JSON.stringify(fileEnv[next]);
    return prev;
  }, {});

  return {
    mode: env.production ? 'production' : 'development',
    entry:  { 
      application : './src/index.tsx' 
    },
    devtool: env.production ? false : 'source-map',
    cache:{
      type : 'filesystem'
    },
    module: {
      rules: [
        {
          test: /.(mjs)?$/,
          resolve: {
            fullySpecified: false, 
          },
        },        
        {
          test: /.(js|jsx|ts|tsx)?$/,
          exclude: /node_modules/,
          use: [{
            loader : 'babel-loader'
          }],
        },
        {
          enforce: 'pre',
          exclude: /node_modules/,
          test: /\.js$/,
          loader: 'source-map-loader',
        },
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: 'javascript/auto',
        },
        {
          test: /\.(png|svg|jpg|jpeg|gif|ico)$/i,
          type: 'asset/resource',
        },
        {
          test: /\.(woff|woff2|eot|ttf|otf)$/i,
          type: 'asset/inline',
        },
        {
          test: /\.(html)$/,
          use: {
            loader: 'html-loader',
          },
        },
        {
          test: /\.(css)$/,
          use: ['style-loader', 'css-loader'],
        },
      ],
    },
    resolve: {
      extensions: ['*', '.mjs', '.js', '.jsx', '.scss', '.css', '.html', '.ts', '.tsx'],
      modules: [path.resolve(__dirname, '.'), 'node_modules'],
    },
    output: {
      path: path.join(__dirname, '/dist'),
      publicPath: '/',      
      chunkFilename: '[name].[chunkhash].js',
    },
    devServer: {            
      historyApiFallback: true,
      open: true,
      compress: true,
      hot: true,            
    },
    plugins: [
      new HTML({
        template: './src/template.html',
        inject: true,
        hash: true,
      }),
      new webpack.DefinePlugin(envKeys),
    ],
    optimization: {
      runtimeChunk: 'single',
      splitChunks: {
        chunks: 'all',
        minChunks: 1,
        cacheGroups: {
          defatulVendor: {
            test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,
            name: 'vendor',
            chunks: 'all',
          },
        },
      },
    },
  };
};

我读了很多书,但仍然找不出问题的所在。任何帮助都将不胜感激,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-02-28 15:17:09

我认为您的构建失败是因为您没有为.ico文件类型指定加载程序。

考虑为它和其他静态资源指定一个加载程序,并更新regex以包含它。

代码语言:javascript
复制
- test: /\.(png|svg|jpg|jpeg|gif)$/i,
+ test: /\.(png|svg|jpg|jpeg|gif|ico)$/i,
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71297078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档