2

尝试将 vue(和 SFC)添加到我的 webpack 应用程序。<template>and<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 文件就是这个演示文件。我正在尝试将其导入名为“testview”的入口点,其中仅包含:

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

1 回答 1

3

想通了。我不得不sideEffects: false从我的 package.json 中删除。这个问题进一步解释了它。

仍然想知道如何将 .vue 样式提取到我的主样式表 现在,.vue 样式正在提取到单独的样式表(开发和生产)。

于 2018-12-12T06:27:04.877 回答