svelte-preprocess-vars

2.0.2 • Public • Published

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).

/svelte-preprocess-vars/

    Package Sidebar

    Install

    npm i svelte-preprocess-vars

    Weekly Downloads

    0

    Version

    2.0.2

    License

    ISC

    Unpacked Size

    150 kB

    Total Files

    51

    Last publish

    Collaborators

    • rob-balfre