当前位置: 首页 > 知识库问答 >
问题:

模块分析失败:意外字符“@”(1:0)您可能需要适当的加载程序来处理此文件类型

危文乐
2023-03-14
const path = require('path');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const devMode = process.env.NODE_ENV !== 'production';

module.exports = {
  entry: {
    main: path.resolve(__dirname, './src/App.tsx'),
  },

  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'public'),
    sourceMapFilename: "[name].js.map"
  },

  externals: {
    'react': 'React',
    'react-dom': 'ReactDOM',
  },

  devtool: 'source-map',

  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        exclude: /node_modules/,
        include: [
          path.resolve(__dirname, './src'),
        ],
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
        },
      },
      {
        test: /\.(scss|css)$/,
        include: path.resolve(__dirname, './src'),
        use: [
          MiniCssExtractPlugin.loader,
          'style-loader',
          'css-loader',
          {
            loader: "postcss-loader",
            options: {
              plugins: () => [
                require("autoprefixer")()
              ],
            },
          },
          'sass-loader',
        ],
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|otf|svg)$/,
        loader: 'url-loader',
        options: {
          limit: 100000,
        }
      },
    ],
  },
  resolve: {
    extensions: ['.js', '.jsx', 'json', 'ts', 'tsx', '.scss'],
    alias: {
      src: path.resolve(__dirname, './src/'),
    },
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './public',
    port: 3030,
    open: true,
    compress: true,
    hot: true,
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
    },
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: devMode ? '[name].css' : '[name].[hash].css',
      chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
    }),
    new CleanWebpackPlugin(),
    new webpack.HotModuleReplacementPlugin(),
  ],
};

组件如下所示。UILIB是我正在使用Yarn Link链接的本地依赖项

import React from 'react';
import ReactDOM from 'react-dom';
import { Header, Footer } from 'uilib';
import 'uilib/index.css';

export const App: React.FC<{}> = () => (
  <>
    <Header/>
    <div>Hello in oct</div>
    <Footer/>
  </>
);

ReactDOM.render(
  <App />,
  document.getElementById('root'),
);

uilib/index.css如下所示

@import url("https://cloud.typography.com/7374818/6819812/css/fonts.css");
.header {
  height: 70px; 
}

共有1个答案

宋昊然
2023-03-14

我觉得ulib文件夹可能不在src目录中。

您是否可以尝试将SCSSCSS的webpack加载器配置更改为如下所示:

      {
        test: /\.(scss|css)$/,
        include: path.resolve(__dirname) // <---- leave it __dirname or remove the prop completely
        ....
        ....
      }

我希望这能让webpack查看Project的root,这样可以帮助它相应地解析文件。

 类似资料: