1

I am making a Vue plugin that contains SFC with its own styles. I am using webpack4 to bundle. For production I use MiniCssExtractPlugin to extract CSS from SFC to separate file. Everything builds up correctly, style.css is created in dist folder and my plugin is working but seems like css from file is not loaded. Is there a step which I am missing? Thank you

main.js

import SearchBar from "./components/Searchbar.vue";
const defaultOptions = {};
export let Search = {
  install(Vue, options) {
    let userOptions = { ...defaultOptions, ...options };
    let props = { ...SearchBar.props };
    Object.keys(userOptions).forEach(k => {
      props[k] = { default: userOptions[k] };
    });
    Vue.component("SearchBarComponent", { ...SearchBar, props });
  }
};

Searchbar.vue

<template>
 <div class="search">My search template</div>
</template>
<script>
 export default{
 }
</script>
<style>
  .search{
    background-color:blue;
   }
</style>

webpack.config.js

let path = require("path");
const VueLoaderPlugin = require("vue-loader/lib/plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  entry: "./src/main.js",
  output: {
    path: path.resolve(__dirname, "./dist"),
    libraryTarget: "commonjs2",
    publicPath: "/dist/",
    filename: "search.min.js"
  },
  plugins: [
    new VueLoaderPlugin(),
    new MiniCssExtractPlugin({
      filename: "style.css"
    })
  ],
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: "vue-loader"
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: "file-loader",
        options: {
          name: "[name].[ext]?[hash]"
        }
      },
      {
        test: /\.css$/,
        use: [
          process.env.NODE_ENV !== "production"
            ? "vue-style-loader"
            : MiniCssExtractPlugin.loader,
          "css-loader"
        ]
      }
    ]
  }
};

In my project, I just install my plugin from npm repository then I use

import {Search} from "my-search-plugin";
Vue.use(Search);

to import plugin, and then call

<SearchBarComponent>

wherever I need. The component appear successfully but styles and not applied.

4

0 回答 0