Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to import a .css file using ES6 modules?

I am trying to import a .css file using ES6 modules but I am getting an error. Is it even possible to import a css file without using a bundler or a transpiler (like webpack)?

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/css"

<html>
    <body>
        <div id="app">
            {{ msg }}
        </div>

        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
        <script type="module">
            import './style.css'

            new Vue({
              el: '#app',
              data: {
                msg: 'Hello World!'
              }
            })
        </script>
    </body>
</html>
like image 419
Liga Avatar asked Jun 08 '19 23:06

Liga


1 Answers

You can't do it with ES6 modules. You can dynamically import it into the HTML though:

<script>
    const url = "./style.css";
    document.head.innerHTML += `<link type="text/css" rel="stylesheet" href=${url}>`;
</script>
like image 96
Jack Bashford Avatar answered Oct 19 '22 22:10

Jack Bashford