Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vite-plugin-sass-dts

Package Overview
Dependencies
Maintainers
0
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-sass-dts

A plugin that automatically creates a type file when using the css module type-safely.

  • 1.3.29
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11K
increased by17.98%
Maintainers
0
Weekly downloads
 
Created
Source

vite-plugin-sass-dts ⚡ Welcome 😀

GitHub Actions status

vite-plugin-sass-dts

A plugin that automatically creates a type file when using the CSS module type-safely.

Demo

Install

npm i -D vite-plugin-sass-dts

For version 1.3.27 or later, please use the latest sass-embedded package; we have confirmed that it does not work with the sass package.

Options

ParameterTypeDescription
enabledModestring[]Create d.ts files for css modules of file extension css, sass, scss included in the project at build time.

Valid enumerations 'development' and 'production'. By default it is enabled only for development.
We recommend that you turn off the flag once you have created the d.ts file, as it will take a long time to build. (default [development])
global.generatebooleanOutputs the common style set in additionalData of preprocessorOptions as a global type definition file.
global.outputFilePathstringSpecify the file that outputs the global common style with an absolute path.Relative paths will be supported.
typeName.replacementstring | (fileName: string) => stringType name can be changed to any value. (default is the classname key as a string. e.g. theClassName: 'theClassName';)
outputDirstringAn absolute path to the output directory. If undefined, declaration files will be generated in the source directories. `)
sourceDirstringAn absolute path to your source code directory. The plugin will replace this path with outputDir option when writing declaration files. `)
esmExportbooleanSpecify dts export type. If enabled, going to use ESM style export export default .... Otherwise export = ....
prettierFilePathstringSpecify the path to the prettier configuration file.

Add it to vite.config.ts

import { defineConfig } from 'vite'
import sassDts from 'vite-plugin-sass-dts'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [sassDts()],
})

Usage

You can create a dts file by saving the scss file during development. You can check the usage example when the following options are set. Prepare the vite.config.ts file with the following options and start it in development mode.

[vite.config.ts]

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import sassDts from 'vite-plugin-sass-dts'
import path from 'path'

export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles" as common;`,
        importer(...args) {
          if (args[0] !== '@/styles') {
            return
          }

          return {
            file: `${path.resolve(__dirname, './src/assets/styles')}`,
          }
        },
      },
    },
  },
  plugins: [
    react(),
    sassDts({
      enabledMode: ['development', 'production'],
      global: {
        generate: true,
        outputFilePath: path.resolve(__dirname, './src/style.d.ts'),
      },
      sourceDir: path.resolve(__dirname, './src'),
      outputDir: path.resolve(__dirname, './dist'),
    }),
  ],
})
npm run dev

Then save the following file ...

[src/assets/styles/_index.scss]

.row {
  display: flex;
}

[src/App.module.scss]

.header-1 {
  background-color: common.$primary;
  &.active {
    background-color: black;
  }
}

.input {
  @media (min-width: 768px) {
    max-width: 370px;
  }
}

Saving the scss file creates a d.ts file in the outputDir hierarchy.

Note: if outputDir is not set, declaration files are output to the same directory as the source files.

[dist/App.scss.d.ts]

import globalClassNames, { ClassNames as GlobalClassNames } from './style.d'
declare const classNames: typeof globalClassNames & {
  readonly 'header-1': 'header-1'
  readonly active: 'active'
  readonly input: 'input'
}
export = classNames

The optional global type definition is output to the output path of the common style specified in the options.

[src/style.d.ts]

declare const classNames: {
  readonly row: 'row'
}
export = classNames

Principles of conduct

Please see the principles of conduct when building a site.

License

This library is licensed under the MIT license.

Keywords

FAQs

Package last updated on 15 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc