【问题标题】:Webpack 3: Use sass-loader and ExtractTextPlugin doesn't workWebpack 3:使用 sass-loader 和 ExtractTextPlugin 不起作用
【发布时间】:2018-02-21 02:43:00
【问题描述】:

我一直在尝试在 webpack 中使用 sass-loader,并按照说明进行操作 -> https://github.com/webpack-contrib/extract-text-webpack-plugin#extracting-sass-or-less 但这不起作用。

谁能帮帮我?

存储库

https://github.com/gpincheiraa/boolean-html-js-exercises/tree/dev

错误

ERROR in   Error: Child compilation failed:
  Module build failed: Error: "extract-text-webpack-plugin" loader is used without the corresponding plugin, refer to https://github.com/webpack/extract-text-webpack-plugin for the usage example

  - Error: "extract-text-webpack-plugin" loader is used without the corresponding plugin, refer to https://github.com/webpack/extract-text-webpack-plugin for the usage example

依赖关系

node v6.11.1
npm 5.3.0

├── babel-cli@6.26.0
├── babel-loader@7.1.2
├── babel-plugin-transform-class-properties@6.24.1
├── babel-polyfill@6.26.0
├── babel-preset-es2015@6.24.1
├── babel-preset-stage-3@6.24.1
├── css-loader@0.28.7
├── extract-text-webpack-plugin@3.0.0
├── html-loader@0.5.1
├── html-webpack-plugin@2.30.1
├── markdown-loader@2.0.1
├── node-sass@4.5.3
├── sass-loader@6.0.6
├── style-loader@0.18.2
├── webpack@3.5.6
└── webpack-dev-server@2.7.1

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: [
      "./index.js"
    ],
    output: {
        path: __dirname + "/dist",
        filename: "index.bundle.js"
    },
    module: {
        rules: [
            { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" },
            { test: /\.md$/, loaders: [ "html-loader", "markdown-loader" ] },
            { test: /\.scss$/,
              use: ExtractTextPlugin.extract({
                  fallback: 'style-loader',
                  use: ['css-loader', 'sass-loader']
              })
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('style.css'),
        new HtmlWebpackPlugin({
          template: 'index.html',
          inject: 'body'
        })
    ],
    devtool: "eval-source-map",
    devServer: {
        filename: "index.bundle.js",
        contentBase: "./",
        port: 3000,
        publicPath: "/",
        stats: {
            colors: true
        }
    }
};

【问题讨论】:

    标签: webpack sass sass-loader extract-text-plugin


    【解决方案1】:

    问题来自commented style code in your index.htmlindex.htmlhtml-webpack-plugin 处理,由于某种原因,它仍然尝试处理 require 调用(line 9line 11)。原因可能是 html-webpack-plugin 的自定义 EJS 加载器。

    最简单的解决方案是从index.html 中完全删除注释代码。

    通过导入.scss 文件,您配置的规则将应用于该文件。但似乎实际的extract-text-webpack-plugin 实例在该过程中不可用。您在这些 require 调用中使用了内联加载程序,但您配置的规则仍将应用于此。为防止应用其他加载器,您可以在导入前加上 !

    来自webpack documentation - Rule.enforce

    可以通过在请求中添加前缀 ! 来省略(覆盖)所有普通加载器。

    通过在请求中添加前缀-!,可以省略(覆盖)所有普通和预加载程序。

    通过在请求中添加前缀!!,可以省略(覆盖)所有普通、后置和预加载器。

    为了能够在您的 HTML 中正确使用 CSS,您还需要在 sass-loader 之后使用 css-loader,因为 EJS 在此位置需要 JavaScript,而不是纯 CSS。要求将变为:

    <%= require("!css-loader!sass-loader!\./sass/index.scss") %>
    

    在您的实际应用程序中导入index.scss而不是html-webpack-plugin使用的模板也会更好。

    【讨论】:

    【解决方案2】:

    我来到这里是因为我使用 Webpack 3 + Sass + React 的配置也不起作用。

    但是,就我而言,这个问题非常愚蠢。我必须说我使用create-react-app 工具创建了这个项目,它设置了一个具有安静复杂/完整配置的webpack.config.dev.js 文件。

    问题是我添加了 sass 规则 AFTER exclude 规则,它在 cmets 中明确表示(哈哈)之后的每个加载程序都将无法工作。

    所以它现在看起来像这样并且它可以工作:

          {
            test: /\.scss$/,
            use: ExtractTextPlugin.extract({
              fallback: 'style-loader',
              use: [require.resolve('css-loader'), require.resolve('sass-loader')],
            })
          },
          {
            exclude: [/\.js$/, /\.html$/, /\.json$/],
            loader: require.resolve('file-loader'),
            options: {
              name: 'static/media/[name].[hash:8].[ext]',
            },
          },
    

    希望这对将来的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 2018-12-08
      • 2018-04-13
      • 2017-05-19
      • 2017-07-15
      • 2021-04-18
      • 2020-07-24
      • 1970-01-01
      • 2017-08-18
      相关资源
      最近更新 更多