Webpack配置错误:“无法解析@import”

时间:2019-08-10 01:03:25

标签: webpack sass bootstrap-4

我有一个非常基本的Webpack设置,但是运行 <script> function addMore(e) { e.innerText = "{{ post.summary_rem }}"; } </script> <span onclick="addMore(this)" style="cursor: pointer;" class="text-muted">...more</span> 来捆绑模块时,Webpack会给我以下错误:npm run build

我已经安装了Bootstrap的依赖项,包括"Could not resolve the @import for ~bootstrap/scss/bootstrap.scss"precssautoprefixerpostcss-loaderjquery。然后,我将Bootstrap导入到我的SCSS文件中。

main.css:

popper.js

webpack.config.js:

@import "~bootstrap/scss/bootstrap.scss";

package.json:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const autoprefixer = require('autoprefixer');
const precss = require('precss');

module.exports = {
  mode: 'development',
  entry: {
    app: './src/index.js',
  },
  devtool: 'inline-source-map', 
  devServer: { 
    contentBase: './dist', 
    publicPath: '/',
    hot: true
  },
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader',
          {
            loader: 'postcss-loader',
            options: {
              plugins: function() {
                return [
                  precss,
                  autoprefixer
                ];
              }
            }
          }]
      }
    ]
  },
  plugins: [
      new CleanWebpackPlugin(),
      new HtmlWebpackPlugin({
        filename: 'index.html',
        template: './src/main.html'
      })
    ],
  output: {
    filename: 'js/[name].bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/'
  }
};

我可能会缺少什么?

1 个答案:

答案 0 :(得分:0)

对于将来的读者来说,这是我的webpack.config.js文件中的语法问题。我没有在装载机前添加loader:。完成此操作后,我就可以完美运行npm run buildnpm start

代替:

{
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader',
          {
            loader: 'postcss-loader',
            options: {
              plugins: function() {
                return [
                  precss,
                  autoprefixer
                ];
              }
            }
          }]
      }

我需要这个:

  {
        test: /\.scss$/,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: function() {
                return [
                  require('precss'),
                  require('autoprefixer')
                ];
              }
            }
          },
          {
          loader: 'sass-loader'
          }
        ]
      }