Webpack 3:使用sass-loader和ExtractTextPlugin不起作用

时间:2017-09-12 14:59:45

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

我一直在尝试在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
        }
    }
};

2 个答案:

答案 0 :(得分:3)

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

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

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

来自webpack documentation - Rule.enforce

  

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

     

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

     

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

为了能够在HTML中正确使用CSS,您还需要在css-loader之后使用sass-loader,因为EJS希望在这个地方使用JavaScript,而不是简单的CSS。要求将成为:

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

最好在实际应用中导入index.scss,而不是html-webpack-plugin使用的模板。

答案 1 :(得分:0)

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

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

问题在于我添加了sass规则 AFTER exclude一个,并且在评论中清楚地说明了(hah)之后的每个加载器都不会工作

现在它看起来像这样,它的工作原理:

      {
        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]',
        },
      },

希望这有助于将来。

相关问题