webpack如何提取vue组件的css到独立文件中 -问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

webpack如何提取vue组件的css到独立文件中

杨冬芳 2016-06-13 14:51:13 5463

现在是单独import进来的样式文件可以单独提取出来,而在vue组件中的样式还是跟js打包到一块儿的,要怎么才能把vue组件中的样式也单独提取出来放到一个外部的样式文件中呢?

完整配置信息,可以将Vue组件的CSS提取出来了,webpack.config.js:

var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
  entry: './js/main.js',
  output: {
    path: __dirname,
    filename: '../static/js/app.js',
  },
  module: {
    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue',
      },
      {
        test: /\.js$/,
        loader: 'babel',
      },
      {
        test: /\.(png|jpg)$/,
        loader: 'url?limit=8192',
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader'),
      },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('style-loader', 'css-loader!sass-loader'),
      },
    ],
  },
  vue: {
    loaders: {
      sass: ExtractTextPlugin.extract('vue-style-loader', 'css-loader!sass-loader'),
    },
  },
  plugins: [
    new ExtractTextPlugin('../static/css/style.css', {
      allChunks: true,
    }),
  ],
};

vue组件:

<style lang='sass'>
  @import "../../scss/_css3Module.scss";
  @import "../../scss/_retinaLine.scss";
  .help-title {
    margin: 0;
  }
  .help-title,
  .help-item {
    display: block;
    padding: 10px;
    text-align: center;
    background-color: #f1f1f1;
    margin-bottom: 5px;
  }
</style>

<template lang='jade'>
  h1.help-title 常用Vue组件
  a.help-item
    Geo
  a.help-item(@click='modalShowEvent(true, false)') 两按钮的弹框
  a.help-item(@click='modalShowEvent(true, true)') 一个按钮的弹框
  a.help-item(@click='modalShowEvent(true, false, "one")') 弹框确认事件1
  a.help-item(@click='modalShowEvent(true, true, "two")') 弹框确认事件2
  modal(v-show='modalToggle')
    p(slot='content') 这是外边的内容
</template>

<script lang='babel'>
  import Geo from './common/geolocation.vue';
  import Modal from './common/modal.vue';
  import { modalShowEvent } from '../store/actions/modal';
  import { modalToggle, modalCancelBtn } from '../store/getters';
  export default {
    components: {
      Geo,
      Modal,
    },
    vuex: {
      getters: {
        modalToggle,
      },
      actions: {
        modalShowEvent,
      },
    },
  }
</script>
JavaScript 前端开发
分享到
取消 提交回答
全部回答(1)
  • 杨冬芳
    2019-07-17 19:36:02

    webpack.config.js里要使用如下套路:

    var ExtractTextPlugin = require('extract-text-webpack-plugin');
    module.exports = {
      entry: './js/main.js',
      output: {
        path: __dirname,
        filename: '../static/js/app.js',
      },
      module: {
        loaders: [
          {
            test: /\.vue$/,
            loader: 'vue',
          },
          {
            test: /\.js$/,
            loader: 'babel',
          },
          {
            test: /\.(png|jpg)$/,
            loader: 'url?limit=8192',
          }
        ],
      },
      vue: {
           loaders: {
              css: ExtractTextPlugin.extract('vue-style-loader', 'css-loader', 'sass-loader')
           }
      },
      plugins: [
        new ExtractTextPlugin('../static/css/style.css', {
          allChunks: true,
        }),
      ],
    };
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题
推荐课程