Giter Site home page Giter Site logo

postcss-modules-values-replace's Introduction

PostCSS Modules Values Replace

PostCSS plugin to work around CSS Modules values limitations.

Replaces CSS Modules @values just as postcss-modules-values does, but without help of css-loader, so it could be used before other PostCSS plugins like postcss-calc.

Example:

/* constants.css */
@value unit: 8px;
@value footer-height: calc(unit * 5);

/* my-components.css */
@value unit, footer-height from "./constants.css";
@value component-height: calc(unit * 10);

.my-component {
  padding: unit;
  margin-top: footer-height;
  height: component-height;
}

yields my-components.css:

 @value unit, footer-height from "./constants.css";
 @value component-height: calc(8px * 10);

 .my-component {
   padding: 8px;
   margin-top: calc(8px * 5);
   height: calc(8px * 10);
 }

and leads to export of following values to JS:

{
    "unit": "8px",
    "footer-height": "calc(8px * 5)",
    "component-height": "calc(8px * 10)",
    ...
}

See how to export computed values in usage with calc example below.

Usage

Place it before other plugins:

postcss([ require('postcss-modules-values-replace'), require('postcss-calc') ]);

When using from webpack, pass its file system in postcss.config.js form:

module.exports = (ctx) => ({
   plugins: [
     require('postcss-modules-values-replace')({fs: ctx.webpack._compiler.inputFileSystem}),
     require('postcss-calc'),
  ]
});

See PostCSS docs for other examples for your environment.

Configuration params

fs Object

File system to use. To make it faster in webpack pass its file system to plugin. Cached Node's file system is used by default.

resolve Object

enhanced-resolve's configuration object, see there for possible options and defaults.

noEmitExports boolean

When enabled @value rules/declarations will be removed from the emitted output

Input:

@value myBrandColor blue;
@font-face {}

body { background: myBrandColor }

Output:

@font-face {}

body { background: blue }

preprocessValues boolean

When enabled, permit plugins defined earlier in the PostCSS pipeline to modify @value declarations before they are recorded by this plugin.

importsAsModuleRequests boolean

When enabled, value imports will be resolved as module requests, in line with css-loader's resolution logic as of 2.0.0. If your code is written with pre-2.0 import syntax, and utilises postcss-modules-tilda for compatibility, this option is not required.

replaceInSelectors boolean

When enabled, value usage within rule selectors will also be replaced by this plugin.

atRules Array<string>

You can pass a list of at-rules in which @value's should be replaced. Only @media rules will be processed by default. Note that passed array isn't merged with default ['media'] but overwrites it, so you'll need to include all the rules you want to be processed.

postcss([
  require('postcss-modules-values-replace')({ atRules: ['media', 'container']  })
]);

Input:

@value $tables from './breakpoints.css';

@container (width >= $tablet) {}

Output:

@container (width >= 768px) {}

calc() and @value

To enable calculations inside @value, enable media queries support in postcss-calc:

postcss([
  require('postcss-modules-values-replace'),
  require('postcss-calc')({mediaQueries: true})
])

or via postcss-cssnext:

postcss([
  require('postcss-modules-values-replace'),
  require('postcss-cssnext')({features: {calc: {mediaQueries: true}}})
])

Example with calc enabled:

/* constants.css */
@value unit: 8px;
@value footer-height: calc(unit * 5);

/* my-components.css */
@value unit, footer-height from "./constants.css";
@value component-height: calc(unit * 10);

.my-component {
  padding: unit;
  margin-top: footer-height;
  height: component-height;
}

yields my-components.css:

 @value unit, footer-height from "./constants.css";
 @value component-height: 80px;

 .my-component {
   padding: 8px;
   margin-top: 40px;
   height: 80px;
 }

and leads to export of following values to JS:

{
    "unit": "8px",
    "footer-height": "40px",
    "component-height": "80px",
    ...
}

Other computations and @value

