Skip to content

Conversation

@tim-lai
Copy link
Contributor

@tim-lai tim-lai commented Aug 8, 2020

Description

  • es2015 bundle with dependencies via npm script build:es:bundle
  • es2015 bundle without dependencies via npm script build:es:bundle
  • jest dependency for test:artifact
  • migrate babel.config.json to babel.config.js and maintain env support for commonjs and es2015
  • remove polyfill
  • fix es syntax: replace module.exports with export
  • fix es syntax: remove .jsx extension inside import

removing the polyfill also reduces bundle size, now down to 956 KiB

Motivation and Context

swagger-ui should be available as an es2015 bundle. This will allow swagger-editor to avoid re-bundling dependencies that it does not directly use, e.g. highlight.js.

However, swagger-editor still expects swagger-ui to exist as a bundle. Therefore, full tree-shaking is not currently possible.

How Has This Been Tested?

New Jest tests to confirm that production bundles export as a function.

Note: known issue that these Jest tests pass locally on Node 10/12/13, but not on Jenkins. As a result, test:artifact is not currently included in the test script.

swagger-editor build is reduced from 4+ MiB (cjs) to 3.05 MiB (es), even with react-syntax-highlighter.

Screenshots (if appropriate):

Checklist

My PR contains...

  • No code changes (src/ is unmodified: changes to documentation, CI, metadata, etc.)
  • Dependency changes (any modification to dependencies in package.json)
  • Bug fixes (non-breaking change which fixes an issue)
  • Improvements (misc. changes to existing features)
  • Features (non-breaking change which adds functionality)

My changes...

  • are breaking changes to a public API (config options, System API, major UI change, etc).
  • are breaking changes to a private API (Redux, component props, utility functions, etc.).
  • are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
  • are not breaking changes.

Documentation

  • My changes do not require a change to the project documentation.
  • My changes require a change to the project documentation.
  • If yes to above: I have updated the documentation accordingly.

Automated tests

  • My changes can not or do not need to be tested.
  • My changes can and should be tested by unit and/or integration tests.
  • If yes to above: I have added tests to cover my changes.
  • If yes to above: I have taken care to cover edge cases in my tests.
  • All new and existing tests passed.

@tim-lai
Copy link
Contributor Author

tim-lai commented Aug 8, 2020

current status: the two test files from 33d9c54 do not work.

> cross-env BABEL_ENV=test mocha "test/mocha/build-artifacts/*.{js,jsx}"


/Users/timothy.lai/Repos/swagger-ui/swagger-config.yaml:1
---
  ^

ReferenceError: Invalid left-hand side expression in prefix operation
    at Object.compileFunction (vm.js:406:10)
    at Generator.next (<anonymous>)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:104:7)
    at Object.<anonymous> (/Users/timothy.lai/Repos/swagger-ui/src/core/plugins/configs/index.js:1)
    at Generator.next (<anonymous>)
    at Object._compile (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:104:7)
    at Object.<anonymous> (/Users/timothy.lai/Repos/swagger-ui/lib/core/presets/base.js:26:39)
    at Generator.next (<anonymous>)
    at Object._compile (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:104:7)
    at Object.<anonymous> (/Users/timothy.lai/Repos/swagger-ui/lib/core/presets/apis.js:6:36)
    at Generator.next (<anonymous>)
    at Object._compile (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:104:7)
    at Object.<anonymous> (/Users/timothy.lai/Repos/swagger-ui/lib/commonjs.js:3:36)
    at Generator.next (<anonymous>)
    at Object._compile (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:104:7)
    at Object.<anonymous> (/Users/timothy.lai/Repos/swagger-ui/test/mocha/build-artifacts/commonjs.js:3:19)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object._compile (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (/Users/timothy.lai/Repos/swagger-ui/node_modules/pirates/lib/index.js:104:7)
    at /Users/timothy.lai/Repos/swagger-ui/node_modules/mocha/lib/mocha.js:349:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/Users/timothy.lai/Repos/swagger-ui/node_modules/mocha/lib/mocha.js:346:14)
    at /Users/timothy.lai/Repos/swagger-ui/node_modules/mocha/lib/mocha.js:382:12
    at new Promise (<anonymous>)
    at Mocha.loadFilesAsync (/Users/timothy.lai/Repos/swagger-ui/node_modules/mocha/lib/mocha.js:381:12)
    at singleRun (/Users/timothy.lai/Repos/swagger-ui/node_modules/mocha/lib/cli/run-helpers.js:149:15)
    at exports.runMocha (/Users/timothy.lai/Repos/swagger-ui/node_modules/mocha/lib/cli/run-helpers.js:186:11)
    at Object.exports.handler (/Users/timothy.lai/Repos/swagger-ui/node_modules/mocha/lib/cli/run.js:319:11)
    at innerArgv.then.argv (/Users/timothy.lai/Repos/swagger-ui/node_modules/yargs/lib/command.js:241:49)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

tim-lai added 29 commits August 13, 2020 16:58
* also change config file type from .json to .js
* mocha test updated for bundled versions
* restore entry point for main
* include es scripts to script: build
* output exceeds assetSizeLimits, yields jest errors
@tim-lai tim-lai force-pushed the feat/build-artifacts branch from 0f10c7f to b06407a Compare August 13, 2020 23:59
@tim-lai tim-lai changed the title Feat/build artifacts feat(build): es2015 bundle artifact Aug 14, 2020
@tim-lai tim-lai marked this pull request as ready for review August 14, 2020 00:17
@tim-lai tim-lai merged commit 2eaa6c1 into swagger-api:master Aug 14, 2020
@TimUnderhay
Copy link

@tim-lai I noticed that the es module still appears to be using CommonJS format, i.e. module.exports. I guess I expected it to be using 'export XXX' format. Am I missing something? Just wanted to ask you before submitting a bug report.

module.exports=function(e){var t={};function n(r){if(t[r])ret...

@marvinthepa marvinthepa mentioned this pull request Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants