Skip to content

Commit

Permalink
feat: introduce tsup for bundling (#25)
Browse files Browse the repository at this point in the history
* chore: introduce tsup for bundling

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)
                                         ~~~~~~~~~~~~~

* chore: remove unused scripts

* chore: use "tsup" for local development

Co-authored-by: Weyert de Boer <weyert@innerfuse.biz>
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
  • Loading branch information
3 people authored Feb 19, 2022
1 parent 0b06aec commit 06329f7
Show file tree
Hide file tree
Showing 4 changed files with 325 additions and 17 deletions.
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
"name": "@mswjs/source",
"version": "0.0.0",
"description": "Generate request handlers for Mock Service Worker from various sources.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
"exports": {
".": {
"require": "./lib/index.cjs",
"import": "./lib/index.mjs"
}
},
"repository": "git@github.com:mswjs/source.git",
"author": "Artem Zakharchenko <kettanaito@gmail.com>",
"license": "MIT",
"scripts": {
"start": "tsc -w",
"start": "yarn build --watch",
"lint": "eslint ./{src,test}/**/*.ts",
"har:fixture": "ts-node test/traffic/fixtures/requests/command.ts",
"clean": "rimraf ./lib",
"build": "yarn lint && yarn clean && tsc",
"prebuild": "yarn lint",
"build": "tsup src/index.ts --format cjs,esm --out-dir lib --dts --clean",
"test": "jest",
"prepublishOnly": "yarn build && yarn test:lib && yarn test"
"prepublishOnly": "yarn test && yarn build"
},
"files": [
"lib",
Expand Down Expand Up @@ -59,6 +66,7 @@
"ts-jest": "26.5.6",
"ts-node": "^10.2.0",
"tslib": "^2.3.1",
"tsup": "^5.11.13",
"typescript": "^4.3.5"
},
"dependencies": {
Expand Down
6 changes: 2 additions & 4 deletions src/fromOpenApi/fromOpenApi.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import { RequestHandler, rest } from 'msw'
import { OpenAPIV3, OpenAPIV2 } from 'openapi-types'
import * as SwaggerParser from '@apidevtools/swagger-parser'
import SwaggerParser from '@apidevtools/swagger-parser'
import { createResponseResolver } from './response/createResponseResolver'
import { normalizeSwaggerUrl } from './utils/normalizeSwaggerUrl'
import { getServers } from './utils/getServers'
import { joinPaths } from './utils/url'

const parser = new SwaggerParser()

/**
* Generates request handlers from the given OpenAPI V2/V3 document.
*/
export async function fromOpenApi(
document: string | OpenAPIV3.Document | OpenAPIV2.Document,
): Promise<RequestHandler[]> {
const specification = await parser.dereference(document)
const specification = await SwaggerParser.dereference(document)

const handlers = Object.entries(specification.paths).reduce<RequestHandler[]>(
(
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
"strict": true,
"allowJs": false,
"moduleResolution": "node",
"esModuleInterop": true,
"downlevelIteration": true,
"allowSyntheticDefaultImports": false,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"noImplicitAny": true,
Expand Down
Loading

0 comments on commit 06329f7

Please sign in to comment.