Skip to content

Commit

Permalink
package: use parcel bundler ...
Browse files Browse the repository at this point in the history
* Significantly reduced deployment size
* Bot starts up much more faster
  • Loading branch information
liushuyu committed Oct 23, 2021
1 parent a382896 commit 3c1019e
Show file tree
Hide file tree
Showing 9 changed files with 4,098 additions and 128 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Expand Up @@ -2,7 +2,7 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [ "*" ]
pull_request:
branches: [ master ]

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -51,3 +51,5 @@ config/development.json
CMakeLists.txt.user*

/dist
# Parcel's cache
/.parcel-cache
4 changes: 4 additions & 0 deletions .parcelrc
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"resolvers": ["@parcel/resolver-glob", "..."]
}
10 changes: 4 additions & 6 deletions Dockerfile
Expand Up @@ -4,9 +4,10 @@ FROM node:16-alpine AS build
WORKDIR /usr/src/app

# Install app dependencies and add source files
COPY package.json yarn.lock tsconfig.json ./
COPY src/ ./src
RUN yarn install && yarn build && rm -f dist/*.map
COPY package.json yarn.lock env.json bundle.sh .parcelrc ./
COPY src/ ./src
COPY patches/ ./patches
RUN apk add python3 build-base && yarn install && sh bundle.sh

# Second stage
FROM node:16-alpine
Expand All @@ -15,9 +16,6 @@ WORKDIR /usr/src/app

# Copy artifacts
COPY --from=build /usr/src/app/dist/ ./
COPY --from=build /usr/src/app/node_modules ./node_modules
COPY env.json src/responses.json ./
COPY src/responses ./responses/

RUN addgroup -S app -g 50000 && \
adduser -S -g app -u 50000 app && \
Expand Down
27 changes: 27 additions & 0 deletions bundle.sh
@@ -0,0 +1,27 @@
#!/bin/bash -e
yarn

[ -d "dist" ] && rm -rf dist

echo "[+] Applying patches to accommodate bundler ..."
for i in patches/*.patch; do
echo "Applying $i ..."
patch -Np1 --no-backup-if-mismatch -i "$i"
done

yarn run parcel build

echo "[+] Installing non-bundle-able packages ..."
cd "dist"
echo '{"name": "citra-discord-bot","license": "GPL-2.0+"}' > package.json
yarn add discord.js@^13
cd ..

echo "[+] Reversing patches ..."
for i in patches/*.patch; do
echo "Reversing $i ..."
patch -Np1 -R -i "$i"
done

echo "[+] Removing patch backup files ..."
find . -name "*.orig" -print -delete
26 changes: 22 additions & 4 deletions package.json
Expand Up @@ -8,22 +8,36 @@
"subdomain": "citra-emu",
"analyze": true,
"license": "GPL-2.0+",
"source": "src/server.ts",
"main": "dist/server.js",
"engines": {
"node": ">= 16"
},
"targets": {
"main": {
"includeNodeModules": {
"discord.js": false
},
"context": "node",
"optimize": true
}
},
"dependencies": {
"checkenv": "^1.2.2",
"discord.js": "^13.2.0",
"ip": "^1.1.5",
"logdna": "^3.5.2",
"logdna-winston": "^4.0.1",
"node-fetch": "^2",
"node-fetch": "^3",
"string-similarity": "^4.0.4",
"typescript": "^4.4.4",
"winston": "^3.3.3"
},
"devDependencies": {
"@parcel/resolver-glob": "^2.0.0",
"@tsconfig/node14": "^1.0.1",
"@types/ip": "^1.1.0",
"@types/node": "^16.11.2",
"@types/node-fetch": "^2",
"@types/node-fetch": "^3",
"@types/string-similarity": "^4.0.0",
"@types/ws": "^8.2.0",
"eslint": "^8.0.1",
Expand All @@ -32,10 +46,14 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.1",
"eslint-plugin-standard": "^4.1.0",
"ts-node": "^10.3.1"
"parcel": "^2.0.0",
"ts-node": "^10.3.1",
"typescript": "^4.4.4"
},
"scripts": {
"check": "yarn run tsc --noEmit",
"build": "yarn run tsc",
"dist": "./bundle.sh",
"serve": "yarn run ts-node ./src/server.ts"
}
}
95 changes: 95 additions & 0 deletions patches/0001-server-make-it-bundler-friendly.patch
@@ -0,0 +1,95 @@
From 2ae094e7ad43d9137fafe84a36eac3e9bf0a8245 Mon Sep 17 00:00:00 2001
From: liushuyu <liushuyu011@gmail.com>
Date: Wed, 15 Sep 2021 22:49:40 -0600
Subject: [PATCH] server: make it bundler friendly

---
src/server.ts | 49 ++++++++++++++++++++-----------------------------
1 file changed, 20 insertions(+), 29 deletions(-)

diff --git a/src/server.ts b/src/server.ts
index bc63ae7..7cf8825 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -1,5 +1,7 @@
// Check for environmental variables.
-require('checkenv').check();
+const checkenv = require('checkenv');
+checkenv.setConfig(require('../env.json'));
+checkenv.check();

import discord = require('discord.js');
import path = require('path');
@@ -9,6 +11,10 @@ import logger from './logging';
import state from './state';
import * as data from './data';
import { IModule, ITrigger } from './models/interfaces';
+// Parcel glob imports
+import commands from './commands/*.ts';
+import triggers from './triggers/*.ts';
+import responses from './responses/*.json';

interface IModuleMap {
[name: string]: IModule;
@@ -233,37 +239,16 @@ client.on('message', message => {
});

// Cache all command modules.
-cachedModules = {};
-fs.readdirSync('./commands/').forEach(function (file) {
- // Load the module if it's a script.
- if (path.extname(file) === '.js') {
- if (file.includes('.disabled')) {
- logger.info(`Did not load disabled module: ${file}`);
- } else {
- const moduleName = path.basename(file, '.js').toLowerCase();
- logger.info(`Loaded module: ${moduleName} from ${file}`);
- cachedModules[moduleName] = require(`./commands/${file}`);
- }
- }
+cachedModules = commands;
+Object.entries(commands).forEach(function (command) {
+ logger.info(`Loaded command: ${command[0]}`);
});

// Cache all triggers.
cachedTriggers = [];
-fs.readdirSync('./triggers/').forEach(function (file) {
- // Load the module if it's a script.
- if (path.extname(file) === '.js') {
- if (file.includes('.disabled')) {
- logger.info(`Did not load disabled trigger: ${file}`);
- } else {
- const moduleName = path.basename(file, '.js').toLowerCase();
- logger.info(`Loaded trigger: ${moduleName} from ${file}`);
- try {
- cachedTriggers.push(require(`./triggers/${file}`));
- } catch (e) {
- logger.error(`Could not load trigger ${moduleName}: ${e}`);
- }
- }
- }
+Object.entries(triggers).forEach((trigger: [string, ITrigger]) => {
+ cachedTriggers.push(trigger[1]);
+ logger.info(`Loaded trigger: ${trigger[0]}`);
});

data.readWarnings();
@@ -271,7 +256,13 @@ data.readBans();

// Load custom responses
if (process.env.DATA_CUSTOM_RESPONSES) {
- data.readCustomResponses();
+ // Load the responses file into the responses variable.
+ state.responses = responses[process.env.TENANT];
+ if (!state.responses) {
+ logger.error(`Failed to load ${process.env.TENANT} from cache! Custom responses are disabled.`);
+ } else {
+ logger.debug(`Loaded ${process.env.TENANT} responses from cache.`);
+ }
}

client.login(process.env.DISCORD_LOGIN_TOKEN);
--
2.33.0

11 changes: 11 additions & 0 deletions patches/0002-winston-use-bundled-logform.patch
@@ -0,0 +1,11 @@
--- a/node_modules/winston/dist/winston.js 2021-08-26 22:26:24.296000000 -0600
+++ b/node_modules/winston/dist/winston.js 2021-09-15 23:35:13.136034453 -0600
@@ -6,7 +6,7 @@
*/
'use strict';

-var logform = require('logform');
+import * as logform from 'logform/dist/browser.js';

var _require = require('./winston/common'),
warn = _require.warn;

0 comments on commit 3c1019e

Please sign in to comment.