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

fix(types): emit and publish type definitions to npm #243

Merged
merged 2 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
tests/screenshots
tests/production
tests/data.txt
.env
.env
.idea
4 changes: 4 additions & 0 deletions empty-module.cjs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// shim for node modules in React Native
// see https://github.com/facebook/react-native/issues/5079
type empty = {};
export default empty;
Copy link
Contributor

Choose a reason for hiding this comment

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

do these not get generated by the tsc, instead, what if we gitignore it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They don't because index-node.cjs.js, index-browser.cjs.js and empty-module.cjs.js live outside of the ./lib folder and are plain JavaScript files.

Copy link
Contributor

Choose a reason for hiding this comment

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

not sure what you mean

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The package.json points to these entry files, and TypeScript will look for the equivalent but with the .d.ts extension.

Copy link
Contributor Author

@etienne-martin etienne-martin Jan 18, 2021

Choose a reason for hiding this comment

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

Ideally I'd get rid of those files sitting at the root of the project and point the package.json to the entry points in the ./dist folder but I didn't want to change everything.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean that tsc generates these files at publish time, why do they need to be committed on git?

Copy link
Contributor

Choose a reason for hiding this comment

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

and are plain JavaScript files.

I see now, makes sense! in the future they should be moved to ts

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. I will work on it next time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another thing is, I'd use the ts-expect package against the build output to test that the types are emitted properly. That would ensure that the chain package.json --> enty-*.js --> entry-*.d.ts is working properly.

Because right now, there are unit test for the source code, but there's nothing that checks that the build output is in good health.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wasn't aware of that. Thanks for the information. I will check it out :)

1 change: 1 addition & 0 deletions index-browser.cjs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/entry-browser-cjs";
1 change: 1 addition & 0 deletions index-node.cjs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/entry-node-cjs";
1 change: 1 addition & 0 deletions lib/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "*/package.json";
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "1.7.0",
"jsdelivr": "dist/search-insights.min.js",
"main": "index-node.cjs.js",
"types": "index-node.cjs.d.ts",
"browser": "index-browser.cjs.js",
"react-native": {
"http": "./empty-module.cjs.js",
Expand All @@ -16,11 +17,13 @@
"dist",
"lib",
"!**/__tests__/**",
"index.cjs.js",
"empty-module.cjs.js"
"empty-module.cjs.js",
"empty-module.cjs.d.ts",
"index-browser.cjs.d.ts",
"index-node.cjs.d.ts"
],
"scripts": {
"build": "rollup --environment NODE_ENV:'production' -c rollup.config.js",
"build": "rollup --environment NODE_ENV:'production' -c rollup.config.js && tsc --build tsconfig.declaration.json",
"build:dev": "rollup --watch --environment NODE_ENV:'development' -c rollup.config.js",
"build:examples": "webpack --config config/webpack.config.js --color --progress",
"dev": "NODE_ENV=development webpack --config config/webpack.config.js --color --progress --watch & node server/server",
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.declaration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"noEmit": false,
"emitDeclarationOnly": true,
"declaration": true,
"declarationDir": "./dist"
},
}
12 changes: 9 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"resolveJsonModule": false,
"moduleResolution": "node",
"noEmit": true,
"noUnusedLocals": true,
"outDir": "./dist/",
"outDir": "./dist",
"rootDir": "./lib",
"module": "es6",
"target": "es6",
"strict": true,
Expand All @@ -13,5 +14,10 @@
"noImplicitAny": false,
"allowJs": true
},
"exclude": ["node_modules", "**/*.test.ts"]
"files": [
"./lib/typings.d.ts",
"./lib/entry-umd.ts",
"./lib/entry-node-cjs.ts",
"./lib/entry-browser-cjs.ts"
]
}