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

Typescript + Tests + Namespaced npm package #7

Closed
wants to merge 6 commits into from
Closed

Conversation

kumavis
Copy link
Member

@kumavis kumavis commented Sep 21, 2020

based on the metamask repo template, but halp test not passing

@kumavis
Copy link
Member Author

kumavis commented Sep 21, 2020

rollup has so much trouble with readable-stream 🚒

@kumavis
Copy link
Member Author

kumavis commented Sep 21, 2020

wait why is the main export a bundle

@kumavis
Copy link
Member Author

kumavis commented Sep 21, 2020

dist tree getting a bit hairy

dist
├── BasePostMessageStream.d.ts
├── bundle.cjs.js
├── bundle.es.js
├── cjs
│   ├── BasePostMessageStream.d.ts
│   ├── BasePostMessageStream.js
│   ├── BasePostMessageStream.js.map
│   ├── enums.d.ts
│   ├── enums.js
│   ├── enums.js.map
│   ├── index.d.ts
│   ├── index.js
│   ├── index.js.map
│   ├── index.test.d.ts
│   ├── index.test.js
│   ├── index.test.js.map
│   ├── WindowPostMessageStream.d.ts
│   ├── WindowPostMessageStream.js
│   ├── WindowPostMessageStream.js.map
│   ├── WorkerParentPostMessageStream.d.ts
│   ├── WorkerParentPostMessageStream.js
│   ├── WorkerParentPostMessageStream.js.map
│   ├── WorkerPostMessageStream.d.ts
│   ├── WorkerPostMessageStream.js
│   └── WorkerPostMessageStream.js.map
├── enums.d.ts
├── esm
│   ├── BasePostMessageStream.d.ts
│   ├── BasePostMessageStream.js
│   ├── BasePostMessageStream.js.map
│   ├── enums.d.ts
│   ├── enums.js
│   ├── enums.js.map
│   ├── index.d.ts
│   ├── index.js
│   ├── index.js.map
│   ├── index.test.d.ts
│   ├── index.test.js
│   ├── index.test.js.map
│   ├── WindowPostMessageStream.d.ts
│   ├── WindowPostMessageStream.js
│   ├── WindowPostMessageStream.js.map
│   ├── WorkerParentPostMessageStream.d.ts
│   ├── WorkerParentPostMessageStream.js
│   ├── WorkerParentPostMessageStream.js.map
│   ├── WorkerPostMessageStream.d.ts
│   ├── WorkerPostMessageStream.js
│   └── WorkerPostMessageStream.js.map
├── index.d.ts
├── index.test.d.ts
├── WindowPostMessageStream.d.ts
├── WorkerParentPostMessageStream.d.ts
└── WorkerPostMessageStream.d.ts

@kumavis
Copy link
Member Author

kumavis commented Sep 21, 2020

hell is empty and all the devils are here 👍

@kumavis
Copy link
Member Author

kumavis commented Sep 22, 2020

maybe hoping this gets fixed here nodejs/readable-stream#348

@danfinlay
Copy link

When I npm test I just get No tests found, exiting with code 1.

@kumavis
Copy link
Member Author

kumavis commented Sep 23, 2020

  • drop bundle (for now)
  • expose node env in workers nodeIntegrationInWorker: true

@kumavis kumavis marked this pull request as ready for review September 23, 2020 06:53
@kumavis
Copy link
Member Author

kumavis commented Sep 23, 2020

why no ci

@kumavis
Copy link
Member Author

kumavis commented Sep 23, 2020

CI avalanche lol

@kumavis
Copy link
Member Author

kumavis commented Sep 23, 2020

cool, just needs coverage tweaks

@kumavis kumavis changed the title wip typescript and tests Typescript + Tests + Namespaced npm package Sep 23, 2020
@whymarrh
Copy link
Contributor

I've created MetaMask/metamask-module-template#19 as an updated baseline for the CI workflow we're using here—once that's merged we can update this workflow to have a similar shape.

I've also created #8, #9, #10, and #11, factoring out some of the extra changes from this PR. We can rebase this and drop those files to keep this changeset focused.

Copy link
Contributor

@whymarrh whymarrh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few initial comments—I'll check it out locally as well.

jest.config.js Outdated
const merge = require('merge');
const tsPreset = require('ts-jest/jest-preset');

module.exports = merge.recursive(tsPreset, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manually merging our config with the preset, is that necessary? Looking at the configuration docs for ts-jest, the preset key can be used.[1] Merging this manually also complicates this process as we both now have an extra dependency and a resolution process (i.e. how does merge.recursive work? how many levels does it do? how does it treat the arrays?) to manage.

If we can get away with using preset: 'ts-jest', let use that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this came from an earlier point where there were two presets, so i had to merge the presets manually. not needed any more 👍

package.json Outdated
Comment on lines 14 to 16
"repository": {
"type": "git",
"url": "git+https://github.com/kumavis/post-message-stream.git"
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file will need to be updated to keep some of the original metadata, that's still useful. I've updated it in #11.

},
"bugs": {
"url": "https://github.com/kumavis/post-message-stream/issues"
"@types/readable-stream": "^2.3.9",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this to the devDependencies and export Node.js types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readable-stream is better because its semantically versioned, node ships breaking changes with its builtins
(if i understood)

tsconfig.json Outdated
Comment on lines 13 to 14
"allowJs": true,
"allowSyntheticDefaultImports": true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is allowJs enabled? I noticed below we're also listing "./src/**/*.js" as included.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figure this out locally, we're not currently reading these files. See #7 (comment) for context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the js has been removed, this is no longer needed 👍

Comment on lines +23 to +25
"build:compile": "yarn build:compile:esm && yarn build:compile:cjs",
"build:compile:esm": "tsc --module ES6 --outDir ./dist/esm",
"build:compile:cjs": "tsc --module UMD --outDir ./dist/cjs",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this locally, I'm not sure I follow what having two builds is for? Could we ship only the CommonJS version?

I'm also confused by the naming here, though maybe it's a typo: build:compile:cjs is outputting UMD to the /cjs? Should that be outputting CJS instead?

We could move these into tsconfig(.*).json files, e.g.:

@@ -21,8 +21,8 @@
     "build": "yarn run build:clean && yarn run build:compile && yarn run build:bundle",
     "build:clean": "rm -rf ./dist && mkdir -p ./dist/esm && mkdir -p ./dist/cjs",
     "build:compile": "yarn build:compile:esm && yarn build:compile:cjs",
-    "build:compile:esm": "tsc --module ES6 --outDir ./dist/esm",
-    "build:compile:cjs": "tsc --module UMD --outDir ./dist/cjs",
+    "build:compile:esm": "tsc --project tsconfig.es2015.json",
+    "build:compile:cjs": "tsc --project tsconfig.json",
     "build:bundle": "browserify --standalone PostMessageStream ./dist/cjs/index.js > ./dist/bundle.js"
   },
   "license": "MIT",

With two config files: tsconfig.json and tsconfig.es2015.json:

@@ -4,17 +4,15 @@
     "declaration": true,
     "sourceMap": true,
     "strict": true,
-    "target": "es5",
+    "target": "ES5",
+    "module": "CommonJS",
     "lib": [
       "DOM",
       "ES2016"
     ],
-    "outDir": "./dist",
-    "allowJs": true,
-    "allowSyntheticDefaultImports": true
+    "outDir": "./dist/cjs"
   },
   "include": [
-    "./src/**/*.ts",
-    "./src/**/*.js"
+    "./src/**/*.ts"
   ]
-}
\ No newline at end of file
+}
@@ -0,0 +1,7 @@
+{
+  "extends": "./tsconfig.json",
+  "compilerOptions": {
+    "target": "ES2015",
+    "module": "ES2015"
+  }
+}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • i think we should build for both (cjs+esm), the ecosystem (node v14, agoric, etc) is trying to get everyone on to esm
  • this setup is intended to be a hybrid package https://2ality.com/2019/10/hybrid-npm-packages.html
  • umd includes cjs with little overhead, so figured why not. can go either way
  • i like moving the config into project files 👍

package.json Outdated Show resolved Hide resolved
package.json Outdated
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "yarn build && yarn testOnly",
"testOnly": "jest --runInBand",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the --runInBand flag here?

Suggested change
"testOnly": "jest --runInBand",
"testOnly": "jest",

The tests seem to pass without it, and the docs suggest it is a niche use case:

  --runInBand, -i               Run all tests serially in the current process
                                (rather than creating a worker pool of child
                                processes that run tests). This is sometimes
                                useful for debugging, but such use cases are
                                pretty rare.                           [boolean]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reduces threads to 1 and is recommended for CI (also allows debugging with ndb)

package.json Outdated
"jest": "^26.4.2",
"merge": "^1.2.1",
"ts-jest": "^26.3.0",
"ts-node": "^9.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to remove this from the list, I don't think we're using it anywhere

@kumavis kumavis requested a review from a team as a code owner September 24, 2020 00:04
@kumavis
Copy link
Member Author

kumavis commented Sep 24, 2020

@kumavis
Copy link
Member Author

kumavis commented Sep 24, 2020

  • cleanup ts config
  • cleanup jest config
  • breakout ts projects
  • verify hybrid npm package
  • clarify exporting node types + types deps (dev or prod?)

@kumavis kumavis reopened this Sep 24, 2020
@kumavis
Copy link
Member Author

kumavis commented Sep 25, 2020

dan asked about compiling in parallel https://github.com/kimmobrunfeldt/concurrently

@rekmarks rekmarks marked this pull request as draft April 28, 2021 21:48
@rekmarks
Copy link
Member

I converted this to draft as I'm going to split up the work over a few different PRs. I will likely use the tests written here.

@rekmarks
Copy link
Member

Actually, I'll just close it.

@rekmarks rekmarks closed this Apr 28, 2021
@rekmarks rekmarks mentioned this pull request May 2, 2021
@rekmarks rekmarks deleted the from-template branch May 2, 2021 04:25
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.

4 participants