vue-media-loader

Enable `media` attribute on Vue SFC styles

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
vue-media-loader
47150.1.36 years ago6 years agoMinified + gzip package size for vue-media-loader in KB

Readme

vue-media-loader
Enable media attribute on Vue SFC styles.

Requirements

  • vue-loader >= 15

Installation

$ npm i -D vue-media-loader

Usage

Add vue-loader and vue-media-loader in your webpack config. Note that you need to insert vue-media-loader between any CSS preprocessors and css-loader.
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {
  // ... Other configs goes here ...

  module: {
    rules: [
      {
        test: /\.vue$/,
        use: 'vue-loader'
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'vue-media-loader',
          'sass-loader'
        ]
      }
    ]
  },

  plugins: [new VueLoaderPlugin()]
}

Then you can write media attribute on <style> block in your .vue files.
<template>
  <p class="message">This text color will be changed if you change the window width.</p>
</template>

<style>
.message {
  color: #333;
}
</style>

<style media="(max-width: 767px)">
.message {
  color: #33a;
}
</style>

<style media="(max-width: 320px)">
.message {
  color: #a33;
}
</style>

The above code is equivalent with the following:
<template>
  <p class="message">This text color will be changed if you change the window width.</p>
</template>

<style>
.message {
  color: #333;
}

@media (max-width: 767px) {
  .message {
    color: #33a;
  }
}

@media (max-width: 320px) {
  .message {
    color: #a33;
  }
}
</style>

License

MIT