2

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

2 回答 2

3

问题来自您的index.html. index.html由 the 处理,并且html-webpack-plugin由于某种原因它仍然尝试处理 require 调用(第 9行和第 11 行)。原因可能是html-webpack-plugin.

最简单的解决方案是从index.html.

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

webpack 文档 -Rule.enforce

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

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

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

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

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

最好index.scss在您的实际应用程序中导入,而不是在html-webpack-plugin.

于 2017-09-12T21:25:03.323 回答
0

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

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

问题是我在sass规则之后添加了规则exclude,它在评论中清楚地表明(哈哈)之后的每个加载器都将不起作用。

所以它现在看起来像这样并且它有效:

      {
        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-01-12T20:41:36.580 回答