Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Node.js API #934

Closed
alexander-akait opened this issue Jan 14, 2021 · 8 comments · Fixed by #1002
Closed

Make Node.js API #934

alexander-akait opened this issue Jan 14, 2021 · 8 comments · Fixed by #1002

Comments

@alexander-akait
Copy link
Contributor

Is your feature request related to a problem? Please describe.

squoosh has CLI, so I think it is possible to implement Node.js API

Describe the solution you'd like

Adding Node.js API

Does other service/app have this feature?

For example - imagemin have CLI and Node.js API

Additional context

The Node.js API will allow squoosh to be used in other tools, for example I am developers https://github.com/webpack-contrib/image-minimizer-webpack-plugin and now we use imagemin for this, but setup is complex and unable on some platform because libraries for compression compiled on developer machine

@surma
Copy link
Collaborator

surma commented Jan 14, 2021

Yup, at some point I want to re-factor the CLI to have a proper separation between a libsquoosh and the cli

@alexander-akait
Copy link
Contributor Author

Yep, it will be great, this would take image optimization to a new level, hoping it will come in the near future, squoosh is super

@tomByrer
Copy link

& deno compat importing also please :)

@tomByrer
Copy link

@alexander-akait in the meanwhile, you can use exec or it's asyc sister. Actually might be better to always run CLI this way (after adding commander or yargs) for a CLI if one uses the same pattern often.

const { exec } = require("child_process")

let file = './img/codepen.io-2021-02-15.png'
let rounds = 2
let bt = 1.1

exec(`squoosh-cli -s .r${rounds}bt${bt} --avif --max-optimizer-rounds ${rounds} --optimizer-butteraugli-target ${bt} -d out ${file}`,  (error, stdout, stderr) => {
	if (error) {
			console.log(`error: ${error.message}`);
			return;
	}
	if (stderr) {
			console.log(`stderr: ${stderr}`);
			return;
	}
	console.log(`🎆 success!
${stdout}`);
})

@tomByrer
Copy link

My above code doesn't work when launched in Window's git-bash, but this one does! Just have to use npm & not have good error reporting:
https://gist.github.com/tomByrer/f6510da9f971a815b9b91d2964d00647

@atjn
Copy link
Contributor

atjn commented Apr 22, 2021

@surma I need this badly enough that I'm willing to yank the source code and manually frankenstein it into my project. If you are up for it, I would much rather spend that time making a PR here that adds an official @squoosh/api package.

I'm not too worried that I will implement it differently from what you have in mind, since it's all experimental, so you can always redesign it when you have time for it. I think most of us are okay with a basic and unstable API, we just want something as soon as possible :)

@surma
Copy link
Collaborator

surma commented Apr 22, 2021

@atjn That’d be amazing. Happy to review a PR!

@atjn
Copy link
Contributor

atjn commented May 4, 2021

@surma there you go, hope you like it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants