Giter Club home page Giter Club logo

postcss-env-function's Introduction

⚠️ PostCSS Environment Variables was moved to @csstools/postcss-plugins. ⚠️
Read the announcement

PostCSS Environment Variables PostCSS Logo

NPM Version CSS Standard Status Build Status Support Chat

PostCSS Environment Variables lets you use env() variables in CSS, following the CSS Environment Variables specification.

@media (max-width: env(--branding-small)) {
  body {
    padding: env(--branding-padding);
  }
}

/* becomes */

@media (min-width: 600px) {
  body {
    padding: 20px;
  }
}

/* when the `importFrom` option is: {
  "environmentVariables": {
    "--branding-small": "600px",
    "--branding-padding": "20px"
  }
} */

Usage

Add PostCSS Environment Variables to your project:

npm install postcss postcss-env-function --save-dev

Use PostCSS Environment Variables to process your CSS:

const postcssEnvFunction = require('postcss-env-function')

postcssEnvFunction.process(YOUR_CSS /*, processOptions, pluginOptions */)

Or use it as a PostCSS plugin:

const postcss = require('postcss')
const postcssEnvFunction = require('postcss-env-function')

postcss([
  postcssEnvFunction(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */)

PostCSS Environment Variables runs in all Node environments, with special instructions for:

Node PostCSS CLI Webpack Create React App Gulp Grunt

Options

importFrom

The importFrom option specifies sources where Environment Variables can be imported from, which might be JS and JSON files, functions, and directly passed objects.

postcssEnvFunction({
  importFrom: 'path/to/file.js' /* module.exports = {
      environmentVariables: {
        '--branding-padding': '20px',
        '--branding-small': '600px'
      }
    } */
})
@media (max-width: env(--branding-small)) {
  body {
    padding: env(--branding-padding);
  }
}

/* becomes */

@media (min-width: 600px) {
  body {
    padding: 20px;
  }
}

Multiple sources can be passed into this option, and they will be parsed in the order they are received. JavaScript files, JSON files, functions, and objects will need to namespace Custom Properties using the environmentVariables or environment-variables key.

postcssEnvFunction({
  importFrom: [
    /* Import from a CommonJS file:
    
    module.exports = {
      environmentVariables: {
        '--branding-padding': '20px'
      }
    } */
    'path/to/file.js',

    /* Import from a JSON file:

    {
      "environment-variables": {
        "--branding-padding": "20px"
      }
    } */
    'and/then/this.json',

    /* Import from an JavaScript Object: */
    {
      environmentVariables: { '--branding-padding': '20px' }
    },

    /* Import from a JavaScript Function: */
    () => {
      const environmentVariables = { '--branding-padding': '20px' }

      return { environmentVariables }
    }
  ]
})

See example imports written in JS and JSON. Currently only valid custom property names (beginning with --) are accepted. Not all valid declaration value names are accepted.

postcss-env-function's People

Contributors

antonio-laguna avatar armano2 avatar danny-andrews avatar dargmuesli avatar ianvs avatar jayenashar avatar jonathantneal avatar romainmenke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

postcss-env-function's Issues

Is it possible to assign env variable to scss variable?

Hi,

I am using wordpress and often I need to use $theme-path across my project to specify the location of assets. I was able to pass the necessary var using your tool into the css file but it only works if I apply this way background: url(env(--theme-path) + '/images/img.jpg') if I try to do something like that

$theme-path: env(--theme-path)
.el {
    background: url($theme-path + "/static/img/background.png");
}

it throws an error. Is there a way around it?

Errors on old filter: alpha() function

I'm getting an error when trying to run postcss-env-fn on some code which uses the old syntax for applying opacity (filter: alpha(opacity=23)).

Code:

.className {
  filter: alpha(opacity=45);
}

Error message:

CssSyntaxError: postcss-env-fn: <css input>:1:1: Unknown word

Generating fallback declaration for safe-area env variables for older browsers

Browsers supporting the env function must support the 4 safe-area-inset-* variables as user-agent env variables (with a value of 0 if they don't need as specific safe area).
But using it in a declaration (for instance to add some margin or padding taking it into account) won't work for older browsers (they will consider the rule as invalid and drop it). This means we need fallback declarations for older browsers:

/* future CSS */
.container {
    padding-left: calc(10px + env(safe-area-inset-left));
}

/* cross-browser CSS */
.container {
    padding-left: calc(10px + 0); /* For older browsers */
    padding-left: calc(10px + constant(safe-area-inset-left)); /* For Safari 11.0 */
    padding-left: calc(10px + env(safe-area-inset-left));
}

Browser compatibility: https://developer.mozilla.org/en-US/docs/Web/CSS/env#Browser_compatibility

In that case, the old browser fallback could even be written as 10px but that could be left to a separate optimizer plugin.

Is this plugin able to handle that ? And if no, is there any plan to support it (or is there any other postcss plugin doing that, that I failed to find) ?

Unknow word when using postcss mixin plugin with postcss-env-function

Hello,

I have an issue when I'm using postcss-env-function with postcss-mixin

In my postcss.config.js I have :
require('postcss-import'), require('tailwindcss'), require('autoprefixer'), require('postcss-mixins'), require('cssnano'), require('postcss-nested'), postcssEnvFunction({ importFrom: './css-env.js', }),

And I also have a mixin file with for example :
@define-mixin lines $lines { display: -webkit-box; -webkit-line-clamp: $lines; -webkit-box-orient: vertical; overflow: hidden; }

And when I run postcss I have this error :
`CssSyntaxError: postcss-env-fn: :1:7: Unknown word

1 | lines $lines
| ^
`

When I removed postcss-env-function I don't have this error.

Any idea how I can resolve this issue ?

Thanks !

cant parser my css file

i get the error

my css file like this

.som-button,.som-button.som-button--error::after,.som-button.som-button--info::after,.som-button.som-button--plain--default::after,.som-button.som-button--plain--error::after,.som-button.som-button--plain--info::after,.som-button.som-button--plain--primary::after,.som-button.som-button--plain--success::after,.som-button.som-button--plain--warn::after,.som-button.som-button--primary::after,.som-button.som-button--success::after,.som-button.som-button--warn::after{border-width:0}.som-button,.som-modal .som-prompt-msgbox,.som-radio .som-radio-input,.som-tabs button{outline:0;-webkit-appearance:none}.som-accordion .som-list-item,.som-button,.som-list-item,.som-list-item.som-list-item-access,.som-modal__btn,.som-radio--button.som-radio__label,.som-radio__label,.som-tab-item{-webkit-tap-highlight-color:transparent}.som-input__elm,.som-nav-bar__title{text-overflow:ellipsis;white-space:nowrap}:root{--primary:#ff571a;--primary-text:var(--primary);--primary-press:#e54e17;--primary-disable:#ff9a76;--primary-selected:#ffeee8;--background-notice:#fff2bd;--background-warning:#fff1f0;--text-title:#1b1c33;--text-body:#5e5e66;--text-caption:#8d8e99;--text-hint:#b0b1b8;--line-b:#d7d7db;--line-c:#ededf0;--press:#e3e3e6;--background-page:#f2f3f5;--background-block:#f7f8fa;--text-white:#fff;--mask:#000;--primary-highlight:var(--primary);--primary-disable-select-color:#ffd2c2;--fs12:12px;--fs14:14px;--fs15:15px;--fs16:16px;--fs18:18px;--fs20:20px;--fs22:22px;--fs24:24px;--border-radius-base:4px;--border-radius-large:6px;--border-radius-small:3px;--font-family:PingFangSC-Regular,Helvetica,"Droid Sans",Arial,sans-serif;--all-transition:all .3s cubic-bezier(.645,.045,.355,1);--fade-linear-transition:opacity 200ms linear;--md-fade-transition:transform 300ms cubic-bezier(.23, 1, .32, 1) 100ms,opacity 300ms cubic-bezier(.23, 1, .32, 1) 100ms;--border-transition-base:border-color .2s cubic-bezier(.645,.045,.355,1);--color-transition-base:color .2s cubic-bezier(.645,.045,.355,1);--background-color-transition-base:background-color .2s cubic-bezier(.645,.045,.355,1);--color-modal:#000000bf;--black:#000;--white:#fff;--font-highlight:var(--white);--success:#1dbf6e;--warning:#ff4040;--link:#4da6ff;--color-info:var(--link);--color-error:var(--warning);--color-warn:#f90;--info-light-bg:rgba(77, 166, 255, .1);--info-button-bg:rgba(77, 166, 255, .8);--info-button-disable:rgba(77, 166, 255, .5);--success-light-bg:rgba(29, 191, 110, .1);--success-button-bg:rgba(29, 191, 110, .8);--success-button-disable:rgba(29, 191, 110, .5);--error-light-bg:#ffedeb;--error-button-bg:rgba(255, 64, 64, .8);--error-button-disable:rgba(255, 64, 64, .5);--warn-light-bg:rgba(255, 153, 0, .08);--warn-button-bg:rgba(255, 153, 0, .15);--warn-button-disable:rgba(255, 153, 0, .4);--fixed-box-shadow:0 2px 12px rgba(0,0,0,.15);--tap-box-shadow:0 -2px 8px rgba(0,0,0,.08);--index-normal:1;--index-back-top:10;--index-top:1000;--index-popper:2000;--accordion-icon-color
...

i think its beacuse of too large, when i slice it, it can work.

Module build failed (from ./node_modules/postcss-loader/src/index.js):
ParserError: Syntax Error at line: 1, column 19
    at /Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/@souche-ui/som-ui/lib/styles/index.css:1:92001
    at Parser.error (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss-values-parser/lib/parser.js:
127:11)
    at Parser.operator (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss-values-parser/lib/parser.
js:183:18)
    at Parser.parseTokens (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss-values-parser/lib/pars
er.js:245:14)
    at Parser.loop (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss-values-parser/lib/parser.js:1
32:12)
    at Parser.parse (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss-values-parser/lib/parser.js:
51:17)
    at getReplacedValue (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss-env-function/index.cjs.j
s:109:37)
    at root.walk.node (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss-env-function/index.cjs.js:
271:35)
    at /Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss/lib/container.js:135:18
    at Rule.each (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss/lib/container.js:101:16)
    at Rule.walk (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss/lib/container.js:131:17)
    at /Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss/lib/container.js:148:24
    at Root.each (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss/lib/container.js:101:16)
    at Root.walk (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss/lib/container.js:131:17)
    at Object.<anonymous> (/Users/cleverboy32/Documents/work/channel-cube-map-mobile/node_modules/postcss-env-function/index.cjs.js:267:14)

Unsupported media range queries

Following the test within the library, here's a reduced test case that will spark the issue:

@media (width < env(--another-custom-width)) {
	body {
		order: 3;
	}
}

@media (width < 64em) {
	body {
		order: 4;
	}
}

Which throws as follows:

postcss-env-fn: <css input>:1:8: Unknown word
CssSyntaxError: <css input>:1:8: Unknown word
    at postcss-env-function/test/basic.css:12:1
    at Input.error (postcss-env-function/node_modules/postcss/lib/input.js:123:16)
    at ValuesParser.unknownWord (postcss-env-function/node_modules/postcss/lib/parser.js:518:22)
    at ValuesParser.unknownWord (postcss-env-function/node_modules/postcss-values-parser/lib/ValuesParser.js:195:15)
    at ValuesParser.other (postcss-env-function/node_modules/postcss-values-parser/lib/ValuesParser.js:110:10)
    at ValuesParser.parse (postcss-env-function/node_modules/postcss-values-parser/lib/ValuesParser.js:135:16)
    at Object.parse (postcss-env-function/node_modules/postcss-values-parser/lib/index.js:21:12)
    at getReplacedValue (postcss-env-function/dist/index.js:101:35)
    at AtRule (postcss-env-function/dist/index.js:240:29)
    at async LazyResult.runAsync (postcss-env-function/node_modules/postcss/lib/lazy-result.js:411:15)
    at async postcss-env-function/node_modules/postcss-tape/dist/index.js:334:18 {
  reason: 'Unknown word',
  source: '(width < env(--another-custom-width))',
  line: 1,
  column: 8,
  input: {
    line: 1,
    column: 8,
    source: '(width < env(--another-custom-width))'
  },
  postcssNode: <ref *1> AtRule {
    raws: {
      before: '\n\n',
      between: ' ',
      afterName: ' ',
      semicolon: false,
      after: '\n'
    },
    type: 'atrule',
    name: 'media',
    parent: Root {
      raws: [Object],
      type: 'root',
      nodes: [Array],
      source: [Object],
      lastEach: 1,
      indexes: [Object],
      [Symbol(isClean)]: true,
      [Symbol(my)]: true
    },
    source: { start: [Object], input: [Input], end: [Object] },
    params: '(width < env(--another-custom-width))',
    nodes: [ [Rule] ],
    proxyCache: [Circular *1],
    [Symbol(isClean)]: true,
    [Symbol(my)]: true
  },
  plugin: 'postcss-env-fn'
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.