Skip to content
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

ES bundle uses CommonJS-style exports. #6678

Closed
TimUnderhay opened this issue Dec 7, 2020 · 11 comments
Closed

ES bundle uses CommonJS-style exports. #6678

TimUnderhay opened this issue Dec 7, 2020 · 11 comments

Comments

@TimUnderhay
Copy link

TimUnderhay commented Dec 7, 2020

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...

  1. 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

@TimUnderhay TimUnderhay changed the title ES bundle uses CommonJS format. ES bundle uses CommonJS-style exports. Dec 7, 2020
@tim-lai
Copy link
Contributor

tim-lai commented Dec 9, 2020

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

@TimUnderhay
Copy link
Author

TimUnderhay commented Dec 9, 2020

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.

@tim-lai
Copy link
Contributor

tim-lai commented Dec 10, 2020

Thanks for the additional info. The ES6 distribution needs to get fixed.

@linelson
Copy link
Contributor

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:
image
after adding swagger-ui-react:
image

@TimUnderhay
Copy link
Author

@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 @ #6720 so hopefully it won't be too much longer.

@andy-han
Copy link

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.

@rafi0101
Copy link

Ping.
Any updates to this topic?

@ShubhamJainSJ
Copy link

Any updates for this issue? It's been quite sometime.

@philipwhiuk
Copy link

philipwhiuk commented Oct 10, 2023

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

@char0n
Copy link
Member

char0n commented Apr 30, 2024

Hi everybody,

Some aspect of this issue has already been addressed. Here is the full documentation of what each of the fragment represents and what module system is used: #7831. I know the naming is very unfortunate, but we there is a plan to fix it as described in #7831.

swagger-ui-es-bundle-core.js is for some time now a true ES build fragment.

Here is the current mapping of the fragments in package.json file:

  "exports": {
    "./dist/swagger-ui.css": "./dist/swagger-ui.css",
    "./dist/oauth2-redirect.html": "./dist/oauth2-redirect.html",
    "./dist/swagger-ui-standalone-preset": "./dist/swagger-ui-standalone-preset.js",
    ".": {
      "browser": {
        "import": "./dist/swagger-ui-es-bundle-core.js",
        "require": "./dist/swagger-ui.js"
      },
      "node": {
        "import": "./dist/swagger-ui-bundle.js",
        "require": "./dist/swagger-ui-es-bundle.js"
      },
      "default": {
        "import": "./dist/swagger-ui-bundle.js",
        "require": "./dist/swagger-ui-es-bundle.js"
      }
    }
  },

@char0n char0n self-assigned this Apr 30, 2024
@char0n
Copy link
Member

char0n commented Apr 30, 2024

@philipwhiuk,

It's too few information to help you. I would need to verify that you'll still seeing the issue and provide additional info about your build system or create a POC repo where this can be reproduced.

We maintain POC integration repos in https://github.com/swagger-api/swagger-ui/wiki/Seamless-Integration-with-Bundlers-&-Test-Tools

Please reopen this issue if you feel it's needed.

@char0n char0n closed this as completed Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants