{"id":2462,"date":"2024-11-23T11:11:18","date_gmt":"2024-11-23T10:11:18","guid":{"rendered":"https:\/\/www.speich.net\/articles\/?p=2462"},"modified":"2024-12-16T14:43:14","modified_gmt":"2024-12-16T13:43:14","slug":"use-esbuild-instead-of-uglifyjs-folder-and-csso-cli","status":"publish","type":"post","link":"https:\/\/www.speich.net\/articles\/en\/2024\/11\/23\/use-esbuild-instead-of-uglifyjs-folder-and-csso-cli\/","title":{"rendered":"Use esbuild instead of uglifyjs-folder and csso-cli"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">You can use the superfast <a href=\"https:\/\/esbuild.github.io\/\">esbuild<\/a> instead of uglifyjs-folder and <a href=\"https:\/\/github.com\/css\/csso-cli\" target=\"_blank\" rel=\"noreferrer noopener\">csso-cli<\/a> to copy (and minify) your JavaScript and\/or CSS files (without having to bundle the files) in your npm scripts.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\"> Why would you want to do this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>simplify your build process<\/li>\n\n\n\n<li>reduce number of dependencies (and potential supply chain attack vectors)<\/li>\n\n\n\n<li>outdated dependencies<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Note: esbuild is not really meant to just copy files without bundling, so use the following with care.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Copy a folder with esbuild<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you just want to copy the contents of a folder, you have do do so by file type:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code># copy js files without any modification\nesbuild .\/somefolder\/*.js --loader:.js=copy --outdir=somefolder\n\n# copy with minification\nesbuild .\/somefolder\/*.js --bundle --minify --sourcemap --external:*.js --format=esm --outdir=somefolder<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The trick to copy with minification but &#8220;without&#8221; bundling is to actually bundle <code>--bundle --minify --sourcemap<\/code>, but mark all files in the folder you want to copy as external <code>--external:.\/somefolder\/*<\/code>. This way the imports are not inlined but kept as import statements. The contents of the files is rewritten though (let, const &#8211;&gt; var).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use esbuild instead of csso-cli<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The same as above works for CSS files instead:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code># copy css files without any modification\nesbuild .\/somefolder\/*.css --loader:.css=copy --outdir=somefolder\n\n# copy with minification\nesbuild .\/somefolder\/*.css --bundle --sourcemap --external:*.css --outdir=test<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you get an error such as <code>No loader is configured for \".woff2\" files: somefolder\/somefont.woff2<\/code> just add the argument <code>--external:*.woff2<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Copy multiple files and folders with esbuild<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To copy several files and olders at once, you need to specifying a copy loader for each file type:<\/p>\n\n\n\n<p class=\"language-bash wp-block-paragraph\"><code>esbuild .\/somefolder\/**\/* --loader:.css=copy --loader:.js=copy --loader:.svg=copy --outdir=\/somefolder<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy &#8220;building&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use the superfast esbuild instead of uglifyjs-folder and csso-cli to copy (and minify) your JavaScript and\/or CSS files (without having to bundle the files) in your npm scripts.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[248,3],"tags":[],"class_list":["post-2462","post","type-post","status-publish","format-standard","hentry","category-cascading-style-sheets-css","category-javascript","entry"],"_links":{"self":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/comments?post=2462"}],"version-history":[{"count":8,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2462\/revisions"}],"predecessor-version":[{"id":2476,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2462\/revisions\/2476"}],"wp:attachment":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/media?parent=2462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/categories?post=2462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/tags?post=2462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}