joel_ace
joel_ace

Reputation: 133

Module parse failed: Unexpected character '@' (1:0) You may need an appropriate loader to handle this file type

I'm having some issues. I keep getting this error

Module parse failed: Unexpected character '@' (1:0) You may need an appropriate loader to handle this file type, currently, no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders @import url("https://cloud.typography.com/7374818/6819812/css/fonts.css");

I've tried some of the suggested solutions I've found online and none seems to work. This is what my Webpack config looks like

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(),
  ],
};

The component looks like this. uilib is a local dependency I'm linking using 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'),
);

The uilib/index.css looks like this

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

Upvotes: 0

Views: 7625

Answers (1)

nishkaush
nishkaush

Reputation: 1548

I feel that ulib folder is perhaps not inside src directory.

Can you try changing your webpack loader config for scss|css to be like so:

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

I am hoping this will let webpack look at project's root which can help it parse the file accordingly.

Also, unless ulib is in node_modules, you can exclude it from loader with exclude: 'node_modules'

Upvotes: 1

Related Questions