postcss-calc and postcss-color-function are known to work inside @value as they traverse media queries. Experience with other plugins may differ if they ignore media queries.

Extracting values for programmatic use

This plugin provides to postcss a custom messages object with type: 'values'. The values property of that object will contain all the extracted values with all substitution performed (i.e. for values that reference other values).

See modules-values-extract for an example of how this can be used.

Environment

Node.js 6.5 or above is recomended.

License

ISC

With thanks

Code is mostly taken from postcss-modules-values by Glen Maddern, Mark Dalgleish and other contributors.

postcss-modules-values-replace's People

Contributors

ackwell avatar alexhisen avatar dependabot[bot] avatar github-actions[bot] avatar greenkeeper[bot] avatar nickw444 avatar princed avatar wearymonkey avatar

Stargazers

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

Watchers

 avatar

postcss-modules-values-replace's Issues

Why does this module depend on node >= 6.5?

As far as I can see, the postcss-modules-values doesn't need any node engine version. Is there a reason why node version is required for this plugin? I'm using this with nodejs 5.12.0, and it compiles just fine. Can this engine dependency be removed?

An in-range update of babel-core is breaking the build 🚨

Version 6.26.0 of babel-core just got published.

Branch Build failing 🚨
Dependency babel-core
Current Version 6.25.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As babel-core is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of babel-cli is breaking the build 🚨

Version 6.26.0 of babel-cli just got published.

Branch Build failing 🚨
Dependency babel-cli
Current Version 6.24.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As babel-cli is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

It breaks on β€œspace toggle hack”

See https://css-tricks.com/a-dry-approach-to-color-themes-in-css/ and https://lea.verou.me/2020/10/the-var-space-hack-to-toggle-multiple-values-with-one-custom-property/

Example code:

:root {
  --OFF: ;
}

error:

TypeError: Cannot read property 'raws' of undefined
    at /Users/filippriabchun/IdeaProjects/ring-ui/src/global/variables.css:2:3
    at Parser.space (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss-values-parser/lib/parser.js:387:25)
    at Parser.parseTokens (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss-values-parser/lib/parser.js:223:14)
    at Parser.loop (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss-values-parser/lib/parser.js:132:12)
    at Parser.parse (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss-values-parser/lib/parser.js:51:17)
    at replaceValueSymbols (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss-modules-values-replace/index.js:22:60)
    at /Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss-modules-values-replace/index.js:193:20
    at /Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss/lib/container.js:55:18
    at Rule.each (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss/lib/container.js:41:16)
    at Rule.walk (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss/lib/container.js:52:17)
    at /Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss/lib/container.js:60:24
    at Root.each (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss/lib/container.js:41:16)
    at Root.walk (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss/lib/container.js:52:17)
    at /Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss-modules-values-replace/index.js:190:8
    at async LazyResult.runAsync (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss/lib/lazy-result.js:396:11)
    at async Object.loader (/Users/filippriabchun/IdeaProjects/ring-ui/node_modules/postcss-loader/dist/index.js:97:14)

Upgrade to PostCSS 8

Consider upgrading the plugin to postcss 8 as it offers performance and smaller node_modules size.

Export regexes as the rules to reuse by other tools

Hi, me again.

Can we expose these regexes?

const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
const matchValueDefinition = /(?:\s+|^)([\w-]+)(:?\s+)(.+?)(\s*)$/g;
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
const matchPath = /"[^"]*"|'[^']*'/;

I think it will be helpful if other tools could reuse those patterns of @value for their purpose (for example validating postcss rules, or preprocess the file before this values-replace plugin).

An in-range update of postcss is breaking the build 🚨

Version 6.0.9 of postcss just got published.

Branch Build failing 🚨
Dependency postcss
Current Version 6.0.8
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

postcss is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes 6.0.9
  • Improve error message for plugin with old PostCSS (by @igoradamenko).
Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

How to remove values completely?

postcss-modules-values-replace replaces values in sheets but leaves them in the head of file, so after compilation I have:

@value spacing2XS from '@platform-ui-mobile/spacing/spacing.css';
@value spacing3XS from '@platform-ui-mobile/spacing/spacing.css';
@value colorLightBlue100 from '@platform-ui-mobile/colors/primitives.css';
@value colorLightBlue300 from '@platform-ui-mobile/colors/primitives.css';
@value colorBlue300 from '@platform-ui-mobile/colors/primitives.css';
@value colorYellow100 from '@platform-ui-mobile/colors/primitives.css';
@value colorGray400 from '@platform-ui-mobile/colors/primitives.css';
@value colorBlack300 from '@platform-ui-mobile/colors/primitives.css';
@value colorWhite100 from '@platform-ui-mobile/colors/primitives.css';
@value colorGray100 from '@platform-ui-mobile/colors/primitives.css';

.wrapper {
    width: 100%;
    display: flex;
    justify-content: center;
    height: 24px;
}
...

How to remove them completely? I dont want to ship my npm package with this useless code.

Incorrect node version requirement

In fact plugin requires Node 7.5+ becouse of async functions.

In my case built have falled with Node 6.11.4

Error text
/opt/teamcity-agent/work/1942dc01ed884f70/node_modules/postcss-modules-values-replace/index.js:72
[16:10:27][Step 3/4] const walk = async (requiredDefinitions, walkFile, root, result) => {
[16:10:27][Step 3/4]                    ^
[16:10:27][Step 3/4] 
[16:10:27][Step 3/4] SyntaxError: Unexpected token (
[16:10:27][Step 3/4]     at createScript (vm.js:56:10)
[16:10:27][Step 3/4]     at Object.runInThisContext (vm.js:97:10)
[16:10:27][Step 3/4]     at Module._compile (module.js:542:28)
[16:10:27][Step 3/4]     at Object.Module._extensions..js (module.js:579:10)
[16:10:27][Step 3/4]     at Module.load (module.js:487:32)
[16:10:27][Step 3/4]     at tryModuleLoad (module.js:446:12)
[16:10:27][Step 3/4]     at Function.Module._load (module.js:438:3)
[16:10:27][Step 3/4]     at Module.require (module.js:497:17)
[16:10:27][Step 3/4]     at require (internal/module.js:20:19)
[16:10:27][Step 3/4]     at module.exports (/opt/teamcity-agent/work/1942dc01ed884f70/webpack/postcss.config.js:34:9)
[16:10:27][Step 3/4]     at Object.<anonymous> (/opt/teamcity-agent/work/1942dc01ed884f70/.storybook/webpack.config.js:125:26)
[16:10:27][Step 3/4]     at Module._compile (module.js:570:32)
[16:10:27][Step 3/4]     at Object.Module._extensions..js (module.js:579:10)
[16:10:27][Step 3/4]     at Module.load (module.js:487:32)
[16:10:27][Step 3/4]     at tryModuleLoad (module.js:446:12)
[16:10:27][Step 3/4]     at Function.Module._load (module.js:438:3)
[16:10:27][Step 3/4]     at Module.require (module.js:497:17)
[16:10:27][Step 3/4]     at require (internal/module.js:20:19)
[16:10:27][Step 3/4]     at exports.default (/opt/teamcity-agent/work/1942dc01ed884f70/node_modules/@storybook/react/dist/server/config.js:57:22)
[16:10:27][Step 3/4]     at buildStatic (/opt/teamcity-agent/work/1942dc01ed884f70/node_modules/@storybook/core/dist/server/build-static.js:77:16)
[16:10:27][Step 3/4]     at Object.<anonymous> (/opt/teamcity-agent/work/1942dc01ed884f70/node_modules/@storybook/react/dist/server/build.js:23:25)
[16:10:27][Step 3/4]     at Module._compile (module.js:570:32)
[16:10:27][Step 3/4] script exit code: 1

An in-range update of babel-polyfill is breaking the build 🚨

Version 6.26.0 of babel-polyfill just got published.

Branch Build failing 🚨
Dependency babel-polyfill
Current Version 6.23.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As babel-polyfill is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of eslint-plugin-import is breaking the build 🚨

Version 2.4.0 of eslint-plugin-import just got published.

Branch Build failing 🚨
Dependency eslint-plugin-import
Current Version 2.3.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As eslint-plugin-import is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 10 commits.

  • 44ca158 update utils changelog
  • a3728d7 bump eslint-module-utils to v2.1.0
  • 3e29169 bump v2.4.0
  • ea9c92c Merge pull request #737 from kevin940726/master
  • 8f9b403 fix typos, enforce type of array of strings in allow option
  • 95315e0 update CHANGELOG.md
  • 28e1623 eslint-module-utils: filePath in parserOptions (#840)
  • 2f690b4 update CI to build on Node 6+7 (#846)
  • 7d41745 write doc, add two more tests
  • dedfb11 add allow glob for rule no-unassigned-import, fix #671

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of enhanced-resolve is breaking the build 🚨

Version 3.4.0 of enhanced-resolve just got published.

Branch Build failing 🚨
Dependency enhanced-resolve
Current Version 3.3.0
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

enhanced-resolve is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Release Notes v3.4.0

Features:

  • Performance
    • Use Map and Set instead of Arrays
    • avoid creating unneeded arrays
    • run plugin in series instead of parallel (to avoid unnecessary I/O)
    • check if plugins are registered before creating callback function
Commits

The new version differs by 14 commits.

  • 8cce0a4 3.4.0
  • c69c626 Merge pull request #96 from webpack/performance/mixed
  • ccd94a3 Merge branch 'master' into performance/mixed
  • 7946e24 Merge pull request #95 from roblg/resolve-async-series
  • 2cc8042 test on lastest node.js versions
  • 0399f4f Merge pull request #94 from DrewML/readme-example
  • f59a379 add special forEachBail with index method to avoid calling .map
  • 9693c61 skip calling hooks which are not bound
  • 03ef8f2 use Map and Set in cache
  • ec6cc3a Make file resolving asyncSeriesBail instead of parallel
  • 8aa41b6 Add basic plugin docs
  • 4643062 Add basic usage example to README.md
  • 54dc56c Merge pull request #93 from kalcifer/tapable-update
  • 7a7b29f Upgrade tapable version

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of babel-register is breaking the build 🚨

Version 6.26.0 of babel-register just got published.

Branch Build failing 🚨
Dependency babel-register
Current Version 6.24.1
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As babel-register is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

Ignore unused values to boost performance

The problem

Take a look at this example:

// main.css
@value sidebarColor from 'sidebar.css';
.sidebar {
  background: sidebarColor;
}

// sidebar.css
@value themeColor from 'theme.css';
@value sidebarColor: #00ff00;

// theme.css
@value themeColor: #f1f1f1;

When resolving main.css, the plugin should only look for sidebarColor in sidebar.css and shouldn't aware of themeColor, because nothing in main.css uses themeColor. It's just wasting resource to resolve theme.css for nothing.

Suggestion

Before walking into an imported file, the plugin should have a list of required definitions, then only looking for that list, nothing else.

I think I can work on this but I need your opinion on it first. WDYT?

An in-range update of eslint-config-airbnb-base is breaking the build 🚨

Version 11.3.0 of eslint-config-airbnb-base just got published.

Branch Build failing 🚨
Dependency eslint-config-airbnb-base
Current Version 11.2.0
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As eslint-config-airbnb-base is β€œonly” a devDependency of this project it might not break production or downstream projects, but β€œonly” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this πŸ’ͺ

Status Details
  • ❌ continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

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.