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.

Run PostCSS synchronously?

See original GitHub issue

I am writing my own node script for development. I am facing a problem where PostCSS processes the files asynchronously and the server starts before the compilation is complete.

I know it should return a LazyResult, is there a way to not do that? This is my code:

const processCSS = () => {
	const CSSFile = fs.readFileSync(path.resolve(__dirname, '../src/styles/main.css'));
	postcss(postcssConfig.plugins)
		.process(CSSFile, { from: 'src/styles/main.css', to: '.tmp/styles/main.css' })
		.then(result => {
			fs.writeFileSync('.tmp/styles/main.css', result.css, () => true);
			logInfo('CSS', 'steelblue', 'Processed successfully');
			if ( result.map ) {
				fs.writeFileSync('.tmp/styles/main.css', result.map, () => true);
			}
		})
		.catch(err => {
		});
}

processCSS();

I tried switching to async/await without success. I’ve seen this old issue and I still have no clue what should be done.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
aicommented, Mar 29, 2021

@sergeysova if you do not want to use async plugins, you can call PostCSS sync API:

let result = postcss(plugins).process(css, opts)
result.css
result.map
result.messages
2reactions
ahmadalfycommented, Feb 18, 2020

I found the problem,

Those plugins I use don’t offer sync mode:

		// require('postcss-import'),
		// require('postcss-easy-import'),
		// require('postcss-preset-env'),
		// require('cssnano'),

Thank you, that sounds like a reasonable solution

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use autoprefixer synchronously - css - Stack Overflow
PostCSS has an API for synchronously getting the results of Processor#process ( process().css , aliased as toString() ), which will work as long...
Read more >
PostCSS API
Processes input CSS through synchronous and asynchronous plugins and calls onFinally on any error or when all plugins will finish work.
Read more >
postcss/postcss - Gitter
I used postcss6 with postcss-nested, postcss-import, postcss-cssnext and autoprefixer in babel6 plugin earler in synchronous mode.
Read more >
postcss-modules-sync - npm
Start using postcss-modules-sync in your project by running `npm i postcss-modules-sync`. There are 27 other projects in the npm registry ...
Read more >
Learn how to power-up your CSS with PostCSS - YouTube
If you're going to use the preset-env, I'd strongly recommend adding the PostCSS Language Support plugin to VS Code, so that it recognizes ......
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