Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Share variables between CSS and JS with Webpack + HMR

NotificationsYou must be signed in to change notification settings

Coobaha/postcss-variables-loader

Repository files navigation

NPM VersionLicenseGithub IssuesTravis StatusCoveralls

Allows you to share variables between CSS and JS with Webpack and HMR.


This loader transforms special CSS files to JS modules.
  • Shared variables between CSS and JS
  • HMR friendly, CSS changes are applied on the fly.

To be more JS friendly loader will:

  • strippx part from CSS px numbers
  • convert dashes-case to camelCase
  • check for runtime config mutations and access of missing keys (only in dev or es6 mode)

Usage

/* variables.config.css */@custom-media --small-device (max-width:480px)):root {--primary-color: blue;--gutter:30px;}
/* component.css  */@import'colors.config.css'.component {color:var(--primary-color);margin:0var(--gutter);}@media (--small-device) {/* styles for small viewport */}
// component.jsimportvariablesfrom'colors.config.css';console.log(variables);/*  variables = {    primaryColor: 'blue';    gutter: 30;    smallDevice: '(max-width: 480px)'  }*/component.style.color=variables.primaryColor;functionadd5ToGutter(){return5+variables.gutter;}

Install

yarn add --dev postcss-variables-loader
npm install --save-dev postcss-variables-loader

NB: You need to process CSS somehow (egpostcss)and imports inside css (eg viapostcss-import)

Recommended webpack configuration:webpack.config.js with babel-loader

rules: [  {    test: /\.config.css$/,    loader: 'babel-loader!postcss-variables-loader'  },  // dont forget to exclude *.config.css from other css loaders  {    test: /\.css$/,    exclude: /\.config.css$/,    loader: 'css-loader!postcss-loader'  }]

Options

ifproduction.env.NODE_ENV === 'development' it will try to wrap config insideProxy in runtime.It is used to guard from accidental mutations or accessing missing keys.If you dont want this behaviour: passes5=1:

loader: 'postcss-variables-loader?es5=1'

License

About

Share variables between CSS and JS with Webpack + HMR

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp