serverless-bundle icon indicating copy to clipboard operation
serverless-bundle copied to clipboard

Making sourcetype unambiguous to support commonjs in addition

Open KeKs0r opened this issue 6 years ago • 8 comments

Hi,

I understand that the configuration is mainly setup to support es6 module syntax. I think it would be great to additionally not break working commonjs.

Webpack itself supports it to a certain degree: You can mix require and export. You can't mix import and module.exports. https://github.com/webpack/webpack/issues/4039#issuecomment-273804003

The problem is that babel-transform is transforming require statements to imports: https://stackoverflow.com/a/56283408/1602476

This could be prevented (see stackoverflow answer) by setting:

"sourceType": "unambiguous"

Would this be something that could potentially be supported? Since I think it would not break existing setups, but would also allow commonjs to be supported additionally.

KeKs0r avatar Oct 21 '19 12:10 KeKs0r

Oh that is interesting. I'll test it out and see how it does. Thanks for bringing it up.

jayair avatar Nov 02 '19 22:11 jayair

@KeKs0r I had a look and I think we could do this. Can I bother you to put a test case together. Something that needs the source type added for the build to work?

jayair avatar Nov 03 '19 17:11 jayair

I found a workaround by creating a .babelrc:

{
  "sourceType": "unambiguous"
}

and its picked up by babel-transform, which resolves the issue. I think this can be closed and potntially just documented, that .babelrc can be used with this plugin.

KeKs0r avatar Nov 12 '19 12:11 KeKs0r

Hmm interesting. I still think your original suggestion of handling this is a good idea. Since it shouldn't break existing configs.

Edit: I'll leave this open for now. If somebody finds some time to put a test case together, that would really help. Then we can put in a update to handle that.

jayair avatar Nov 17 '19 17:11 jayair

Agree with @KeKs0r that this should be documented. We've been running a fork of serverless-bundle internally for a bit because this issue hadn't surfaced during our searches.

btw, thanks for sharing your solution. Really appreciate it!

MrChrisRodriguez avatar Jun 24 '22 16:06 MrChrisRodriguez

@KeKs0r where did you drop that .babelrc file? I tried dropping it in the same dir as my serverless.yaml file and serverless-bundle did not use it. I'm running serverless-bundle 4.4.0

Zambonilli avatar Dec 21 '22 21:12 Zambonilli

For anyone else hitting this, you can manually update your version of serverless-bundle and add sourceType: "unambiguous" to the babel-loader config here.

https://github.com/AnomalyInnovations/serverless-bundle/blob/master/src/webpack.config.js#L157

Zambonilli avatar Dec 21 '22 22:12 Zambonilli

I was only ever able to get @KeKs0r's answer to work with a babel.config.json file not a .babelrc file in the same directory as the serverless.yaml file that is using serverless-bundle.

Zambonilli avatar Feb 06 '23 22:02 Zambonilli