From 9ae38a5c126f1c69d76b77a8aaf99edbe198e15a Mon Sep 17 00:00:00 2001 From: Micah Galizia Date: Sat, 24 Aug 2024 22:39:32 -0400 Subject: [PATCH] feat: bundle modules with server js instead of installing in docker (#283) * feat: bundle modules with server js instead of installing in docker --- package-lock.json | 32 +++++++++----------------- packages/api/Dockerfile | 3 ++- packages/api/package.json | 2 +- packages/api/src/server.ts | 2 +- packages/api/webpack.common.js | 42 +++++++++++++++++++--------------- packages/api/webpack.prod.js | 4 ++++ 6 files changed, 42 insertions(+), 43 deletions(-) diff --git a/package-lock.json b/package-lock.json index 27f6496..eb885c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7875,15 +7875,6 @@ "@types/json-schema": "*" } }, - "node_modules/@types/eslint-scope": { - "version": "3.7.7", - "resolved": "https://registry.npmjs.org/@types/eslint-scope/-/eslint-scope-3.7.7.tgz", - "integrity": "sha512-MzMFlSLBqNF2gcHWO0G1vP/YQyfvrxZ0bF+u7mzUdZ1/xK4A4sru+nraZz5i3iEIk1l1uyicaDVTB4QbbEkAYg==", - "dependencies": { - "@types/eslint": "*", - "@types/estree": "*" - } - }, "node_modules/@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", @@ -25018,9 +25009,9 @@ } }, "node_modules/tslib": { - "version": "2.6.3", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.3.tgz", - "integrity": "sha512-xNvxJEOUiWPGhUuUdQgAJPKOOJfGnIyKySOc09XkKsgdUV/3E2zvwZYdejjmRgPCgcym1juLH3226yA7sEFJKQ==" + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.7.0.tgz", + "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==" }, "node_modules/tsutils": { "version": "3.21.0", @@ -25507,11 +25498,10 @@ } }, "node_modules/webpack": { - "version": "5.93.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.93.0.tgz", - "integrity": "sha512-Y0m5oEY1LRuwly578VqluorkXbvXKh7U3rLoQCEO04M97ScRr44afGVkI0FQFsXzysk5OgFAxjZAb9rsGQVihA==", + "version": "5.94.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.94.0.tgz", + "integrity": "sha512-KcsGn50VT+06JH/iunZJedYGUJS5FGjow8wb9c0v5n1Om8O1g4L6LjtfxwlXIATopoQu+vOXXa7gYisWxCoPyg==", "dependencies": { - "@types/eslint-scope": "^3.7.3", "@types/estree": "^1.0.5", "@webassemblyjs/ast": "^1.12.1", "@webassemblyjs/wasm-edit": "^1.12.1", @@ -25520,7 +25510,7 @@ "acorn-import-attributes": "^1.9.5", "browserslist": "^4.21.10", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^5.17.0", + "enhanced-resolve": "^5.17.1", "es-module-lexer": "^1.2.1", "eslint-scope": "5.1.1", "events": "^3.2.0", @@ -26726,7 +26716,7 @@ "ts-loader": "^9.4.2", "ts-node": "^10.9.1", "ts-prune": "^0.10.3", - "webpack": "^5.88.2", + "webpack": "^5.94.0", "webpack-cli": "^5.1.4", "websocket": "^1.0.34" } @@ -27157,9 +27147,9 @@ } }, "packages/api/node_modules/@types/node": { - "version": "18.19.44", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.44.tgz", - "integrity": "sha512-ZsbGerYg72WMXUIE9fYxtvfzLEuq6q8mKERdWFnqTmOvudMxnz+CBNRoOwJ2kNpFOncrKjT1hZwxjlFgQ9qvQA==", + "version": "18.19.45", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.45.tgz", + "integrity": "sha512-VZxPKNNhjKmaC1SUYowuXSRSMGyQGmQjvvA1xE4QZ0xce2kLtEhPDS+kqpCPBZYgqblCLQ2DAjSzmgCM5auvhA==", "dev": true, "dependencies": { "undici-types": "~5.26.4" diff --git a/packages/api/Dockerfile b/packages/api/Dockerfile index 375081e..b475f0c 100644 --- a/packages/api/Dockerfile +++ b/packages/api/Dockerfile @@ -9,8 +9,9 @@ WORKDIR /usr/src/app COPY package*.json ./ COPY server.js ./ +# Webpack will build modules into the production bundle so we don't need npm install anymore # If you are building your code for production -RUN npm install --omit=dev --only=production +# RUN npm install --omit=dev --only=production EXPOSE 3000 CMD [ "node", "server.js" ] diff --git a/packages/api/package.json b/packages/api/package.json index 0cef9df..669fb7a 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -44,7 +44,7 @@ "ts-loader": "^9.4.2", "ts-node": "^10.9.1", "ts-prune": "^0.10.3", - "webpack": "^5.88.2", + "webpack": "^5.94.0", "webpack-cli": "^5.1.4", "websocket": "^1.0.34" }, diff --git a/packages/api/src/server.ts b/packages/api/src/server.ts index 6d88cf6..dfeeb63 100644 --- a/packages/api/src/server.ts +++ b/packages/api/src/server.ts @@ -19,7 +19,7 @@ import mongoose from "mongoose"; import { WebSocketServer } from "ws"; import { ValueType, metrics } from "@opentelemetry/api"; -// mongoose.set('debug', true); +// mongoose.set('debug', true); // log.info(`System starting in ${process.env.NODE_ENV}`); diff --git a/packages/api/webpack.common.js b/packages/api/webpack.common.js index f70078b..9f91a3b 100644 --- a/packages/api/webpack.common.js +++ b/packages/api/webpack.common.js @@ -1,39 +1,38 @@ -var fs = require('fs'); +const fs = require("fs"); // forces all node modules to be treated as externals // borrowed from http://jlongster.com/Backend-Apps-with-Webpack--Part-I var nodeModules = {}; -fs.readdirSync('node_modules').filter(function(x) { - return ['.bin'].indexOf(x) === -1; -}) -.forEach(function(mod) { - nodeModules[mod] = 'commonjs ' + mod; -}); +fs.readdirSync("node_modules") + .filter(function (x) { + return [".bin"].indexOf(x) === -1; + }) + .forEach(function (mod) { + nodeModules[mod] = "commonjs " + mod; + }); module.exports = { entry: { - 'server': './src/server.ts' + server: "./src/server.ts", }, - target: 'node', + target: "node", output: { - path: __dirname + '/.', - filename: '[name].js', - chunkFilename: '[id].chunk.js' + path: __dirname + "/.", + filename: "[name].js", + chunkFilename: "[id].chunk.js", }, resolve: { - extensions: ['.webpack.js', '.web.js', '.ts', '.js'] + extensions: [".webpack.js", ".web.js", ".ts", ".js"], }, - devtool: 'eval-source-map', + devtool: "eval-source-map", module: { rules: [ { test: /\.ts$/, - use: [ - {loader: 'ts-loader' } - ] - } - ] + use: [{ loader: "ts-loader" }], + }, + ], }, externals: nodeModules, ignoreWarnings: [ @@ -47,5 +46,10 @@ module.exports = { message: /Critical dependency: the request of a dependency is an expression/, }, + { + // don't error out over peer dependencies we're not using + module: /mongodb|ws/, + message: /Module not found: Error: Can't resolve/, + }, ], }; diff --git a/packages/api/webpack.prod.js b/packages/api/webpack.prod.js index 2a5c148..20c4850 100644 --- a/packages/api/webpack.prod.js +++ b/packages/api/webpack.prod.js @@ -11,3 +11,7 @@ module.exports = merge(common, { }), ], }); + +// build the prod js with tree shaking node modules +// to avoid installing in the docker image +delete module.exports.externals;