Skip to content

Commit

Permalink
Change webpack config.
Browse files Browse the repository at this point in the history
Changed webpack config to build only one single JS file that which even does not require the `node_modules` directory.
  • Loading branch information
samchon committed Nov 17, 2023
1 parent d3724c1 commit 72c4d54
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@typescript-eslint/parser": "^5.26.0",
"chalk": "^4.1.2",
"cli": "^1.0.1",
"copy-webpack-plugin": "^11.0.0",
"eslint-plugin-deprecation": "^1.4.1",
"express": "^4.18.2",
"inquirer": "^8.2.5",
Expand All @@ -85,7 +86,7 @@
"typescript-transform-paths": "^3.4.6",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4",
"webpack-node-externals": "^3.0.0"
"write-file-webpack-plugin": "^4.5.1"
},
"dependencies": {
"@nestia/core": "^2.3.9",
Expand Down
43 changes: 41 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
const path = require("path");
const nodeExternals = require("webpack-node-externals");

const CopyWebpackPlugin = require("copy-webpack-plugin");
const WriteFilePlugin = require("write-file-webpack-plugin");
const { IgnorePlugin } = require("webpack");

const lazyImports = [
"@fastify/static",
"@fastify/view",
"@nestjs/microservices",
"@nestjs/websockets",
"class-transformer",
"class-validator",
];

// @reference https://tech-blog.s-yoshiki.com/entry/297
module.exports = {
// CUSTOMIZE HERE
entry: {
Expand All @@ -17,7 +30,6 @@ module.exports = {
},

// JUST KEEP THEM
externals: [nodeExternals()],
mode: "production",
target: "node",
module: {
Expand All @@ -32,4 +44,31 @@ module.exports = {
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: ".env",
to: "[name][ext]",
},
{
from: "./node_modules/.prisma/client/*.node",
to: () => Promise.resolve("[path][name][ext]"),
},
],
}),
new WriteFilePlugin(),
new IgnorePlugin({
checkResource: (resource) => {
if (lazyImports.some((modulo) => resource.startsWith(modulo))) {
try {
require.resolve(resource);
} catch (err) {
return true;
}
}
return false;
},
}),
],
};

0 comments on commit 72c4d54

Please sign in to comment.