From ddab90ccf790c608654c56119bdc12679e899e00 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Wed, 5 Jun 2024 08:05:51 +0800 Subject: [PATCH] shell: experiment with esbuild and terser --- scripts/gh-pages-deploy.sh | 2 +- shell/esbuild.js | 22 ++++-- shell/js/meson.build | 23 ++----- shell/package-lock.json | 134 ++++++++++++++++++++++++++++++++++++- shell/package.json | 3 +- 5 files changed, 159 insertions(+), 25 deletions(-) diff --git a/scripts/gh-pages-deploy.sh b/scripts/gh-pages-deploy.sh index bb299226..52a8a1da 100644 --- a/scripts/gh-pages-deploy.sh +++ b/scripts/gh-pages-deploy.sh @@ -51,7 +51,7 @@ $file_packager "${dest_dir}/lite-xl-files.json" \ --no-force --no-node --use-preload-cache --use-preload-plugins \ --quiet --js-output="${dest_dir}/lite-xl-files.js" # this file is rather big, run closure compiler over the output -node shell/closure.js "${dest_dir}/lite-xl-files.min.js" "${dest_dir}/lite-xl-files.js" +node shell/esbuild.js "${dest_dir}/lite-xl-files.min.js" true "${dest_dir}/lite-xl-files.js" # these files can be removed for final distribution rm -rf "$(pwd)/${dest_dir}/lite-xl.js" "$(pwd)/${dest_dir}/lite-xl-files.js" \ "${data_dir}" "$(pwd)/${dest_dir}/doc" diff --git a/shell/esbuild.js b/shell/esbuild.js index a3a6287d..8d9093f6 100644 --- a/shell/esbuild.js +++ b/shell/esbuild.js @@ -1,13 +1,27 @@ const installAndRun = require("./deps"); -installAndRun((outputFile, minify, ...entrypoints) => { +installAndRun((outputFile, minify, entrypoint, ...inject) => { const esbuild = require("esbuild"); - esbuild.buildSync({ - entryPoints: entrypoints, + const { minify_sync } = require("terser"); + const { writeFileSync } = require("fs"); + const output = esbuild.buildSync({ + entryPoints: [entrypoint], bundle: true, - minify: minify === "true", + minify: entrypoint.endsWith("js") ? false : minify == "true", target: "es2017", logLevel: "info", outfile: outputFile, + write: !entrypoint.endsWith("js"), + inject, }); + if (entrypoint.endsWith("js") && minify == "true") { + const minified = minify_sync(output.outputFiles[0].text, { + compress: { + booleans_as_integers: true, + ecma: 2017, + passes: 3, + }, + }); + writeFileSync(outputFile, minified.code, { encoding: "utf8" }); + } }); diff --git a/shell/js/meson.build b/shell/js/meson.build index 6583d12d..f9f2232c 100644 --- a/shell/js/meson.build +++ b/shell/js/meson.build @@ -1,31 +1,18 @@ -main_src = [ - 'main.js', - 'idbsync.js', - 'fs.js', -] - -main_bundle = custom_target('main.bundle.js', - input: main_src, - output: 'main.bundle.js', - depend_files: [package_lock, esbuild_script], - # esbuild only needs the entrypoint to figure out what files to bundle - command: [node_exe, esbuild_script , '@OUTPUT@', 'false', '@INPUT0@']) - custom_target('main.js', - input: [lite_exe, main_bundle], + input: ['main.js', lite_exe], output: 'main.js', build_by_default: true, - depend_files: [package_lock, closure_script, closure_externs], + depend_files: [package_lock, esbuild_script], install: true, install_dir: lite_bindir, - # the first must be the entrypoint - command: [node_exe, closure_script , '@OUTPUT@', '@INPUT@']) + # esbuild only needs the entrypoint to figure out what files to bundle + command: [node_exe, esbuild_script , '@OUTPUT@', 'true', '@INPUT@']) custom_target('advanced.js', input: 'advanced.js', output: 'advanced.js', - depend_files: [package_lock, esbuild_script], build_by_default: true, + depend_files: [package_lock, esbuild_script], install: true, install_dir: lite_bindir, # esbuild only needs the entrypoint to figure out what files to bundle diff --git a/shell/package-lock.json b/shell/package-lock.json index a68fdc11..9ac71d39 100644 --- a/shell/package-lock.json +++ b/shell/package-lock.json @@ -14,7 +14,8 @@ "devDependencies": { "esbuild": "^0.21.1", "google-closure-compiler": "^20240317.0.0", - "prettier": "^3.2.5" + "prettier": "^3.2.5", + "terser": "^5.31.0" } }, "node_modules/@esbuild/aix-ppc64": { @@ -385,6 +386,83 @@ "node": ">=12" } }, + "node_modules/@jridgewell/gen-mapping": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.5.tgz", + "integrity": "sha512-IzL8ZoEDIBRWEzlCcRhOaCupYyN5gdIK+Q6fbFdPDg6HqX6jpkItn7DFIpW9LQzXG6Df9sA7+OKnq0qlz/GaQg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/set-array": "^1.2.1", + "@jridgewell/sourcemap-codec": "^1.4.10", + "@jridgewell/trace-mapping": "^0.3.24" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/set-array": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@jridgewell/set-array/-/set-array-1.2.1.tgz", + "integrity": "sha512-R8gLRTZeyp03ymzP/6Lil/28tGeGEzhx1q2k703KGWRAI1VdvPIXdG70VJc2pAMw3NA6JKL5hhFu1sJX0Mnn/A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/source-map": { + "version": "0.3.6", + "resolved": "https://registry.npmjs.org/@jridgewell/source-map/-/source-map-0.3.6.tgz", + "integrity": "sha512-1ZJTZebgqllO79ue2bm3rIGud/bOe0pP5BjSRCRxxYkEZS8STV7zN84UBbiYu7jy+eCKSnVIUgoWWE/tt+shMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/gen-mapping": "^0.3.5", + "@jridgewell/trace-mapping": "^0.3.25" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.25", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.25.tgz", + "integrity": "sha512-vNk6aEwybGtawWmy/PzwnGDOjCkLWSD2wqvjGGAgOAwCGWySYXfYoxt00IJkTF+8Lb57DwOb3Aa0o9CApepiYQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, + "node_modules/acorn": { + "version": "8.11.3", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", + "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", + "dev": true, + "license": "MIT", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/ansi-styles": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", @@ -401,6 +479,13 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/buffer-from": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", + "dev": true, + "license": "MIT" + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", @@ -477,6 +562,13 @@ "dev": true, "license": "MIT" }, + "node_modules/commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true, + "license": "MIT" + }, "node_modules/core-util-is": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", @@ -711,6 +803,27 @@ "node": ">=0.10.0" } }, + "node_modules/source-map-support": { + "version": "0.5.21", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", + "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", + "dev": true, + "license": "MIT", + "dependencies": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + }, + "node_modules/source-map-support/node_modules/source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/string_decoder": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", @@ -734,6 +847,25 @@ "node": ">=8" } }, + "node_modules/terser": { + "version": "5.31.0", + "resolved": "https://registry.npmjs.org/terser/-/terser-5.31.0.tgz", + "integrity": "sha512-Q1JFAoUKE5IMfI4Z/lkE/E6+SwgzO+x4tq4v1AyBLRj8VSYvRO6A/rQrPg1yud4g0En9EKI1TvFRF2tQFcoUkg==", + "dev": true, + "license": "BSD-2-Clause", + "dependencies": { + "@jridgewell/source-map": "^0.3.3", + "acorn": "^8.8.2", + "commander": "^2.20.0", + "source-map-support": "~0.5.20" + }, + "bin": { + "terser": "bin/terser" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/shell/package.json b/shell/package.json index 3971bf25..f9c04b3f 100644 --- a/shell/package.json +++ b/shell/package.json @@ -15,7 +15,8 @@ "devDependencies": { "esbuild": "^0.21.1", "google-closure-compiler": "^20240317.0.0", - "prettier": "^3.2.5" + "prettier": "^3.2.5", + "terser": "^5.31.0" }, "dependencies": { "fflate": "^0.8.2"