Making sourcetype unambiguous to support commonjs in addition
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.
Oh that is interesting. I'll test it out and see how it does. Thanks for bringing it up.
@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?
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.
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.
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!
@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
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
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.