swagger-ui
swagger-ui copied to clipboard
ES bundle uses CommonJS-style exports.
Q&A (please complete the following information)
- Swagger-UI version: 3.37.2
- Swagger/OpenAPI version: N/A
Content & configuration
Apologies if this isn't considered a bug, but I couldn't find anything clearly explaining this. The ES bundle artefacts still use CommonJ-style exports. I expected that they would be utilising ES6/ES2015-style exports, for use in frameworks like Angular and others that prefer the ES format over CommonJS, for tree-shaking purposes.
swagger-ui-es-bundle.js:
module.exports=function(e){var t={};function...
Should be something like:
export default function(e){var t={};function...
Example Swagger/OpenAPI definition: N/A
Swagger-UI configuration options: N/A
Describe the bug you're encountering
To reproduce...
- Have a look at swagger-ui-es-bundle.js
Expected behavior
ES module should use ES-style exports.
Screenshots
N/A
Additional context or thoughts
Hi, is your application not able to tree-shake swagger-ui? As an example, swagger-editor uses and tree-shakes the swagger-ui-es-bundle.
https://github.com/swagger-api/swagger-editor
Hi @tim-lai,
When compiling Swagger UI into an Angular app using the Angular CLI, it outputs this warning:
Warning: /workspace/src/app/my-app/my-component.ts depends on 'swagger-ui'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
** Angular Live Development Server is listening on xxx:yyy, open your browser on http://xxx:yyy/ **
✔ Compiled successfully.
From the link it provides, it states:
"It is recommended that you avoid depending on CommonJS modules in your Angular applications. Depending on CommonJS modules can prevent bundlers and minifiers from optimizing your application, which results in larger bundle sizes. Instead, it is recommended that you use ECMAScript modules in your entire application. For more information, see How CommonJS is making your bundles larger."
How CommonJS is making your bundles larger: https://web.dev/commonjs-larger-bundles/
Now I can, of course, ignore or suppress the warning, but it would be better for the ES bundle to utilise ECMAScript exports so that the best optimisation possible can occur, at least IMHO.
Thanks for the additional info. The ES6 distribution needs to get fixed.
Also experiencing this issue with the swagger-ui-react package in a project with a webpack 5 build. Adds ~2.4mb to our pre-gzip bundle size and nearly a megabyte post-gzip because it is not tree-shaking dependencies:
From a bundle analyzer before adding swagger-ui-react:
after adding swagger-ui-react:

@linelson as a temporary workaround, I've published a version with a corrected ES bundle. I intend to unpublish as soon as this is issue has been fixed, but should you wish to use it, it's @kensingtontech/swagger-ui. I see that there is a PR in progress @ https://github.com/swagger-api/swagger-ui/pull/6720 so hopefully it won't be too much longer.
Hello, is there any update to this? My organization has pretty strict compliance with our angular app, and this issue is preventing us from using this package.
Ping. Any updates to this topic?
Any updates for this issue? It's been quite sometime.
I think this is why building swagger breaks for me following an upgrade:
[16:07:18] Error:SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (2:0) while parsing
<PROJECT_PATH>\node_modules\swagger-ui-react\swagger-ui-es-bundle-core.js
while parsing file: <PROJECT_PATH>\node_modules\swagger-ui-react\swagger-ui-es-bundle-core.js