无法在Webpack生产版本中提取Vue SFC样式

时间:2018-12-12 05:58:52

标签: vue.js webpack webpack-4 vue-loader mini-css-extract-plugin

尝试将vue(和SFC)添加到我的webpack应用中。 <template><script>块可以正常工作,但是由于某些原因,<style>块中的样式并未提取出来用于生产构建。

在开发版本中,它将.vue <style>块提取到一个单独的css文件(以入口点命名)。可以,但是我希望他们进入我的主要样式表。

但是无论我做什么,我都无法在生产版本中显示任何.vue样式(在任何文件中)。

这是我的webpack配置的缩写版本:

const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const VueLoaderPlugin = require("vue-loader/lib/plugin");
...

module.exports = (env) => {
  return {
    entry: {
      app: ["./src/polyfills.js", "./src/scss/styles.scss", "./src/app.js"],
      ...
      testview: "./src/js/views/TestView.js"
    },
    output: {
      path: assets,
      filename: "[name].[hash].js",
      publicPath: "/static/"
    },
    resolve: {
      modules: ["node_modules", "src"],
      alias: {
        vue$: "vue/dist/vue.esm.js"
      },
      extensions: ["*", ".js", ".vue"]
    },
    module: {
      rules: [
        {
          test: /\.vue$/,
          loader: "vue-loader"
        },
        {
          test: /\.js?$/,
          exclude: /node_modules/,
          use: [
            {
              loader: "babel-loader",
              options: {
                presets: [
                  [
                    "@babel/preset-env",
                    {
                      targets: {
                        browsers: ["> 1%", "last 2 versions", "ie >= 11"]
                      }
                    }
                  ]
                ],

                plugins: ["@babel/plugin-proposal-class-properties"],
                code: true,
                comments: true,
                cacheDirectory: true,
                babelrc: false
              }
            }
          ]
        },
        {
          test: /\.s?[ac]ss$/,
          use: [
            "vue-style-loader",
            MiniCssExtractPlugin.loader,
            {
              loader: "css-loader",
              options: {
                sourceMap: ifNotProduction()
              }
            },
            {
              loader: "postcss-loader",
              options: {
                ident: "postcss",
                sourceMap: ifNotProduction(),
                plugins: () =>
                  ifProduction([
                    require("autoprefixer")({
                      preset: "default"
                    }),
                    require("cssnano"),
                    require("css-mqpacker")
                  ])
              }
            },
            {
              loader: "sass-loader",
              options: {
                sourceMap: ifNotProduction()
              }
            }
          ]
        }
      ]
    },
    optimization: {
      splitChunks: {
        cacheGroups: {
          commons: {
            name: "commons",
            chunks: "initial",
            minChunks: 2,
            minSize: 0
          },
          styles: {
            name: "styles",
            test: /\.css$/,
            chunks: "all",
            enforce: true
          }
        }
      },
      occurrenceOrder: true 
    },
    plugins: [
      new VueLoaderPlugin(),
      new MiniCssExtractPlugin({
        filename: "style.[hash].css"
      }),
      new HtmlWebpackPlugin({
        hash: true,
        inject: false,
        template: "./src/jinja-templates/base.html.j2",
        filename: `${templates}/base.html.j2`,
      })
    ]
  };
};

我正在使用的.vue文件是这个demo one。我正在尝试将其导入名为“ testview”的入口点,该入口点仅包含:

import Vue from "vue";
import MainContent from "../components/main-content";
let MainComponent = Vue.extend(MainContent);
new MainComponent().$mount("#mainContent");

1 个答案:

答案 0 :(得分:1)

弄清楚了。我必须从我的package.json中删除sideEffects: falseissue对此进行了进一步说明。

还是想知道如何将.vue样式提取到我的主样式表中。到目前为止,.vue样式正在提取到单独的样式表中(开发和生产)。