Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.

Fix tsc and Jest setup for paths mapping (#539)

* Update package naming conventions

* Readd individual tsconfig.json configs to packages

* Fix moduleNameMapper for new paths convention

* Fix scripts/rollup/build to glob instead tsc paths

Changed files
+91 -28
exchanges
graphcache
packages
core
preact-urql
react-urql
svelte-urql
scripts
+15 -6
CONTRIBUTING.md
···
## How do I add a new package?
+
First of all we need to know where to put the package.
+
+
- Exchanges should be added to `exchanges/` and the folder should be the plain
+
name of the exchange. Since the `package.json:name` is following the convention
+
of `@urql/exchange-*` the folder should just be without this conventional prefix.
+
- All other packages should be added to `packages/`. Typically all packages should
+
be named `@urql/*` and their folders should be named exactly this without the
+
prefix or `*-urql`. Optionally if the package will be named `*-urql` then the folder
+
can take on the same name.
+
When adding a new package, start by copying a `package.json` file from another project.
You may want to alter the following fields first:
···
If your entrypoint won't be at `src/index.ts` you may alter it. But the `types` field has to match
the same file relative to the `dist/types` folder, where `rollup` will output the TypeScript
-
declaration files. When setting up your package make sure to create a `src/index.ts` file (or a file
-
at what you've set `source` to)
+
declaration files.
+
+
When setting up your package make sure to create a `src/index.ts` file
+
(or any other file which you've pointed `package.json:source` to). Also don't forget to
+
copy over the `tsconfig.json` from another package (You won't need to change it).
The `scripts.prepare` task is set up to check your new `package.json` file for correctness. So in
case you get anything wrong, you'll get a short error when running `yarn` after setting your new
project up. Just in case! 😄
-
-
Lastly, your new package will need to be added to the `tsconfig.json` in the root of the repository.
-
Add a new entry to `compilerOptions.paths` where the key is the `name` you've used in your
-
`package.json` and the value is an array with a single entry, the path to your package + `src/`.
Afterwards you can check whether everything is working correctly by running:
+12
exchanges/graphcache/tsconfig.json
···
+
{
+
"extends": "../../tsconfig.json",
+
"include": ["src"],
+
"compilerOptions": {
+
"baseUrl": "./",
+
"paths": {
+
"urql": ["../../node_modules/urql/src"],
+
"*-urql": ["../../node_modules/*-urql/src"],
+
"@urql/*": ["../../node_modules/@urql/*/src"]
+
}
+
}
+
}
+1
package.json
···
"eslint-plugin-react": "^7.18.0",
"eslint-plugin-react-hooks": "^2.3.0",
"execa": "^4.0.0",
+
"glob": "^7.1.6",
"graphql": "^14.6.0",
"graphql-tag": "^2.10.1",
"husky": "^4.2.1",
+12
packages/core/tsconfig.json
···
+
{
+
"extends": "../../tsconfig.json",
+
"include": ["src"],
+
"compilerOptions": {
+
"baseUrl": "./",
+
"paths": {
+
"urql": ["../../node_modules/urql/src"],
+
"*-urql": ["../../node_modules/*-urql/src"],
+
"@urql/*": ["../../node_modules/@urql/*/src"]
+
}
+
}
+
}
+12
packages/preact-urql/tsconfig.json
···
+
{
+
"extends": "../../tsconfig.json",
+
"include": ["src"],
+
"compilerOptions": {
+
"baseUrl": "./",
+
"paths": {
+
"urql": ["../../node_modules/urql/src"],
+
"*-urql": ["../../node_modules/*-urql/src"],
+
"@urql/*": ["../../node_modules/@urql/*/src"]
+
}
+
}
+
}
+12
packages/react-urql/tsconfig.json
···
+
{
+
"extends": "../../tsconfig.json",
+
"include": ["src"],
+
"compilerOptions": {
+
"baseUrl": "./",
+
"paths": {
+
"urql": ["../../node_modules/urql/src"],
+
"*-urql": ["../../node_modules/*-urql/src"],
+
"@urql/*": ["../../node_modules/@urql/*/src"]
+
}
+
}
+
}
+12
packages/svelte-urql/tsconfig.json
···
+
{
+
"extends": "../../tsconfig.json",
+
"include": ["src"],
+
"compilerOptions": {
+
"baseUrl": "./",
+
"paths": {
+
"urql": ["../../node_modules/urql/src"],
+
"*-urql": ["../../node_modules/*-urql/src"],
+
"@urql/*": ["../../node_modules/@urql/*/src"]
+
}
+
}
+
}
+5 -7
scripts/jest/preset.js
···
-
const { pathsToModuleNameMapper } = require('ts-jest/utils');
-
const { compilerOptions } = require('../../tsconfig.json');
-
module.exports = {
preset: 'ts-jest',
setupFiles: [
···
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
-
moduleNameMapper: pathsToModuleNameMapper(
-
compilerOptions.paths,
-
{ prefix: '<rootDir>/../../' }
-
),
+
moduleNameMapper: {
+
"^urql$": "<rootDir>/../../node_modules/urql/src",
+
"^(.*-urql)$": "<rootDir>/../../node_modules/$1/src",
+
"^@urql/(.*)$": "<rootDir>/../../node_modules/@urql/$1/src",
+
},
watchPlugins: ['jest-watch-yarn-workspaces'],
testRegex: '(src/.*(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json'],
+3 -3
scripts/rollup/build.js
···
#!/usr/bin/env node
const path = require('path');
+
const glob = require('glob').sync;
const execa = require('execa');
-
const { compilerOptions: { paths } } = require('../../tsconfig.json');
const workspaceRoot = path.resolve(__dirname, '../../');
const rollupConfig = path.resolve(__dirname, './config.js');
-
let packages = Object.keys(paths).map(package => {
-
return path.resolve(workspaceRoot, paths[package][0], '../');
+
let packages = glob('{packages,exchanges}/*/package.json').map(pkg => {
+
return path.resolve(pkg, '../');
});
// CircleCI parallelism
+1 -3
scripts/rollup/plugins.js
···
: typescript({
useTsconfigDeclarationDir: true,
objectHashIgnoreUnknownHack: true,
-
tsconfigDefaults: require('../../tsconfig.json'),
tsconfigOverride: {
exclude: [
'src/**/*.test.ts',
···
],
compilerOptions: {
sourceMap: true,
-
baseUrl: '.',
declaration: !isProduction,
-
declarationDir: './dist/types',
+
declarationDir: settings.types,
target: 'es6',
},
},
+2 -1
scripts/rollup/settings.js
···
const path = require('path');
-
const cwd = process.cwd();
+
export const cwd = process.cwd();
export const pkg = require(path.resolve(cwd, './package.json'));
+
export const types = path.resolve(cwd, 'dist/types/');
const normalize = name => name
.replace(/[@\s\/\.]+/g, ' ')
+4 -8
tsconfig.json
···
"compilerOptions": {
"baseUrl": "./",
"paths": {
-
"@urql/core": ["packages/core/src"],
-
"@urql/exchange-graphcache": ["exchanges/graphcache/src"],
"urql": ["packages/react-urql/src"],
-
"preact-urql": ["packages/preact-urql/src"],
-
"svelte-urql": ["packages/svelte-urql/src"]
+
"*-urql": ["packages/*-urql/src"],
+
"@urql/exchange-*": ["exchanges/*/src"],
+
"@urql/*": ["packages/*-urql/src", "packages/*/src"]
},
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
···
"noImplicitAny": false,
"noUnusedParameters": true
},
-
"include": [
-
"packages",
-
"exchanges"
-
],
+
"include": ["packages", "exchanges"],
"exclude": [
"scripts",
"packages/*/examples",