From e66448117562fcd9f6fc89c03799009673fcc379 Mon Sep 17 00:00:00 2001 From: Allan Niles Date: Wed, 16 Apr 2025 10:48:59 -0600 Subject: [PATCH] refactor minify script --- .github/workflows/latest.yml | 6 ++--- .github/workflows/stable.yml | 7 +----- .gitignore | 3 +-- README.md | 6 +++++ data/minify/index.js | 28 ----------------------- data/minify/package.json | 22 ------------------ {data/minify => minify}/README.md | 20 +++++++--------- minify/minify.js | 38 +++++++++++++++++++++++++++++++ package.json | 37 ++++++++++++++++++------------ 9 files changed, 80 insertions(+), 87 deletions(-) delete mode 100644 data/minify/index.js delete mode 100644 data/minify/package.json rename {data/minify => minify}/README.md (50%) create mode 100644 minify/minify.js diff --git a/.github/workflows/latest.yml b/.github/workflows/latest.yml index a150619..8405deb 100644 --- a/.github/workflows/latest.yml +++ b/.github/workflows/latest.yml @@ -24,15 +24,15 @@ jobs: git pull - name: Minify Files run: | - cd /mnt/HDD/public/.EmulatorJS/data/minify/ + cd /mnt/HDD/public/.EmulatorJS/ npm i - npm run build + npm run minify - name: Zip Minified Files run: | cd /mnt/HDD/public/.EmulatorJS/data/ zip emulator.min.zip emulator.min.js emulator.min.css - name: Clean Up Minify run: | - cd /mnt/HDD/public/.EmulatorJS/data/ + cd /mnt/HDD/public/.EmulatorJS/ rm "minify/package-lock.json" rm -rf "minify/node_modules/" \ No newline at end of file diff --git a/.github/workflows/stable.yml b/.github/workflows/stable.yml index c5003e0..65ef198 100644 --- a/.github/workflows/stable.yml +++ b/.github/workflows/stable.yml @@ -66,9 +66,4 @@ jobs: run: | cd /mnt/HDD/public/ rm -f "$OLD_VERSION/data/emulator.min.zip" - rm -rf "$OLD_VERSION/data/minify/node_modules/" - - - - - + rm -rf "$OLD_VERSION/node_modules/" diff --git a/.gitignore b/.gitignore index 6116e25..e7c8751 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ **/node_modules/ *.db -data/minify/package-lock.json package-lock.json yarn.lock roms/ @@ -8,4 +7,4 @@ data/emulator.min.js data/emulator.min.css data/cores .DS_Store -.vscode/* \ No newline at end of file +.vscode/* diff --git a/README.md b/README.md index ac4c3b6..fb18555 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,12 @@ npm i npm start ``` +#### Minifying + +Before pushing the script files onto your production server it is recommended to minify them to save on load times as well as bandwidth. + +Read the [minifying](minify/README.md) documentation for more info. +
**>> When reporting bugs, please specify what version you are using** diff --git a/data/minify/index.js b/data/minify/index.js deleted file mode 100644 index afd81f3..0000000 --- a/data/minify/index.js +++ /dev/null @@ -1,28 +0,0 @@ -const UglifyJS = require("uglify-js"); -const fs = require('fs'); -const uglifycss = require('uglifycss'); - -const scripts = [ - "emulator.js", - "nipplejs.js", - "shaders.js", - "storage.js", - "gamepad.js", - "GameManager.js", - "socket.io.min.js", - "compression.js" -]; -let code = "(function() {\n"; -for (let i=0; i -server it is recommended to minify them to save on
-load times as well as bandwidth. +Before pushing the script files onto your production server it is recommended to minify them to save on load times as well as bandwidth.
@@ -15,20 +12,19 @@ load times as well as bandwidth. ## Steps -1. Open a terminal in `/data/minify` . +1. Open a terminal in the root of the project. 2. Install the dependencies with: - ```sh - npm install - ``` + ```sh + npm i + ``` 3. Start the minification with: - ```sh - node index.js - ``` - + ```sh + node run minify + ``` diff --git a/minify/minify.js b/minify/minify.js new file mode 100644 index 0000000..b8cb09c --- /dev/null +++ b/minify/minify.js @@ -0,0 +1,38 @@ +import path from "path"; +import { fileURLToPath } from "url"; +import minify from "@node-minify/core"; +import terser from "@node-minify/terser"; +import cleanCSS from "@node-minify/clean-css"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const rootPath = path.resolve(__dirname, "../"); + +async function doMinify() { + await minify({ + compressor: terser, + input: path.join(rootPath, "data/src/*.js"), + output: path.join(rootPath, "data/emulator.min.js"), + }) + .catch(function (err) { + console.error(err); + }) + .then(function () { + console.log("Minified JS"); + }); + await minify({ + compressor: cleanCSS, + input: path.join(rootPath, "data/emulator.css"), + output: path.join(rootPath, "data/emulator.min.css"), + }) + .catch(function (err) { + console.error(err); + }) + .then(function () { + console.log("Minified CSS"); + }); +} + +console.log("Minifying"); +await doMinify(); +console.log("Minifying Done!"); diff --git a/package.json b/package.json index d7e58c1..9e16e6c 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,25 @@ { - "name": "@emulatorjs/emulatorjs", - "version": "4.2.1", - "repository": { - "type": "git", - "url": "https://github.com/EmulatorJS/EmulatorJS.git" - }, - "license": "GPL-3.0", - "description": "EmulatorJS is a frontend for RetroArch in the web browser.", - "scripts": { - "start": "http-server" - }, - "dependencies": { - "http-server": "^14.1.1" - } + "name": "@emulatorjs/emulatorjs", + "version": "4.2.1", + "type": "module", + "description": "EmulatorJS is a frontend for RetroArch in the web browser.", + "homepage": "https://emulatorjs.org", + "license": "GPL-3.0", + "repository": { + "type": "git", + "url": "https://github.com/EmulatorJS/EmulatorJS.git" + }, + "bugs": { + "url": "https://github.com/EmulatorJS/EmulatorJS/issues" + }, + "scripts": { + "start": "http-server", + "minify": "node minify/minify.js" + }, + "dependencies": { + "@node-minify/clean-css": "^9.0.1", + "@node-minify/core": "^9.0.2", + "@node-minify/terser": "^9.0.1", + "http-server": "^14.1.1" + } }