{"id":16068176,"url":"https://github.com/milewski/bin-exec-loader","last_synced_at":"2025-03-18T05:31:04.401Z","repository":{"id":74778257,"uuid":"87798106","full_name":"milewski/bin-exec-loader","owner":"milewski","description":"Execute any binary as a webpack loader.","archived":false,"fork":false,"pushed_at":"2023-08-11T10:27:52.000Z","size":1449,"stargazers_count":12,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-16T17:24:05.519Z","etag":null,"topics":["binary","executable","loader","pipe","universal-loader","webpack","webpack-loader"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/milewski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-10T10:24:13.000Z","updated_at":"2024-02-25T08:16:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"73d3ed0f-87af-4ccf-88a6-381a15f540b3","html_url":"https://github.com/milewski/bin-exec-loader","commit_stats":{"total_commits":51,"total_committers":5,"mean_commits":10.2,"dds":0.2549019607843137,"last_synced_commit":"e92fd7325dfcfb10b4d8d489b4091cd63356b620"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milewski%2Fbin-exec-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milewski%2Fbin-exec-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milewski%2Fbin-exec-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milewski%2Fbin-exec-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/milewski","download_url":"https://codeload.github.com/milewski/bin-exec-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244164819,"owners_count":20408991,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary","executable","loader","pipe","universal-loader","webpack","webpack-loader"],"created_at":"2024-10-09T06:08:52.296Z","updated_at":"2025-03-18T05:31:04.395Z","avatar_url":"https://github.com/milewski.png","language":"TypeScript","readme":"# bin-exec-loader\n\n[![npm version](https://badge.fury.io/js/bin-exec-loader.svg)](https://badge.fury.io/js/bin-exec-loader)\n[![npm downloads](https://img.shields.io/npm/dm/bin-exec-loader.svg)](https://www.npmjs.com/package/bin-exec-loader)\n\nPipe **any file** through **any binary** with webpack.\n\n##### Usage Examples\n\n- You could transform any `/\\.wav$/` to `.mp3` using [ffmpeg](https://ffmpeg.org/)\n- You could transform any `/\\.pdf$/` to single **JPGs** [ImageMagick](https://www.imagemagick.org/script/convert.php)\n- This list could go on indefinitely...\n\n## Install\n\n```bash\n$ npm install bin-exec-loader --save\n```\n\n## Usage\n\nIn your `webpack.config.js` add the bin-exec-loader\n\n#### Examples\n\nLet's say you would like to use imagemagick [`convert`](https://www.imagemagick.org/script/convert.php) to scale all your images down by 50%\n\nIn plain bash you would do like this:\n\n```bash\n$ convert input/image.png -resize 50% output/image.png\n```\n\nThen if you wish to execute the same command but as a webpack-loader you would do:\n\n```js\nmodule: {\n    rules: [\n        {\n            test: /\\.(png|jpg|gif)$/,\n            use: [\n                { loader: 'file-loader', options: { name: '[name].[ext]' } },\n                {\n                    loader: 'bin-exec-loader',\n                    options: {\n                        binary: 'convert', // imagemagick converter binary\n                        prefix: '-', // because imagemagick uses an uncommon syntax -like-this --instead-of-this\n                        export: false, // because i want to let file-loader handle the output\n                        emitFile: false, // otherwise we will end up with the original input file also saved on disk\n                        args: {\n                            $1: '[input]', // [input] will be replaced by the current file that is being proceed\n                            resize: '50%',\n                            $2: '[output]' // [output] will be where your output get's temporarily written\n                        }\n                    }\n                }\n            ]\n        }\n    ]\n}\n```\n\nYou can also set dynamic args like this:\n\n```js\nconst smallImage = require('./test/sample-files/sample.png?resize=50%25') // 50%25 is the encoded version of 50%\n```\n```js\n{\n    test: /\\.(png|jpg|gif)$/,\n    loader: 'bin-exec-loader',\n    options: {\n        binary: 'convert',\n        prefix: '-', \n        args: {\n            $1: '[input]',\n            resize: '[resize]', // now all the parameters you send from the queryString will be available here as [param]\n            //resize: '[resize=50%]', // optionally you can set a default value\n            $2: '[output]'\n        }\n    }\n}\n```\n\nif your binary produces multiple outputs you can grab those like this:\n\u003e convert each page of a pdf to jpg and retrieve an array of paths as result\n\n```js\n{\n    test: /\\.pdf$/,\n    loader: 'bin-exec-loader',\n    options: {\n        binary: 'convert',\n        prefix: '-',\n        multiple: true, // let the loader knows there will be more than one output\n        emitFile: /\\d\\.jpg$/ // emit only the files ending with this pattern. e.g file-01.jpg\n        name: '[name].jpg'\n        args: {\n            $1: '[input]',\n            $2: '[output]'\n        }\n    }\n}\n```\n```js\nconsole.log(require('./some/file.pdf'))\n// [ 'file-01.jpg', file-02.jpg', 'file-03.jpg', 'file-04.jpg' ]\n```\n\nHow about a loader over http? optimizing your image using [tinypng](https://tinypng.com/developers/reference) api?\n\n```bash\n$ curl --user api:YOUR_API_KEY --data-binary @unoptimized.png https://api.tinify.com/shrink\n```\n\n```js\n{\n    test: /\\.(png|jpg|gif)$/,\n    use: 'bin-exec-loader',\n    options: {\n        binary: 'curl',\n        export: true,\n        args: {\n            user: 'api:YOUR_API_KEY',\n            dataBinary: '@[input]',\n            $0: 'https://api.tinify.com/shrink'\n        }\n    }\n}\n```\n\nThen in some file in your bundle..\n\n```js\nconst file = require('some-file.png')\n\nconsole.log(file);\n\n/**\n{\n  \"input\": {\n    \"size\": 826071,\n    \"type\": \"image/png\"\n  },\n  \"output\": {\n    \"size\": 183477,\n    \"type\": \"image/png\",\n    \"width\": 1000,\n    \"height\": 665,\n    \"ratio\": 0.2221,\n    \"url\": \"https://api.tinify.com/output/sau7d5debbhlrtae.png\"\n  }\n}\n**/\n```\n\nYou can also chain it with pretty much with any loader, you just need to understand the use of the option export, e.g: in the example above you could also archive the same result chaining it with `json-loader` :\n\n```js\n{\n    test: /\\.(png|jpg|gif)$/,\n    use: [\n        { loader: 'json-loader' },\n        {\n            loader: 'bin-exec-loader',\n            options: {\n                binary: 'curl',\n                //export: true, disable export so the raw output is passed to the next loader\n                args: {\n                    user: 'api:YOUR_API_KEY',\n                    dataBinary: '@[input]',\n                    $0: 'https://api.tinify.com/shrink'\n                }\n            }\n        }\n    ]\n}\n```\n\n## Options\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth align=\"left\"\u003eOptions\u003c/th\u003e\n    \u003cth align=\"left\"\u003eType\u003c/th\u003e\n    \u003cth align=\"left\"\u003eDefault\u003c/th\u003e\n    \u003cth align=\"left\"\u003eDescription\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003ebinary\u003c/td\u003e\n    \u003ctd\u003estring\u003c/td\u003e\n    \u003ctd\u003e-\u003c/td\u003e\n    \u003ctd\u003eThe binary you want to execute, could be a string to some executable available in your PATH or a npm module.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eexport\u003c/td\u003e\n    \u003ctd\u003eboolean\u003c/td\u003e\n    \u003ctd\u003efalse\u003c/td\u003e\n    \u003ctd\u003eDetermines if the output should be read from the \u003ccode\u003e[output]\u003c/code\u003e placeholder or it should be exported as a \u003ccode\u003emodule.exports = data\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003equote\u003c/td\u003e\n    \u003ctd\u003eboolean\u003c/td\u003e\n    \u003ctd\u003efalse\u003c/td\u003e\n    \u003ctd\u003eWhether the params should be wrapped with quotes \u003ccode\u003e--param \"one\"\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eequals\u003c/td\u003e\n    \u003ctd\u003eboolean\u003c/td\u003e\n    \u003ctd\u003efalse\u003c/td\u003e\n    \u003ctd\u003eWhether the params should be assigned with a equal sign \u003ccode\u003e--param=one\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eemitFile\u003c/td\u003e\n    \u003ctd\u003eboolean|regExp\u003c/td\u003e\n    \u003ctd\u003etrue\u003c/td\u003e\n    \u003ctd\u003eWhether if the output should be emitted\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003ename\u003c/td\u003e\n    \u003ctd\u003estring\u003c/td\u003e\n    \u003ctd\u003e[name].[ext]\u003c/td\u003e\n    \u003ctd\u003eThe output file name, you can use \u003ccode\u003e[name]\u003c/code\u003e,\u003ccode\u003e[hash]\u003c/code\u003e,\u003ccode\u003e[ext]\u003c/code\u003e and for \u003cb\u003eimages\u003c/b\u003e only: \u003ccode\u003e[width]\u003c/code\u003e, \u003ccode\u003e[height]\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eprefix\u003c/td\u003e\n    \u003ctd\u003estring|function\u003c/td\u003e\n    \u003ctd\u003estandard\u003c/td\u003e\n    \u003ctd\u003eThe prefix used to on the args key. \u003ccode\u003estandard\u003c/code\u003e will act like most CLI does, single letter gets \u003ccode\u003e-\u003c/code\u003e more than one gets \u003ccode\u003e--\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003eenable\u003c/td\u003e\n    \u003ctd\u003eboolean\u003c/td\u003e\n    \u003ctd\u003etrue\u003c/td\u003e\n    \u003ctd\u003eEnable/Disable this loader, good to use when you don't want to run it on \u003ccode\u003eprocess.env.NODE_ENV === 'development'\u003c/code\u003e for example.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003ecache\u003c/td\u003e\n    \u003ctd\u003eboolean\u003c/td\u003e\n    \u003ctd\u003etrue\u003c/td\u003e\n    \u003ctd\u003eTell webpack if the output of this loader should be cached.\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003emultiple\u003c/td\u003e\n    \u003ctd\u003eboolean\u003c/td\u003e\n    \u003ctd\u003efalse\u003c/td\u003e\n    \u003ctd\u003e\n    Determine if the binary will produce multiple outputs. If \u003ccode\u003etrue\u003c/code\u003e the result will always be an array of path of the resulting files. you can control what gets emitted with using \u003ccode\u003eemitFile: regExp\u003c/code\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n      \u003ctd\u003eargs\u003c/td\u003e\n      \u003ctd\u003eobject\u003c/td\u003e\n      \u003ctd\u003e{}\u003c/td\u003e\n      \u003ctd\u003e\n      The args you want to invoke your command with.\n      \u003cul\u003e\n          \u003cli\u003e\u003ccode\u003e$\u003c/code\u003e will be replaced \u003ccode\u003e-\u003c/code\u003e\u003c/li\u003e\n          \u003cli\u003e\u003ccode\u003e$0...N\u003c/code\u003e will be removed. e.g \u003ccode\u003e{ $2: \"hello\" }\u003c/code\u003e will become \u003ccode\u003emy-cli hello\u003c/code\u003e\u003c/li\u003e\n      \u003c/ul\u003e\n       You can also use \u003ccode\u003e[input]\u003c/code\u003e and \u003ccode\u003e[output]\u003c/code\u003e on the values as placeholders for the the real resource path.\n       e.g \u003ccode\u003e{ $0:\"[input]\" }\u003c/code\u003e will become \u003ccode\u003eopen an/absolute/path/file.extension\u003c/code\u003e\n      \u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n\n## Testing\n\nTo run the tests locally it's necessary to have installed [ImageMagick](http://www.imagemagick.org/script/download.php) and [GhostScript](https://ghostscript.com/download/gsdnld.html)\n\n## License\n\n[MIT](LICENSE) © [Rafael Milewski](https://rafael-milewski.com?github=readme)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilewski%2Fbin-exec-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmilewski%2Fbin-exec-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilewski%2Fbin-exec-loader/lists"}