3

I'm trying to import SASS (scss) variables in to the javascript in typescript Vue 3 project:

// @/assets/styles/colors.scss
$white: #fff;
// @/assets/styles/_exports.scss
@import "./colors.scss";

:export {
  white: $white;
}
<template>
  <div>
  </div>
</template>
<script lang="ts">
import colors from "@/assets/styles/_export.scss";
</script>
// vue.config.js
module.exports = {
  css: {
    loaderOptions: {
      sass: {
        prependData: '@import "@/assets/styles/colors.scss";'
      }
    }
  }
};

But I'm getting Cannot find module '@/assets/styles/_export.scss' or its corresponding type declarations..

Any ideas pls?

Thanks

2 Answers 2

10

I created a declaration file named shims-scss.d.ts in src directory.

// shims-scss.d.ts

declare module '*.scss' {
  const content: {[className: string]: string};
  export default content;
}

It worked for me to import an object from a scss file like as _exports.scss you wrote.

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to Michael Benares's answer, for Vue 3 project created with Vue CLI, I also had to name the file as "_name.module.scss" (hence ".module") in order to able to import the style to TypeScript.
Without that, the imported object was undefined.

The source - https://stackoverflow.com/a/71942017/12136394

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.