Skip to content

Commit

Permalink
Replace terser with swc
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Feb 14, 2024
1 parent ecab95d commit 02881fa
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 59 deletions.
37 changes: 20 additions & 17 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import child_process from "node:child_process"
import * as chokidar from "chokidar"
import * as ws from "ws"
import * as rollup from "rollup"
import * as terser from "terser"
import * as swc from "@swc/core"

import {
DIST_DIRNAME,
Expand Down Expand Up @@ -192,23 +192,13 @@ const command_handlers = {
return
}

const minified = await unsafePromiseToError(terser.minify(chunk.code, {module: true}))
const transformed = await swc.transform(chunk.code, {
jsc : JSC_CONFIG,
minify : true,
filename: chunk.fileName,
})

if (minified instanceof Error) {
// eslint-disable-next-line no-console
console.error("Failed to minify " + chunk.fileName + ":", minified)
errors_count += 1
return
}

if (typeof minified.code !== "string") {
// eslint-disable-next-line no-console
console.error("No code for minified chunk:", chunk.fileName)
errors_count += 1
return
}

return fsp.writeFile(path.join(dist_path, chunk.fileName), minified.code)
return fsp.writeFile(path.join(dist_path, chunk.fileName), transformed.code)
})

await Promise.all(promises)
Expand Down Expand Up @@ -246,6 +236,19 @@ if (!hasKey(command_handlers, command)) panic("Unknown command", command)
const command_handler = command_handlers[command]
command_handler(args.slice(1))

/** @type {swc.JscConfig} */
const JSC_CONFIG = {
target : "es2018",
keepClassNames : false,
loose : false,
externalHelpers: false,
minify : {
compress : true,
mangle : true,
inlineSourcesContent: true,
}
}

/**
* @param {boolean} is_release
* @returns {Promise<number>} exit code
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"prettier": "^3.1.1",
"prettier-plugin-jsdoc": "^1.3.0",
"rollup": "^4.9.4",
"terser": "^5.26.0",
"@swc/core": "^1.4.1",
"typescript": "^5.3.3",
"ws": "^8.16.0"
},
Expand Down
167 changes: 126 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 02881fa

Please sign in to comment.