|
| 1 | +import path from "path"; |
| 2 | +import fs from "fs"; |
| 3 | +import { fileURLToPath } from "url"; |
| 4 | +import { minify } from "terser"; |
| 5 | +import * as chalkUtils from "./chalkUtils.js"; |
| 6 | +import fromConsole from "./fromConsole.js"; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +export default async function validateTerser() { |
| 12 | + chalkUtils.step("Validating Terser build (mangle-props keep_quoted)"); |
| 13 | + |
| 14 | + const filesToCheck = [ |
| 15 | + "../dist/export/c3runtime/main.js", |
| 16 | + "../dist/export/editor.js", |
| 17 | + ]; |
| 18 | + |
| 19 | + let hadError = false; |
| 20 | + |
| 21 | + for (const file of filesToCheck) { |
| 22 | + const absolutePath = path.resolve(__dirname, file); |
| 23 | + if (!fs.existsSync(absolutePath)) { |
| 24 | + chalkUtils.info(`Skipping ${file} as it does not exist.`); |
| 25 | + continue; |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const code = fs.readFileSync(absolutePath, "utf8"); |
| 30 | + const result = await minify(code, { |
| 31 | + mangle: { |
| 32 | + properties: { |
| 33 | + keep_quoted: true, |
| 34 | + }, |
| 35 | + }, |
| 36 | + compress: { |
| 37 | + dead_code: true, |
| 38 | + drop_console: false, |
| 39 | + drop_debugger: true, |
| 40 | + keep_classnames: false, |
| 41 | + keep_fargs: true, |
| 42 | + keep_fnames: false, |
| 43 | + keep_infinity: false, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + if (result.error) { |
| 48 | + throw result.error; |
| 49 | + } |
| 50 | + chalkUtils.success(`Terser validation passed for ${file}`); |
| 51 | + } catch (error) { |
| 52 | + chalkUtils.error(`Terser validation failed for ${file}`); |
| 53 | + chalkUtils.error(error.message || error); |
| 54 | + hadError = true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (hadError) { |
| 59 | + chalkUtils.failed("Terser validation failed."); |
| 60 | + } else { |
| 61 | + chalkUtils.success("Terser validation successful!"); |
| 62 | + } |
| 63 | + |
| 64 | + return hadError; |
| 65 | +} |
| 66 | + |
| 67 | +// if is being called from the command line |
| 68 | +if (fromConsole(import.meta.url)) { |
| 69 | + chalkUtils.fromCommandLine(); |
| 70 | + validateTerser(); |
| 71 | +} |
0 commit comments