簡體   English   中英

CSS_MODULES:模塊構建失敗,帶有extract-text-webpack-plugin

[英]CSS_MODULES: Module build failed with extract-text-webpack-plugin

當我在生產環境中使用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,但我不知道這里出了什么問題。

我更改了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
                    },
                }
            })
        ]
    })
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM