Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

fix: add explicit module declaration to type definitions #16219

Merged
merged 1 commit into from
Mar 30, 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
1 change: 1 addition & 0 deletions web3.js/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/commitlint.config.js
/coverage
/declarations
/deploy
/doc
/lib
Expand Down
3 changes: 3 additions & 0 deletions web3.js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ test-ledger/

# Flow
module.flow.js

# TypeScript
declarations
1 change: 1 addition & 0 deletions web3.js/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test/dist
module.flow.js
declarations
367 changes: 27 additions & 340 deletions web3.js/package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions web3.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"main": "lib/index.cjs.js",
"module": "lib/index.esm.js",
"types": "lib/types/index.d.ts",
"types": "lib/index.d.ts",
"browserslist": [
"defaults",
"not IE 11",
Expand All @@ -37,15 +37,16 @@
"/src"
],
"scripts": {
"build": "cross-env NODE_ENV=production rollup -c; npm run flowgen",
"build": "npm run clean; cross-env NODE_ENV=production rollup -c; npm run generate:dts; npm run generate:flow",
"build:browser-test": "rollup -c test/rollup.config.js",
"build:fixtures": "set -ex; ./test/fixtures/noop-program/build.sh",
"clean": "rimraf ./coverage ./lib",
"codecov": "set -ex; npm run test:cover; cat ./coverage/lcov.info | codecov",
"dev": "cross-env NODE_ENV=development rollup -c",
"doc": "set -ex; typedoc",
"doc:watch": "watch 'npm run doc' . --wait=1 --ignoreDirectoryPattern=/doc/",
"flowgen": "./scripts/flowgen.sh",
"generate:dts": "./scripts/typegen.sh",
"generate:flow": "flowgen lib/index.d.ts -o module.flow.js",
"lint": "set -ex; npm run pretty; eslint . --ext .js,.ts",
"lint:fix": "npm run pretty:fix && eslint . --fix",
"lint:watch": "watch 'npm run lint:fix' . --wait=1 --ignoreDirectoryPattern=/doc/",
Expand Down Expand Up @@ -126,10 +127,9 @@
"prettier": "^2.0.0",
"rimraf": "3.0.2",
"rollup": "2.43.1",
"rollup-plugin-flat-dts": "^1.0.3",
"rollup-plugin-dts": "^3.0.1",
"rollup-plugin-node-polyfills": "^0.2.1",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-ts": "^1.3.7",
"semantic-release": "^17.0.2",
"sinon": "^10.0.0",
"start-server-and-test": "^1.12.0",
Expand Down
15 changes: 0 additions & 15 deletions web3.js/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import typescript from 'rollup-plugin-ts';
import nodeResolve from '@rollup/plugin-node-resolve';
import nodePolyfills from 'rollup-plugin-node-polyfills';
import replace from '@rollup/plugin-replace';
Expand All @@ -13,7 +12,6 @@ const extensions = ['.js', '.ts'];
function generateConfig(configType, format) {
const browser = configType === 'browser';
const bundle = format === 'iife';
const generateTypescript = configType === 'typescript';

const config = {
input: 'src/index.ts',
Expand All @@ -25,12 +23,6 @@ function generateConfig(configType, format) {
extensions,
preferBuiltins: !browser,
}),
generateTypescript
? typescript({
browserslist: false,
tsconfig: './tsconfig.d.json',
})
: undefined,
babel({
exclude: '**/node_modules/**',
extensions,
Expand Down Expand Up @@ -145,12 +137,6 @@ function generateConfig(configType, format) {
},
];
break;
case 'typescript':
config.output = {
file: 'lib/types/index.d.ts',
};
config.plugins.push(json());
break;
default:
throw new Error(`Unknown configType: ${configType}`);
}
Expand All @@ -160,7 +146,6 @@ function generateConfig(configType, format) {

export default [
generateConfig('node'),
generateConfig('typescript'),
generateConfig('browser', 'esm'),
generateConfig('browser', 'iife'),
];
7 changes: 7 additions & 0 deletions web3.js/rollup.config.types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import dts from 'rollup-plugin-dts';

export default {
input: './declarations/index.d.ts',
output: [{file: 'lib/index.d.ts', format: 'es'}],
plugins: [dts()],
};
16 changes: 0 additions & 16 deletions web3.js/scripts/flowgen.sh

This file was deleted.

19 changes: 19 additions & 0 deletions web3.js/scripts/typegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set -e

# Generate typescript declarations
npx tsc -p tsconfig.d.json -d

# Flatten typescript declarations
npx rollup -c rollup.config.types.js

# Replace export with closing brace for module declaration
sed -i '$s/export {.*};/}/' lib/index.d.ts

# Replace declare's with export's
sed -i 's/declare/export/g' lib/index.d.ts

# Prepend declare module line
sed -i '2s;^;declare module "@solana/web3.js" {\n;' lib/index.d.ts

# Run prettier
npx prettier --write lib/index.d.ts
13 changes: 7 additions & 6 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,7 @@ describe('Connection', () => {
const newAccount = new Account().publicKey;

let testToken: Token;
let testTokenPubkey: PublicKey;
let testTokenAccount: PublicKey;
let testSignature: TransactionSignature;
let testOwner: Account;
Expand Down Expand Up @@ -1474,11 +1475,11 @@ describe('Connection', () => {
testOwner = accountOwner;
testToken = token;
testTokenAccount = tokenAccount as PublicKey;
testTokenPubkey = testToken.publicKey as PublicKey;
});

it('get token supply', async () => {
const supply = (await connection.getTokenSupply(testToken.publicKey))
.value;
const supply = (await connection.getTokenSupply(testTokenPubkey)).value;
expect(supply.uiAmount).to.eq(111.11);
expect(supply.decimals).to.eq(2);
expect(supply.amount).to.eq('11111');
Expand All @@ -1488,7 +1489,7 @@ describe('Connection', () => {

it('get token largest accounts', async () => {
const largestAccounts = (
await connection.getTokenLargestAccounts(testToken.publicKey)
await connection.getTokenLargestAccounts(testTokenPubkey)
).value;

expect(largestAccounts).to.have.length(2);
Expand Down Expand Up @@ -1575,7 +1576,7 @@ describe('Connection', () => {
it('get parsed token accounts by owner', async () => {
const tokenAccounts = (
await connection.getParsedTokenAccountsByOwner(testOwner.publicKey, {
mint: testToken.publicKey,
mint: testTokenPubkey,
})
).value;
tokenAccounts.forEach(({account}) => {
Expand All @@ -1593,7 +1594,7 @@ describe('Connection', () => {
it('get token accounts by owner', async () => {
const accountsWithMintFilter = (
await connection.getTokenAccountsByOwner(testOwner.publicKey, {
mint: testToken.publicKey,
mint: testTokenPubkey,
})
).value;
expect(accountsWithMintFilter).to.have.length(2);
Expand All @@ -1607,7 +1608,7 @@ describe('Connection', () => {

const noAccounts = (
await connection.getTokenAccountsByOwner(newAccount, {
mint: testToken.publicKey,
mint: testTokenPubkey,
})
).value;
expect(noAccounts).to.have.length(0);
Expand Down
3 changes: 2 additions & 1 deletion web3.js/tsconfig.d.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"stripInternal": true,
"emitDeclarationOnly": true,
"stripInternal": true
}
}
9 changes: 2 additions & 7 deletions web3.js/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"allowJs": true,
"skipLibCheck": true,
"declaration": true,
"declarationMap": true,
"declarationDir": "lib/types",
"declarationDir": "declarations",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
Expand All @@ -16,11 +15,7 @@
"isolatedModules": true,
"baseUrl": "src",
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"paths": {
// This is needed so that @solana/spl-token's @solana/web3.js doesn't conflict
"@solana/web3.js": ["."]
}
"noImplicitReturns": true
},
"include": ["src"]
}