【问题标题】:CSS_MODULES: Module build failed with extract-text-webpack-pluginCSS_MODULES:模块构建失败,extract-text-webpack-plugin
【发布时间】:2017-09-30 16:33:03
【问题描述】:

当我在生产环境中使用 CSS 模块提取 CSS 时,它会报错,但在开发环境中运行良好。

webpack.base.js

const path = require("path")
const webpack = require("webpack")

module.exports = function(env) {
  return {
    entry: {
      main: path.resolve(__dirname, "../src/main.js"),
    },
    output: {
      path: path.resolve(__dirname, "../dist"),
      sourceMapFilename: "[name].map",
      publicPath: "/",
      filename: (env === "dev") ? "[name].js" : "[name].[hash:16].js",
    },
    resolve: {
      extensions: [".ts", ".js", ".json"],
      modules: [path.join(__dirname, "../src"), "node_modules"]
    },
    module: {
      loaders: [{
          test: /\.jsx?$/,
          use: ["babel-loader"],
          exclude: "/node_modules/"
        },
        {
          test: /\.(png|jpg|gif)$/,
          use: ["url-loader?limit=20000&name=images/[hash:16].[ext]"],
          exclude: "/node_module/"
        },
        {
          test: /\.scss$/,
          use: ["style-loader", "css-loader?modules", "postcss-loader", "sass-loader"],
          exclude: ["/node_module/", path.resolve(__dirname, "../static")]
        },
        {
          test: /\.scss$/,
          use: ["style-loader", "css-loader", "sass-loader"],
          include: path.resolve(__dirname, "../static")
        },
      ],
    },
  }
}

webpack.prod.js

const path = require("path");
const webpack = require("webpack");
const webpackMerge = require("webpack-merge");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const autoprefixer = require("autoprefixer");
const precss = require("precss");
const baseConfig = require("./webpack.base.js");
const config = require("./config.js");
const vendor = config.vendor;

module.exports = function(env) {
  return webpackMerge(baseConfig(env), {
    entry: {
      main: path.resolve(__dirname, "../src/main.js"),
      vendor,
    },
    module: {
      loaders: [{
          test: /\.scss$/,
          loaders: ExtractTextPlugin.extract({
            fallbackLoader: "style-loader",
            use: [
              "css-loader?minimize&modules&importLoaders=1&localIdentName=[name]_[local]_[hash:base64:5]",
              "postcss-loader",
              "sass-loader"
            ]

          }),
          exclude: ["/node_module/", path.resolve(__dirname, "../static")]
        },
        {
          test: /\.scss$/,
          loaders: ExtractTextPlugin.extract({
            fallbackLoader: "style-loader",
            use: [
              "css-loader?minimize",
              "sass-loader"
            ]
          }),
          include: path.resolve(__dirname, "../static")
        },
      ],
    },
    plugins: [
      new webpack.optimize.UglifyJsPlugin({
        compress: {
          warnings: false,
          screw_ie8: true,
          conditionals: true,
          unused: true,
          comparisons: true,
          sequences: true,
          dead_code: true,
          evaluate: true,
          if_return: true,
          join_vars: true,
        },
        output: {
          comments: false,
        },
      }),
      new ExtractTextPlugin({
        filename: "style.[contenthash:16].css",
        disable: false,
        allChunks: true,
      }),
      new HTMLWebpackPlugin({
        template: "src/index.html"
      }),
      new webpack.optimize.CommonsChunkPlugin({
        name: ["vendor", "manifest"]
      }),
      new webpack.DefinePlugin({
        "process.env": {
          NODE_ENV: JSON.stringify("production")
        }
      }),
      new webpack.LoaderOptionsPlugin({
        options: {
          postcss() {
            return [precss, autoprefixer];
          }
        }
      })
    ]
  })
}

webpack.dev.js

const path = require("path");
const webpack = require("webpack")
const webpackMerge = require("webpack-merge");
const OpenBrowserPlugin = require("open-browser-webpack-plugin");
const autoprefixer = require("autoprefixer");
const precss = require("precss");

const baseConfig = require("./webpack.base.js");
const config = require("./config");
const port = config.port;

module.exports = function(env){
    console.log(`
#################################################
  Server is listening at: http://localhost:${config.port} 
#################################################
    `);
    return webpackMerge(baseConfig(env),{
        entry:[
            "react-hot-loader/patch",
            "webpack-dev-server/client?http://localhost:" + port,
            "webpack/hot/only-dev-server",
            path.resolve(__dirname,"../src/main.js"),
        ],
        devtool: "cheap-module-source-map",
        plugins:[
            new webpack.HotModuleReplacementPlugin(),
            new OpenBrowserPlugin({ url: "http://localhost:" + port }),
            new webpack.LoaderOptionsPlugin({
                options:{
                    postcss(){
                        return[precss, autoprefixer];
                    }
                }
            })
        ],
        devServer:{
            hot:true,
            port:config.port,
            historyApiFallback:true,
        }
    })
}

控制台中的错误消息

ERROR in ./~/css-loader?modules!./~/postcss-loader!./~/sass-loader/lib/loader.js!./~/extract-text-webpack-plugin/loader.js?{"omit":1,"remove":true}!./~/style-loader!./~/css-loader?minimize&modules&importLoaders=1&localIdentName=[name]_[local]_[hash:base64:5]!./~/postcss-loader!./~/sass-loader/lib/loader.js!./src/components/Welcome/style.scss
Module build failed:
        display: flex;
              ^
      Invalid CSS after "module.exports": expected "{", was '= {"box":"style_box'
      in C:\Users\Charley\Desktop\myproj\gogogo\src\components\Welcome\style.scss (line 2, column 16)
 @ ./src/components/Welcome/style.scss 4:14-512
 @ ./src/components/Welcome/index.js
 @ ./src/router/components.js
 @ ./src/router/index.js 
 @ ./src/main.js

我的代码有什么问题,我该如何解决?我认为问题是extract-text-webpack-plugin,但我不知道这里出了什么问题。

【问题讨论】:

    标签: javascript css reactjs webpack css-modules


    【解决方案1】:

    我改变了我的 webpack.prod.js

    const path = require("path");
    const webpack = require("webpack");
    const webpackMerge = require("webpack-merge");
    const ExtractTextPlugin = require("extract-text-webpack-plugin"); 
    const HTMLWebpackPlugin = require("html-webpack-plugin");
    const autoprefixer = require("autoprefixer");
    const precss = require("precss");
    const baseConfig = require("./webpack.base.js");
    const config = require("./config.js");
    const vendor = config.vendor;
    
    module.exports = function(env){
        return webpackMerge(baseConfig(env),{
            entry:{
                main:path.resolve(__dirname,"../src/main.js"),
                vendor,
            },
            module:{
               // modified goes here
                rules:[
                    {
                        test:/\.jsx?$/,
                        use:["babel-loader"],
                        exclude:"/node_modules/"
                    },
                    { 
                        test: /\.(png|jpg|gif)$/, 
                        use: ["url-loader?limit=20000&name=images/[hash:16].[ext]"], 
                        exclude: "/node_module/" 
                    },
                    { 
                        test: /\.s?css$/, 
                        use: ExtractTextPlugin.extract({
                                fallback: "style-loader",
                                use: [
                                    "css-loader?minimize&modules&importLoaders=1&localIdentName=[name]_[local]_[hash:base64:5]",
                                    "sass-loader",
                                    "postcss-loader"
                                ]
                             }),
                        exclude: ["/node_module/",path.resolve(__dirname,"../static")]
                    },
                    { 
                        test: /\.s?css$/, 
                        use: ExtractTextPlugin.extract({
                                fallback: "style-loader",
                                use: [
                                    "css-loader?minimize",
                                    "sass-loader",
                                    "postcss-loader"
                                ]
                             }),
                        include: [path.resolve(__dirname,"../static")]
                    }
                ],
            },
            plugins:[
                new webpack.optimize.UglifyJsPlugin({
                    compress: {
                        warnings: false,
                        screw_ie8: true,
                        conditionals: true,
                        unused: true,
                        comparisons: true,
                        sequences: true,
                        dead_code: true,
                        evaluate: true,
                        if_return: true,
                        join_vars: true,
                    },
                    output: {
                        comments: false,
                    },
                }),
                new ExtractTextPlugin({
                    filename:"style.[contenthash:16].css",
                    disable:false,
                    allChunks:true,
                }),
                new HTMLWebpackPlugin({
                    template:"src/index.html" 
                }),
                new webpack.optimize.CommonsChunkPlugin({
                    name: ["vendor","manifest"]
                }),
                new webpack.DefinePlugin({
                    "process.env": { 
                        NODE_ENV: JSON.stringify("production") 
                    }
                }),
                new webpack.LoaderOptionsPlugin({
                    options:{
                        postcss(){
                            return[precss, autoprefixer];
                        },
                        sassLoader: {
                            sourceMap: true
                        },
                    }
                })
            ]
        })
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-09
      • 2016-03-04
      • 2016-09-15
      • 1970-01-01
      • 2019-12-04
      • 2018-01-26
      • 2018-05-29
      • 2017-06-18
      • 2017-07-11
      相关资源
      最近更新 更多