Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

rob-balfre/svelte-preprocess-vars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

svelte-preprocess-vars

Svelte preprocessor to inject JS variables into component styles.

Takes:

module.exports = {
  primaryColor: 'blue',
};

and converts:

<style>
  h1 {
    color: var(--primaryColor);
  }
</style>

to:

<style>
  h1 {
    color: blue;
  }
</style>

Setup:

npm install svelte-preprocess-vars --save-dev

Example webpack svelte-loader config:

const path = require('path');
const webpack = require('webpack');
const sveltePreprocessVars = require('svelte-preprocess-vars');

const variablesPath = path.resolve('shared-variables.js');

module.exports = {
  module: {
    rules: [
      {
        test: /\.(html)$/,
        use: {
          loader: 'svelte-loader',
          options: {
            hydratable: true,
            hotReload: true,
            preprocess: sveltePreprocessVars(variablesPath),
            externalDependencies: [variablesPath]  // ensures svelte-loader recompiles the components if the variables change
          }
        }
      }
    ]
  },
};

Webpack and svelte-loader won't watch these variables unless you include them in your 'externalDependencies' (see example above).

About

Svelte preprocessor to inject JS variables into component styles

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published