Skip to content

Commit

Permalink
feat: bundle modules with server js instead of installing in docker (#…
Browse files Browse the repository at this point in the history
…283)

* feat: bundle modules with server js instead of installing in docker
  • Loading branch information
micahg authored Aug 25, 2024
1 parent 2a1edd8 commit 9ae38a5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 43 deletions.
32 changes: 11 additions & 21 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion packages/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand Down
42 changes: 23 additions & 19 deletions packages/api/webpack.common.js
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -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/,
},
],
};
4 changes: 4 additions & 0 deletions packages/api/webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 9ae38a5

Please sign in to comment.