简体   繁体   English

当 sass 文件中有 url 时,Webpack 模块构建失败

[英]Webpack module build failing when there are url's in the sass file

I'm having trouble with the module build for my style.scss file.我在为我的 style.scss 文件构建模块时遇到问题。 It seems that the image URLs that I've referenced are causing the build to fail and I'm not sure why.似乎我引用的图像 URL 导致构建失败,我不知道为什么。 I thought it may be due to lacking an appropriate loader, so I installed the resolve-url-loader, which hasn't made any difference.我认为这可能是由于缺少合适的加载器,所以我安装了 resolve-url-loader,它没有任何区别。 I've looked at many other instances where people have experienced something similar and none of their solutions have worked for me.我看过许多其他情况,人们经历过类似的事情,但他们的解决方案都没有对我有用。

The Error:错误:

ERROR in ./src/sass/style.scss
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleNotFoundError: Module not found: Error: Can't resolve '../public/fonts/montserrat-v15-latin-600.eot' in 'C:\Users\Kaleshe\Local Sites\soundtruism\app\public\wp-content\themes\soundtruism\src\sass'
    at C:\Users\Kaleshe\Local S

File Structure:文件结构:

文件结构

webpack.config.js: webpack.config.js:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');

module.exports = {
  context: __dirname,
  entry: {
    frontend: ['./src/index.js', './src/sass/style.scss'],
    customizer: './src/customizer.js'
  },
  output: {
    path: path.resolve(__dirname, 'public'),
    filename: '[name]-bundle.js'
  },
  mode: 'development',
  devtool: 'cheap-eval-source-map',
  module: {
    rules: [
      {
        enforce: 'pre',
        exclude: /node_modules/,
        test: /\.jsx$/,
        loader: 'eslint-loader'
      },
      {
        test: /\.jsx?$/,
        loader: 'babel-loader'
      },
      {
        test: /\.(sass|scss)$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-sass-loader', 'resolve-url-loader', 'sass-loader']
      },
      {
        test: /\.svg$/,
        loader: 'svg-sprite-loader',
        options: {
          extract: true,
          spriteFilename: 'svg-defs.svg'
        }
      },
      {
        test: /\.(jpe?g|png|gif)\$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'images/',
              name: '[name].[ext]'
            }
          },
          'img-loader'
        ]
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: '[name].css'
    }),
    new BrowserSyncPlugin({
      open: 'external',
      host: 'soundtruism.local',
      files: '**/*.php',
      proxy: 'soundtruism.local',
    })
  ],
  optimization: {
    minimizer: [new UglifyJsPlugin(), new OptimizeCssAssetsPlugin()]
  }
};

PATH FROM THE ERROR You are referencing the fonts folder located in public directory.错误的路径您正在引用位于public目录中的 fonts 文件夹。 Where as the, the sass file is in src/sass .其中, sass文件位于src/sass中。 You need to use ../../public to access the public directory.您需要使用../../public访问public目录。

The first ../ will take you to src and the next ../ will take you to / .第一个../将带您到src ,下一个../将带您到/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM