I have a nestjs app and I need to bundle it with webpack. Bundle should containt the whole script with all files. So I created my webpack configuration:
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: {
backend: './src/index.ts',
},
target: 'node',
module: {
rules: [
{
test: /.ts$/,
use: 'ts-loader',
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'webpack-graphql-loader',
},
{
test: /\.mjs$/,
include: /node_modules/,
type: "javascript/auto",
}
],
},
externals: ['fsevents'],
mode: 'production',
resolve: {
extensions: ['.ts', '.mjs', '.js', '.json'],
},
output: {
path: path.join(__dirname, 'bundled'),
library: 'handler',
libraryTarget: "commonjs2"
},
plugins: [
new webpack.IgnorePlugin({
checkResource(resource) {
const lazyImports = [
'@nestjs/microservices',
'cache-manager',
'@nestjs/microservices/microservices-module',
'@nestjs/websockets/socket-module',
'class-transformer',
'apollo-server-fastify',
];
if (!lazyImports.includes(resource)) {
return false;
}
try {
require.resolve(resource);
} catch (err) {
return true;
}
return false;
},
}),
],
};
It seems it works fine but when I change my code a little it started to throw a strange error.
Error: Nest can't resolve dependencies of the r (?). Please make sure that the argument r at index [0] is available in the s context.
Potential solutions: - If r is a provider, is it part of the current s? - If r is exported from a separate @Module, is that module imported within s? @Module({ imports: [ /* the Module containing r */ ] })
So I tried to represent minimal code example and found when error occured. The problem in logger
import { Injectable, Logger } from '@nestjs/common';
@Injectable()
export class SecondService {
private readonly logger = new Logger(SecondService.name); // this line throw an error
}
So, if you just comment logger declaration - everything will start work. But it is strange because I have FirstController
which use FirstService
(which use logger too but doesn't throw an error) which use SecondService
(which use logger and throw an error).
I think that it is better to use my repository than pass code here: https://github.com/baleksandr48/webpack_for_nestjs
For running use:
npm run bundle && node -e "require('./bundled/backend.js').handler()"
Please, help me to solve this strange issue