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

Update the packaging to split the bundles into esmodule and commonjs #55

Merged
merged 37 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ce140d0
esm working?
johnpmitsch May 16, 2023
690d076
esm config minified
johnpmitsch May 16, 2023
2b3c81b
wip
johnpmitsch May 16, 2023
093bb7c
wip
johnpmitsch May 16, 2023
8655ea2
wip
johnpmitsch May 16, 2023
5eac38f
wip
johnpmitsch May 17, 2023
6240ed4
esm build working?
johnpmitsch May 17, 2023
8582416
wip
johnpmitsch May 17, 2023
a0c6d89
replace apollo/client/core
johnpmitsch May 18, 2023
0948633
Revert "replace apollo/client/core"
johnpmitsch May 18, 2023
f2cd08d
wip
johnpmitsch May 18, 2023
29f9a8b
working
johnpmitsch May 18, 2023
1d9f48c
working without apollo client
johnpmitsch May 18, 2023
c3787d8
working with no apollo client
johnpmitsch May 18, 2023
fa552d3
errors working
johnpmitsch May 18, 2023
1aa8081
update .gitignore
johnpmitsch May 18, 2023
25ed3c3
update .gitignore
johnpmitsch May 18, 2023
344670c
Refactor tsconfig and add one for cjs
johnpmitsch May 18, 2023
26e4756
cjs working?
johnpmitsch May 18, 2023
bced3d6
wip
johnpmitsch May 19, 2023
6921be4
Update from @apollo/client to urql
johnpmitsch May 20, 2023
bab4eab
packaging tweaks
johnpmitsch May 22, 2023
d2bef72
Remove old rollup plugins
johnpmitsch May 22, 2023
8b81f99
Remove dts-bundle-generator
johnpmitsch May 22, 2023
11f5318
Add externals plugin to cjs too
johnpmitsch May 22, 2023
5e5a30a
Revert "Update from @apollo/client to urql"
johnpmitsch May 23, 2023
d641fd9
Merge remote-tracking branch 'origin/main' into v1packaging_urql
johnpmitsch May 24, 2023
1838118
tests working
johnpmitsch May 24, 2023
f3b8215
Update codegen
johnpmitsch May 24, 2023
67e61dd
Don't minify generated types
johnpmitsch May 24, 2023
d44851a
packaging updates
johnpmitsch May 24, 2023
dd584ed
Remove unnecessary packages
johnpmitsch May 24, 2023
162873c
Update tsconfig references
johnpmitsch May 24, 2023
8709eb7
Update tsconfig spec
johnpmitsch May 24, 2023
7ba8214
Update formatting
johnpmitsch May 24, 2023
0647405
Remove type from package.json
johnpmitsch May 24, 2023
d103a04
Added jest timeout back
johnpmitsch May 24, 2023
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ Thumbs.db

# secrets
graphqlHeaders.json

# bundling
.rollup.cache/

5 changes: 3 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

# Ignore codegen generated file
# See more: https://www.the-guild.dev/graphql/codegen/docs/integrations/prettier
/packages/libs/api/sdk/src/graphql/types.ts
/packages/libs/api/sdk/src/graphql/fragmentMatcher.ts
packages/libs/sdk/src/api/graphql/generatedTypes.ts
packages/libs/sdk/src/api/graphql/fragmentMatcher.ts
packages/libs/sdk/src/api/graphql/schema.json
3 changes: 2 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
"!{projectRoot}/tsconfig.spec.json",
"!{projectRoot}/jest.config.[jt]s"
]
}
},
"neverConnectToCloud": true
}
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@
"@nrwl/rollup": "16.1.0",
"@nrwl/webpack": "16.1.0",
"@nx/eslint-plugin": "~16.1.0",
"@nx/express": "~16.1.0",
"@nx/jest": "~16.1.0",
"@nx/js": "~16.1.0",
"@nx/linter": "~16.1.0",
"@nx/node": "~16.1.0",
"@nx/react": "~16.1.0",
"@nx/web": "~16.1.0",
"@nx/workspace": "~16.1.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
"@pollyjs/adapter-node-http": "^6.0.5",
"@pollyjs/core": "^6.0.5",
"@pollyjs/persister-fs": "^6.0.5",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^14.1.0",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-typescript": "^8.5.0",
"@svgr/webpack": "^6.1.2",
"@testing-library/react": "13.4.0",
Expand Down Expand Up @@ -95,6 +98,8 @@
"react-test-renderer": "18.2.0",
"rollup": "^2.79.1",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-dts": "^5.3.0",
"rollup-plugin-node-externals": "^6.0.1",
"style-loader": "^3.3.0",
"stylus": "^0.55.0",
"stylus-loader": "^7.1.0",
Expand All @@ -105,10 +110,6 @@
"typescript": "5.0.4",
"url-loader": "^4.1.1",
"webpack": "^5.76.0",
"webpack-merge": "^5.8.0",
"@nx/react": "~16.1.0",
"@nx/jest": "~16.1.0",
"@nx/express": "~16.1.0",
"@nx/node": "~16.1.0"
"webpack-merge": "^5.8.0"
}
}
1 change: 1 addition & 0 deletions packages/libs/sdk/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
QUICKNODE_GRAPH_API_KEY=replaceme
2 changes: 1 addition & 1 deletion packages/libs/sdk/.test.env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QUICKNODE_GQL_API_KEY=replaceme
QUICKNODE_GRAPH_API_KEY=replaceme
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just updated the name here and in the docs and code for consistency

8 changes: 4 additions & 4 deletions packages/libs/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ Please submit any questions, issues, or feedback as an [issue in Github](https:/

We recommend using the example application to develop

1. cd `packages/apps/examples/sdk-api`
1. cd `packages/apps/examples/sdk-api` from `qn-oss` monorepo root
2. `cp .env.example .env` and add api key
3. `nx serve apps-examples-sdk-api`
4. Then you can send requests to the API, for example: `curl http://localhost:3333/api/nftsByAddress/0xbc08dadccc79c00587d7e6a75bb68ff5fd30f9e0`
Expand All @@ -383,6 +383,6 @@ Run `nx lint libs-sdk` to execute the lint via [ESLint](https://eslint.org/).

Generate graphql typings via [Codegen](https://www.the-guild.dev/graphql/codegen).

1. cd `packages/libs/sdk`
2. add a `graphqlHeaders.json` with any authorization headers you want to pass to graphql API
3. run `yarn run codegen`
1. navigate to `packages/libs/sdk` from `qn-oss` monorepo root
1. `cp .env.example .env` and add api key
1. run `yarn run codegen`
41 changes: 0 additions & 41 deletions packages/libs/sdk/codegen.ts

This file was deleted.

26 changes: 26 additions & 0 deletions packages/libs/sdk/codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
overwrite: true
documents:
- src/**/*.gql
- src/**/!(*.d).{ts,tsx}
schema:
- https://api.quicknode.com/graphql:
headers:
content-type: application/json
x-api-key: '${QUICKNODE_GRAPH_API_KEY}'
generates:
src/api/graphql/generatedTypes.ts:
plugins:
- typescript
- typescript-operations
- typed-document-node
config:
namingConvention: ./codegenCustomNaming.cjs
exportFragmentSpreadSubTypes: true
inlineFragmentTypes: combine
fragmentMasking: false
src/api/graphql/fragmentMatcher.ts:
plugins:
- fragment-matcher
src/api/graphql/schema.json:
plugins:
- introspection
2 changes: 2 additions & 0 deletions packages/libs/sdk/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export default {
},
moduleFileExtensions: ['ts', 'js', 'html'],
setupFiles: ['./spec/testSetup/jestSetup.ts'],
setupFilesAfterEnv: ['./spec/testSetup/jestSetupAfterEnv.ts'],
testEnvironment: 'node',
slowTestThreshold: 15,
};
19 changes: 13 additions & 6 deletions packages/libs/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
},
"license": "MIT",
"version": "1.0.0-alpha.0",
"main": "./src/index.js",
"module": "./src/index.esm.js",
"types": "./src/index.d.ts",
"main": "./cjs/index.js",
"module": "./esm/index.js",
"types": "./index.d.ts",
"dependencies": {
"@urql/core": "^4.0.7",
"@urql/exchange-graphcache": "^6.0.4",
"cross-fetch": "^3.1.6",
"graphql": "^16.5.0"
"cross-fetch": "^3.1.6"
},
"devDependencies": {
"@graphql-codegen/cli": "2.13.8",
Expand All @@ -30,10 +29,18 @@
"@types/mocha": "^10.0.1",
"@types/node": "^18.13.0",
"@types/supertest": "^2.0.12",
"dotenv": "^16.0.3",
"eslint-plugin-no-only-tests": "^3.1.0",
"graphql": "^16.6.0",
"supertest": "^6.2.4"
},
"scripts": {
"codegen": "graphql-codegen --config codegen.ts"
"codegen": "graphql-codegen --require dotenv/config"
},
"exports": {
".": {
"import": "./esm/index.js",
"require": "./cjs/index.js"
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the part that dynamically will import the correct bundle based on whether the package is imported with require or import

}
}
6 changes: 3 additions & 3 deletions packages/libs/sdk/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"options": {
"commands": [
"rm -rf ./dist/packages/libs/sdk",
"./node_modules/rollup/dist/bin/rollup --config packages/libs/sdk/rollup.config.js",
"yarn tsc --project packages/libs/sdk/tsconfig.lib.json --declarationDir dist --emitDeclarationOnly"
"./node_modules/rollup/dist/bin/rollup --config packages/libs/sdk/rollup.mjs",
"node ./tools/scripts/sdk_package_json.mjs"
],
"outputPath": "dist/packages/libs/sdk"
"parallel": false
}
},
"publish": {
Expand Down
40 changes: 0 additions & 40 deletions packages/libs/sdk/rollup.config.js

This file was deleted.

65 changes: 65 additions & 0 deletions packages/libs/sdk/rollup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import typescript from '@rollup/plugin-typescript';
import copy from 'rollup-plugin-copy';
import externals from 'rollup-plugin-node-externals';
import dts from 'rollup-plugin-dts';
import json from '@rollup/plugin-json';
import path from 'path';
import { URL } from 'url';

// esm patch for __dirname
const __dirname = new URL('.', import.meta.url).pathname;
const rootDir = path.resolve(__dirname);
const toAbsoluteDir = (relativeDir) => path.resolve(rootDir, relativeDir);

const bundle = (config) => ({
input: toAbsoluteDir('./src/index.ts'),
...config,
});

// The shared rollup plugins for esm and cjs bundles
const sharedPlugins = [
externals(), // Exclude node_modules from bundle
json({ compact: true }),
typescript({
tsconfig: toAbsoluteDir('tsconfig.lib.json'),
}),
];

export default [
bundle({
output: {
file: 'dist/packages/libs/sdk/esm/index.js',
format: 'esm',
},
plugins: [...sharedPlugins],
}),
bundle({
output: {
file: 'dist/packages/libs/sdk/cjs/index.js',
format: 'cjs',
exports: 'named',
},
plugins: [
...sharedPlugins,
copy({
targets: [
{
src: [toAbsoluteDir('README.md'), toAbsoluteDir('package.json')],
dest: 'dist/packages/libs/sdk/',
},
],
}),
],
}),
bundle({
plugins: [
dts({
input: toAbsoluteDir('./src/index.ts'),
}),
], // Rollup the .d.ts files
output: {
file: 'dist/packages/libs/sdk/index.d.ts',
format: 'es',
},
}),
];
2 changes: 1 addition & 1 deletion packages/libs/sdk/spec/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { API } from '../../src';

const opts: Record<string, any> = {
graphApiKey: process.env['QUICKNODE_GQL_API_KEY'] || '',
graphApiKey: process.env['QUICKNODE_GRAPH_API_KEY'] || '',
};

export const apiClient = new API(opts);
5 changes: 5 additions & 0 deletions packages/libs/sdk/spec/testSetup/jestSetupAfterEnv.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not ideal but I had to add this back, it was removed in #56 because I thought we no longer needed it.

I'm seeing this error, if I re-record the failing tests it will show up on other previously passing tests. This was similar to the apollo errors I saw that required this, IIRC it basically came down to race conditions because of the speed at the tests are executed. Can look into it more when I get a chance

image

Copy link
Contributor Author

@johnpmitsch johnpmitsch May 25, 2023

Choose a reason for hiding this comment

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

Getting the actual error, it's the same as the one I ran into with Apollo, I have to dig into it a bit more of the why here, it could have something to do with the http request mocking

      CombinedError: [Network] req.setHeader is not a function
          at operation (/Users/johnmitsch/qn-oss/packages/libs/sdk/node_modules/@urql/core/src/utils/result.ts:168:1)
          at networkMode (/Users/johnmitsch/qn-oss/packages/libs/sdk/node_modules/@urql/core/src/internal/fetchSource.ts:151:1)
          at processTicksAndRejections (node:internal/process/task_queues:96:5)
          at /Users/johnmitsch/qn-oss/packages/libs/sdk/node_modules/wonka/dist/wonka.js:342:20

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @ts-ignore
afterEach(async () => {
// pause after tests to avoid race conditions
await new Promise((resolve) => setTimeout(resolve, 100));
});
2 changes: 1 addition & 1 deletion packages/libs/sdk/src/api/controllers/nfts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class NftsController {
private async getByWalletAddress(
variables: WalletNFTsByAddressQueryVariablesType & NonQueryInput
): Promise<WalletNFTsByAddressFormattedResult> {
const { chain, contractAddresses, ...queryVariables } = variables;
const { chain, ...queryVariables } = variables;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a small change that I missed from 42a6627, I noticed in the intellisense options when testing out the built package

const userChain = chain || this.defaultChain;
const query: Record<ChainName, TypedDocumentNode<any, any>> = {
ethereum: CodegenEthMainnetWalletNFTsByAddressDocument,
Expand Down
Loading