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

feat: introduce tsup for bundling #25

Merged
merged 3 commits into from
Feb 19, 2022
Merged

feat: introduce tsup for bundling #25

merged 3 commits into from
Feb 19, 2022

Conversation

weyert
Copy link
Collaborator

@weyert weyert commented Feb 1, 2022

Added the package tsup to assist in bundling @mswjs/source-package so
it both supports CommonJS and ESM modules. The package.json file has
been modified so the appropriate properties for the modern exports-approach
of exporting, and the older main-module combination.

During building and running the tests the fromOpenApi had issues
to pass the tests or build due to the way the SwaggerParser utility
class was exporter. The code has been updated so it refers to
the deferences-function via SwaggerParser.deferences() instead of trying
to instantiate the class.

For this reason the tsconfig.json has been updated so that it enables
esModuleInterop and allowSyntheticDefaultImports after this change
the package build and bundled successfully with tsup and passed the
unit tests, before errors like below were shown:

    src/fromOpenApi/fromOpenApi.ts:16:35 - error TS2351: This expression is not constructable.
      Type 'typeof SwaggerParser' has no construct signatures.

    16   const specification = await new SwaggerParser().dereference(document)
                                         ~~~~~~~~~~~~~

@weyert weyert requested a review from kettanaito February 1, 2022 01:24
@weyert
Copy link
Collaborator Author

weyert commented Feb 1, 2022

I have tested these changes by run the build-command and copy the contents of the generated lib-directory to the lib-directory in my project that uses this package and then wrote the following Jest unit test:

import { fromTraffic } from '@mswjs/source'
import { default as json } from './__data__/health-check.json'

  it.only('should throw exception when all services are unavailable', async () => {
    server.use(
      ...fromTraffic(json),
      // rest.get('https://localhost:30001/-/health', (_req, res, _ctx) => {
      //   res.networkError('Network Error')
      // }),
      // rest.get('https://localhost:30002/-/health', (_req, res, _ctx) => {
      //   res.networkError('Network Error')
      // }),
    )

    await expect(controller.check()).rejects.toThrow('Service Unavailable Exception')
  })

For me, after these changes, the above unit test was passing without errors. The project is a Nest.js project using Typescript.

Note I currently don't have a ESM only project ready to test if the ESM module part of the package works as expected. I will try to do make one tomorrow.

package.json Outdated Show resolved Hide resolved
@kettanaito
Copy link
Member

It'd be great to have automated tests in different formats (CJS/ESM) that help us catch regressions regarding module format incompatibilities.

@kettanaito kettanaito assigned weyert and unassigned kettanaito Feb 1, 2022
@kettanaito kettanaito force-pushed the fix-bundling-package branch from 508ae8e to 48a02f4 Compare February 1, 2022 14:10
@kettanaito
Copy link
Member

@weyert, I'm sorry, I think I've just messed up the commit authorship after rewording the commit. Could you please git commit --amend --author=X with your proper name + GitHub email? Thanks.

Added the package `tsup` to assist in bundling `@mswjs/source`-package so
it both supports CommonJS and ESM modules. The `package.json` file has
been modified so the appropriate properties for the modern `exports`-approach
of exporting, and the older `main`-`module` combination.

During building and running the tests the `fromOpenApi` had issues
to pass the tests or build due to the way the `SwaggerParser` utility
class was exporter. The code has been updated so it refers to
the `deferences`-function via `SwaggerParser.deferences()` instead of trying
to instantiate the class.

For this reason the `tsconfig.json` has been updated so that it enables
`esModuleInterop` and `allowSyntheticDefaultImports` after this change
the package build and bundled successfully with `tsup` and passed the
unit tests, before errors like below were shown:

    src/fromOpenApi/fromOpenApi.ts:16:35 - error TS2351: This expression is not constructable.
      Type 'typeof SwaggerParser' has no construct signatures.

    16   const specification = await new SwaggerParser().dereference(document)
                                         ~~~~~~~~~~~~~
@weyert weyert force-pushed the fix-bundling-package branch from 48a02f4 to db1b96a Compare February 3, 2022 11:41
@weyert
Copy link
Collaborator Author

weyert commented Feb 3, 2022

@weyert, I'm sorry, I think I've just messed up the commit authorship after rewording the commit. Could you please git commit --amend --author=X with your proper name + GitHub email? Thanks.

I think I did now but not 100% sure

@weyert
Copy link
Collaborator Author

weyert commented Feb 3, 2022

It'd be great to have automated tests in different formats (CJS/ESM) that help us catch regressions regarding module format incompatibilities.

Yeah, good idea, I am not sure what the best approach is. Maybe a simple script that .mjs (for ESM) and .js (for CJS) then tries to load the package?

@kettanaito
Copy link
Member

@weyert, you've set the commit author to @tapico-weyert. If it's the expected result then everything is okay!

@kettanaito kettanaito merged commit 06329f7 into main Feb 19, 2022
@kettanaito kettanaito deleted the fix-bundling-package branch February 19, 2022 23:33
@kettanaito
Copy link
Member

Released: v0.1.0 🎉

This has been released in v0.1.0!

Make sure to always update to the latest version (npm i @mswjs/source@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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