question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JS API example not working

See original GitHub issue

Hi there. I have a problem… I cant understand how readme example is supposed to work:

var postcss = require('postcss');
postcss([ require('autoprefixer'), require('cssnano') ])
    .process(css, { from: 'src/app.css', to: 'app.css' })
    .then(function (result) {
        fs.writeFileSync('app.css', result.css);
        if ( result.map ) fs.writeFileSync('app.css.map', result.map);
    });

What is css? In the doc you write It is a String with input CSS… but if I have to read my input file and place it as a string to css argument, what is options.from then?

Also, why I should write app.css file in promise then? What is options.to then?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:20 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
michael-ciniawskycommented, Dec 27, 2016

@Diokuz


const css = fs.readFileSync('./src/app.css', 'utf-8') // {String}

postcss(plugins)
   .process(css, { from: 'src/app.css', to: 'app.css', map: { inline: false } })
   .then((result) => {
      fs.writeFileSync('./app.css', result.css, 'utf-8') // {String}
     // Doesn't need options.from/to to work
      console.log(result.map) 
     // Needs options.from/to to know the path/location of the origin (css) and
     // destination (result.css) to generate the mappings (changes made by postcss plugins)
     //
     // Generated Souremap
     //
     // {
     //   "version": 3,
     //   "file": "app.css", (options.from)
     //   "sources": ["src/app.css"], (options.to)
     //   "mappings": "AAAA,KAAI" 
     // }
   })
1reaction
aicommented, Dec 26, 2016

@Diokuz I thought about file system shortcut. But it is unnecessary in most of cases:

  1. gulp-postcss gets file content via Gulp API.
  2. postcss-loader gets file content via webpack API.
  3. PostCSS CLI use better reading mechanism to read multiple files faster.
  4. PostCSS for client JS has even no access to fs.

In result there is no good reason to some postcss().readAndWrite() shortcut. Anyway most of PostCSS runners will get file content by other way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the API not working correctly? - Stack Overflow
I am new to JavaScript and API's so I can't solve this problem.. Here's the the javascript and the form part of the...
Read more >
Introduction to web APIs - Learn web development | MDN
Browser APIs are built into your web browser and are able to expose data from the browser and surrounding computer environment and do...
Read more >
Node.js v19.3.0 Documentation
In most cases, AsyncLocalStorage works without issues. In rare situations, the current store is lost in one of the asynchronous operations. If your...
Read more >
Error Messages | Maps JavaScript API - Google Developers
Check that the API key you currently use on your website is listed. If that's not the case, switch to a different project,...
Read more >
API - esbuild
If you still run into cross-platform quote escaping issues with different ... For example, assume you have a file called process-shim.js that exports...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found