Webpack You may need an appropriate loader to handle this file type, with sue
Asked Answered
V

1

9

I created my project with Webpack and using Vue.js in development, but I can't inject <style> in the Vue template.

It got me

Module parse failed: Unexpected token (18:5)
You may need an appropriate loader to handle this file type.
| 
| 
> body {
|   background-color: red;
| }

this is my webpack.config.js

  ...
  module: {
    rules: [{
        test: /\.vue$/,
        loader: "vue-loader"
      },
      {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["@babel/preset-env"]
        }
      },
    ]
  },

and also my App.vue

<template>
  <div id="app">
    <counter></counter>
  </div>
</template>

<script>
import Counter from "./app/Counter.vue";
export default {
  name: "app",
  components: {
    counter: Counter
  }
};
</script>

<style>
body {
  background-color: red;
}
</style>
Void answered 12/6, 2018 at 16:58 Comment(2)
Which version of vue-loader is used?Lajoie
@Lajoie my cue-loader is version 15.2.4Void
B
25

Had the same issue. Looking into the docs for vue-loader css needs rules too.

Install the packages vue-style-loader and css-loader

Add the following to your rules section in webpack.config.js:

  {
    test: /\.css$/,
    use: [
      'vue-style-loader',
      'css-loader'
    ]
  },
Boethius answered 13/6, 2018 at 19:10 Comment(2)
I don't understand why this isn't documented somewhere in the Typescript, Vue, or GitHub/vue-loader domains. Spent waaaay to long looking for this solution.Cluny
Same here. Spent waaay too much time looking for this. Browsed through many git issues, other stackoverflow questions, and this is what finally worked.Boles

© 2022 - 2024 — McMap. All rights reserved.