terser-glob
Version:
A CLI module that allows you to work with multiple files at once using the glob pattern in the terser cli.
2 lines • 3.86 kB
JavaScript
import e from"meow";import{minify as o}from"terser";import{glob as s}from"glob";import{join as a,dirname as t}from"path";import{access as n,readFile as r,writeFile as i,mkdir as l}from"fs/promises";import{constants as p}from"fs";const f=e({importMeta:import.meta,help:["Usage"," terser-glob path [terser-cli-options]",""," When using a glob rule in a path, you must use quotes. ('abc/*.js'...) "," -V, --version output the version number"," -p, --parse <options> Specify parser options."," -c, --compress [options] Enable compressor/specify compressor options."," -m, --mangle [options] Mangle names/specify mangler options."," --mangle-props [options] Mangle properties/specify mangler options."," -f, --format [options] Format options."," -b, --beautify [options] Alias for --format."," -o, --output <file> Output file (default STDOUT)."," --comments [filter] Preserve copyright comments in the output."," --config-file <file> Read minify() options from JSON file."," --ecma <version> Specify ECMAScript release: 5, 2015, 2016 or 2017..."," -e, --enclose [arg[,...][:value[,...]]] Embed output in a big function with configurable arguments and values."," --ie8 Support non-standard Internet Explorer 8."," --keep-classnames Do not mangle/drop class names."," --keep-fnames Do not mangle/drop function names. Useful for code relying on Function.prototype.name."," --module Input is an ES6 module"," --name-cache <file> File to hold mangled name mappings."," --safari10 Support non-standard Safari 10."," --source-map [options] Enable source map/specify source map options."," --toplevel Compress and/or mangle variables in toplevel scope."," --debug Print job progress to the console"," -h, --help output usage information"].join("\n")});function c(e){process.stdout.write(e)}async function m(e){try{return await n(e,p.F_OK),!0}catch(e){return!1}}!async function(){if(!f)throw new Error("Unknown error");if(f.flags.version||f.flags.V)return void c("terser-glob does not provide version information; check the modules you have installed.");if(f.input?.length<1)throw new Error("No input file specified");const e=[];for(let o=0;o<f.input.length;o+=1)e[o]=f.input[o].replace(/^"(.*)"$/,"$1").replace(/^'(.*)'$/,"$1");const n=await s(e,{cwd:process.cwd()}),p=f.flags.configFile||void 0,g=JSON.parse(JSON.stringify({ecma:f.flags.ecma||void 0,enclose:f.flags.enclose||f.flags.e||!1,parse:f.flags.parse||f.flags.p||{},compress:f.flags.compress||f.flags.c||{},mangle:f.flags.mangle||f.flags.m||!0,module:f.flags.module||!1,format:f.flags.beautify||f.flags.b||f.flags.format||f.flags.f||null,sourceMap:f.flags.sourceMap||!1,toplevel:f.flags.toplevel||!1,nameCache:f.flags.nameCache||null,ie8:f.flags.ie8||!1,keep_classnames:f.flags.keepClassnames||void 0,keep_fnames:f.flags.keepFnames||!1,safari10:f.flags.safari10||!1}));if(p)try{const e=await r(p,"utf-8"),o=JSON.parse(e);for(let e=0;e<Object.entries(o).length;e+=1){const[s,a]=Object.entries(o)[e];g[s]=a}}catch(e){throw new Error(`Error reading or parsing the configuration file: ${e}`)}f.flags.debug&&c(JSON.stringify(g,null,2));for(let e=0;e<n.length;e+=1){const s=n[e];let p=s;(f.flags.output||f.flags.o)&&(p=a(f.flags.output||f.flags.o,s));const c={};c[s]=await r(s,"utf-8");const u=await o(c,g),d=t(p);await m(d)||await l(d,{recursive:!0}),u.code&&await i(p,u.code)}}();