Serverless-offline: In `alpha-13`, the code executed is not the one transpiled by Webpack / Babel

Created on 18 Aug 2019  路  9Comments  路  Source: dherault/serverless-offline

On alpha-13, the code executed doesn't seem to be the one transpiled by Babel via Webpack (serverless-webpack plugin). The .webpack folder contains the correct compiled sources, and serverless offline does compile them, but it seems like what's being executed is the original file :

$ yarn sls offline
Serverless: Bundling with Webpack...
Time: 5219ms
Built at: 08/19/2019 1:37:15 AM
                   Asset      Size                 Chunks             Chunk Names
          example/app.js  2.49 MiB            example/app  [emitted]  example/app
src/functions/graphql.js     4 MiB  src/functions/graphql  [emitted]  src/functions/graphql
Entrypoint src/functions/graphql = src/functions/graphql.js
Entrypoint example/app = example/app.js
[./example/app.js] 5.39 KiB {example/app} [built]

# webpack stuff

[./src/functions/graphql.js] 3.74 KiB {src/functions/graphql} [built]
    + 1150 hidden modules
Serverless: Watching for changes...
offline: Starting Offline: prod/us-east-1.
offline: 
offline: [HTTP] server ready: http://localhost:3000 馃殌
offline: 
offline: OpenAPI/Swagger documentation:
offline: [GET] http://localhost:3000/documentation
offline: 
offline: Enter "rp" to replay the last request
offline: 
offline: [ANY] http://localhost:3000/graphql
offline: [ANY] http://localhost:3000/
offline: [ANY] http://localhost:3000/{any*}

offline: ANY / (位: example)
/Users/verekia/Local/Code/sharyn-boilerplate-serverless/example/app.js:117
export const main = async (event: Object, context: Object) => {
^^^^^^

SyntaxError: Unexpected token export

The exact same code works with 5.10.1.

Most helpful comment

Confirming that the fix works in both the demo repository and in my own project. Thank you.

All 9 comments

thank you @verekia for filing the issue! that's interesting.

The .webpack folder contains the correct compiled sources, and serverless offline does compile them,

I think serverless-webpack + Babel is compiling your code, not serverless-offline.

I'm guessing serverless-webpack "overwrites" the path to the handler, which has a different location for the compiled files.

could you create a small repository so I can debug this issue?

Here it is @dnalborczyk , in its most minimal form!

https://github.com/verekia/serverless-offline-alpha-webpack-bug

I think serverless-webpack + Babel is compiling your code, not serverless-offline.

True, what I meant is that serverless-offline correctly triggers the Webpack compilation, but it seems to use the wrong file after that.

similar issue is happening to me as well with serverless-webpack.

configuration that works on latest 5.x.x, doesn't work on 6 latest alpha.

running sls offline does generate bundled code in .webpack folder, but i'm getting this error while accessing the function: "Error: Cannot find module 'path/to/original/handler/path'"

seems like serverless-offline is trying to require the original module

Here it is @dnalborczyk , in its most minimal form!

https://github.com/verekia/serverless-offline-alpha-webpack-bug

thank you @verekia Looking into the issue.

Thank you for the quick fix! Looking forward to the release.
Keep up the great work 馃憤

Confirming that the fix works in both the demo repository and in my own project. Thank you.

This one fixes not only the webpack+babel integration, but typescript as well. However when I tried to run the project that has custom authorizers configured (so one function refers to another using the second as an authorizer), second is still loaded from the /src/.. directory because HttpServer also uses servicePath from options. Then createAuthScheme uses this path in:

https://github.com/hsz/serverless-offline/blob/4956230912f26d40f0fea78ab5fe0dbdbfe4085f/src/events/http/createAuthScheme.js#L48

Reusing the solution from https://github.com/dherault/serverless-offline/commit/e301a2d00315fb1a7eaa876faa6287a8169ce6e6 fixes this custom authorizer problem as well.

I've created a PR for that:

https://github.com/dherault/serverless-offline/pull/835

Stilll an issue with "serverless-offline": "^6.8.0",

It would be nice to see any configuration changes that could sollve this, the documentation simply refers to using serverless-webpack

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adambiggs picture adambiggs  路  4Comments

davidroman0O picture davidroman0O  路  4Comments

Looveh picture Looveh  路  4Comments

stunningpixels picture stunningpixels  路  3Comments

JimLynchCodes picture JimLynchCodes  路  4Comments