-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
serverless-middleware incompatibility with serverless-webpack #52
Comments
Hi @rezaerami , Thanks for the extensive description. I've used As you have noticed, middleware works by create a single handler that imports all the middlewares and chain them into a single handler as expected by serverless. So, Reading at the issue, the problem here seems to be: why can't webpack find the imported modules?
File extensions were introduced in version 3.2 to enable support for esm modules. Node is flexible with the extensions but EcmaScript requires etensions.
What do you mean?
Why are you using both plugins?
I don't understand this. If you create a sample project for me to replicate I can try to help more. |
not sure if you have used them with TS before cause JS works fine. The reason webpack can't find JS modules is that they are not created yet by webpack itself.
but since those js files (imports inside wrapped function file in .middleware) are not created by webpack yet, when it reaches to middleware TS file, it tries to import them but it can't so it puts module not found in the bundled middleware file,
here is my webpack config const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const path = require('path');
const webpackConfig = () => {
console.log('beginning of webpack build', slsw.lib.entries);
return {
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: {
...slsw.lib.entries,
'src/middlewares/errorHandler': './src/middlewares/errorHandler.js',
'src/middlewares/logger': './src/middlewares/logger.js',
},
target: 'node',
externals: [nodeExternals()],
resolve: {
extensions: ['.ts', '.js'],
alias: {
'@': path.resolve(__dirname, 'src/'),
},
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
},
{
test: /\.js$/,
use: 'babel-loader',
},
],
},
};
};
module.exports = webpackConfig();
let's say if I could make middleware create functions on different directories pre:
- .webpack/services/path/to/middleware.foo then the import for middleware in wrapped handler file will also look like this
this is again in case we could put webpack before middleware which is also not an option anymore here is the sample project: make sure to run yarn and then you can test that function with node 18x is required |
Alright! Thanks for the sample repo. Your problem is, as expected, it related with version 3.2.0 of middleware adding the You can read more about the benefits of adding the The problem is that Your webpack.config.js should look like: const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const path = require('path');
const webpackConfig = () => {
console.log('beginning of webpack build', slsw.lib.entries);
return {
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
target: 'node',
externals: [nodeExternals()],
resolve: {
extensions: ['.ts', '.js'],
// This is the key. This tells webpack to load files with js extension as ts
extensionAlias: {
'.js': ['.js', '.ts'],
},
alias: {
'@': path.resolve(__dirname, 'src/'),
},
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
},
// You don't need this anymore as everything will be loaded using ts-loader.
// {
// test: /\.js$/,
// use: 'babel-loader',
// },
],
},
};
};
module.exports = webpackConfig(); btw, your project has some incompatible dependencies that you should probably look into:
|
cool, by adding webpack but I highly suggest to add these to documentation thanks again, I'll close the issue ps: example repo has been removed (for users seeing this issue in future) |
I've been trying to use middleware with
serverless-webpack
and encountered some issues that almost make it impossible to combineserverless-middleware
,serverless-webpack
, and TypeScript.The problem arises when placing
serverless-middleware
beforeserverless-webpack
in theplugins
list withinserverless.yml
. This causes the function's entry point to change from/path/to/invoked.handler
to.middleware/functionxxx/
, a clever behavior. However, a hurdle emerges: the outcome of the middleware contains imports like:Later, when Webpack attempts to bundle these files, it results in an error. The cause of the error is that, unlike
serverless-typescript-plugin
which transpiles TypeScript to JavaScript and doesn't mind if files exist or not, Webpack expects JS files and throws aMODULE NOT FOUND
error.To address this, I need to either transpile those files using
tsc
to create JS files, or set up a separate Webpack build to bundle them into JS before invokingserverless-middleware
. This way, when the actualserverless-webpack
processes theserverless-middleware
-wrapped functions, it encounters JS files and avoids theMODULE NOT FOUND
error.Another approach is to reorder the plugins, placing
serverless-webpack
afterserverless-plugin-typescript
. This resolves the issue by generating JS files, but it contradicts the initial intent of usingserverless-webpack
to avoid the overhead introduced byserverless-plugin-typescript
.A potential solution might involve modifying how files are imported in
serverless-middleware
wrapped functions, and removing the file extension to ensure compatibility with middleware placement. Additionally, ifserverless-middleware
could be configured to read bundled files by specifying the root, it might offer a cleaner solution.For now, I've positioned
serverless-middleware
afterserverless-plugin-typescript
, though this creates unnecessary overhead due to middleware wrapping, building, and bundling in sequence, which doesn't seem ideal.also, there should be a way to include middleware in webpack build, by reading middleware directory or providing them from middleware plugin to webpack cause right now my approach to include middleware in webpack is by adding them to entry object in webpack, it's not necessary to do it if serverless-middleware was mentioned after serverless-typescrypt-plugin (which is not a good practice at all)
The text was updated successfully, but these errors were encountered: