UNPKG

@node-minify/google-closure-compiler

Version:
1 lines 4.82 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/*!\n * node-minify\n * Copyright(c) 2011-2024 Rodolphe Stoclin\n * MIT Licensed\n */\n\n/**\n * Module dependencies.\n */\nimport { runCommandLine } from \"@node-minify/run\";\nimport type {\n MinifierOptions,\n Options,\n OptionsPossible,\n} from \"@node-minify/types\";\nimport { utils } from \"@node-minify/utils\";\nimport compilerPath from \"google-closure-compiler-java\";\n\n// the allowed flags, taken from https://github.com/google/closure-compiler/wiki/Flags-and-Options\nconst allowedFlags = [\n \"angular_pass\",\n \"assume_function_wrapper\",\n \"checks_only\",\n \"compilation_level\",\n \"create_source_map\",\n \"define\",\n \"env\",\n \"externs\",\n \"export_local_property_definitions\",\n \"generate_exports\",\n \"language_in\",\n \"language_out\",\n \"output_wrapper\",\n \"polymer_version\",\n \"process_common_js_modules\",\n \"rename_prefix_namespace\",\n \"rewrite_polyfills\",\n \"use_types_for_optimization\",\n \"warning_level\",\n];\n\n/**\n * Run Google Closure Compiler.\n * @param settings GCC options\n * @param content Content to minify\n * @param callback Callback\n * @param index Index of current file in array\n * @returns Minified content\n */\nconst minifyGCC = ({ settings, content, callback, index }: MinifierOptions) => {\n const options = applyOptions({}, settings?.options ?? {});\n return runCommandLine({\n args: gccCommand(options),\n data: content,\n settings,\n callback: (err: unknown, content?: string) => {\n if (err) {\n if (callback) {\n return callback(err);\n }\n throw err;\n }\n if (settings && !settings.content && settings.output) {\n utils.writeFile({ file: settings.output, content, index });\n }\n if (callback) {\n return callback(null, content);\n }\n return content;\n },\n });\n};\n\n/**\n * Adds any valid options passed in the options parameters to the flags parameter and returns the flags object.\n * @param flags the flags object to add options to\n * @param options the options object to add to the flags object\n * @returns the flags object with the options added\n */\ntype Flags = {\n [key: string]: boolean | Record<string, OptionsPossible>;\n};\nconst applyOptions = (flags: Flags, options?: Options): Flags => {\n if (!options || Object.keys(options).length === 0) {\n return flags;\n }\n Object.keys(options)\n .filter((option) => allowedFlags.indexOf(option) > -1)\n .forEach((option) => {\n const value = options[option];\n if (\n typeof value === \"boolean\" ||\n (typeof value === \"object\" && !Array.isArray(value))\n ) {\n flags[option] = value as\n | boolean\n | Record<string, OptionsPossible>;\n }\n });\n return flags;\n};\n\n/**\n * GCC command line.\n * @param options the options to pass to GCC\n * @returns the command line arguments to pass to GCC\n */\n\nconst gccCommand = (options: Record<string, OptionsPossible>) => {\n return [\"-jar\", compilerPath].concat(utils.buildArgs(options ?? {}));\n};\n\n/**\n * Expose `minifyGCC()`.\n */\nminifyGCC.default = minifyGCC;\nexport = minifyGCC;\n"],"mappings":";;;;;;AASA,SAAS,sBAAsB;AAM/B,SAAS,aAAa;AACtB,OAAO,kBAAkB;AAhBzB;AAAA;AAmBA,QAAM,eAAe;AAAA,MACjB;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACJ;AAUA,QAAM,YAAY,CAAC,EAAE,UAAU,SAAS,UAAU,MAAM,MAAuB;AAC3E,YAAM,UAAU,aAAa,CAAC,GAAG,UAAU,WAAW,CAAC,CAAC;AACxD,aAAO,eAAe;AAAA,QAClB,MAAM,WAAW,OAAO;AAAA,QACxB,MAAM;AAAA,QACN;AAAA,QACA,UAAU,CAAC,KAAcA,aAAqB;AAC1C,cAAI,KAAK;AACL,gBAAI,UAAU;AACV,qBAAO,SAAS,GAAG;AAAA,YACvB;AACA,kBAAM;AAAA,UACV;AACA,cAAI,YAAY,CAAC,SAAS,WAAW,SAAS,QAAQ;AAClD,kBAAM,UAAU,EAAE,MAAM,SAAS,QAAQ,SAAAA,UAAS,MAAM,CAAC;AAAA,UAC7D;AACA,cAAI,UAAU;AACV,mBAAO,SAAS,MAAMA,QAAO;AAAA,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