Skip to content

Commit

Permalink
chore: add extentsion in imports (pmndrs#1823)
Browse files Browse the repository at this point in the history
* chore: add extentsion in imports

* fix lint

* typescript 5.0

* update ts 5

* revert typeVersions

* improve typing

* refactor workflow file
  • Loading branch information
dai-shi authored Apr 10, 2023
1 parent 12e256a commit 9297b8b
Show file tree
Hide file tree
Showing 40 changed files with 140 additions and 107 deletions.
13 changes: 5 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"prefer-const": "error",
"curly": ["warn", "multi-line", "consistent"],
"no-console": "off",
"import/extensions": ["error", "always"],
"import/no-unresolved": ["error", { "commonjs": true, "amd": true }],
"import/export": "error",
"@typescript-eslint/no-duplicate-imports": ["error"],
Expand Down Expand Up @@ -93,12 +94,8 @@
"@typescript-eslint/parser": [".js", ".jsx", ".ts", ".tsx"]
},
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx", ".json"],
"paths": ["src"]
},
"alias": {
"extensions": [".js", ".jsx", ".ts", ".tsx", ".json"],
"extensions": [".js", ".jsx", ".ts", ".tsx"],
"map": [
["^jotai$", "./src/index.ts"],
["jotai", "./src"]
Expand All @@ -108,9 +105,9 @@
},
"overrides": [
{
"files": ["src"],
"parserOptions": {
"project": "./tsconfig.json"
"files": ["tests/**/*.ts", "tests/**/*.tsx"],
"rules": {
"import/extensions": ["error", "never"]
}
},
{
Expand Down
13 changes: 9 additions & 4 deletions .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ jobs:
sed -i~ "1s/^/import.meta.env=import.meta.env||{};import.meta.env.MODE='${NODE_ENV}';/" tests/*/*.tsx tests/*/*/*.tsx
env:
NODE_ENV: ${{ matrix.env }}
- name: Patch for UMD/SystemJS
if: ${{ matrix.build == 'umd' || matrix.build == 'system' }}
- name: Patch for UMD
if: ${{ matrix.build == 'umd' }}
run: |
sed -i~ "s/<rootDir>\/src\(.*\)\.ts/<rootDir>\/dist\/${BUILD}\1.${NODE_ENV}.js/" package.json
sed -i~ "s/<rootDir>\/src\(.*\)\.ts/<rootDir>\/dist\/umd\1.${NODE_ENV}.js/" package.json
sed -i~ 's/"test:ci":.*,$/"test:ci": "jest",/' package.json
env:
BUILD: ${{ matrix.build }}
NODE_ENV: ${{ matrix.env }}
- name: Patch for SystemJS
if: ${{ matrix.build == 'system' }}
run: |
sed -i~ "s/<rootDir>\/src\(.*\)\.ts/<rootDir>\/dist\/system\1.${NODE_ENV}.js/" package.json
env:
NODE_ENV: ${{ matrix.env }}
- name: Test ${{ matrix.build }} ${{ matrix.env }}
run: |
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/test-old-typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: false
matrix:
typescript:
- 4.9.4
- 4.9.5
- 4.8.4
- 4.7.4
- 4.6.4
Expand All @@ -34,6 +34,9 @@ jobs:
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- run: yarn install --frozen-lockfile --check-files
- run: yarn build
- name: Patch for All TS
sed -i~ 's/"moduleResolution": "nodenext",/"moduleResolution": "node",/' tsconfig.json
sed -i~ 's/"allowImportingTsExtensions": true,//' tsconfig.json
- name: Patch for Old TS
if: ${{ matrix.typescript == '4.7.4' || matrix.typescript == '4.6.4' || matrix.typescript == '4.5.5' || matrix.typescript == '4.4.4' || matrix.typescript == '4.3.5' || matrix.typescript == '4.2.3' || matrix.typescript == '4.1.5' || matrix.typescript == '4.0.5' || startsWith(matrix.typescript, '3.') }}
run: |
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/simple-read.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env npx ts-node

import { add, complete, cycle, save, suite } from 'benny'
import { atom } from '../src/vanilla/atom'
import type { PrimitiveAtom } from '../src/vanilla/atom'
import { createStore } from '../src/vanilla/store'
import { atom } from '../src/vanilla/atom.ts'
import type { PrimitiveAtom } from '../src/vanilla/atom.ts'
import { createStore } from '../src/vanilla/store.ts'

const createStateWithAtoms = (n: number) => {
let targetAtom: PrimitiveAtom<number> | undefined
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/simple-write.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env npx ts-node

import { add, complete, cycle, save, suite } from 'benny'
import { atom } from '../src/vanilla/atom'
import type { PrimitiveAtom } from '../src/vanilla/atom'
import { createStore } from '../src/vanilla/store'
import { atom } from '../src/vanilla/atom.ts'
import type { PrimitiveAtom } from '../src/vanilla/atom.ts'
import { createStore } from '../src/vanilla/store.ts'

const createStateWithAtoms = (n: number) => {
let targetAtom: PrimitiveAtom<number> | undefined
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/subscribe-write.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env npx ts-node

import { add, complete, cycle, save, suite } from 'benny'
import { atom } from '../src/vanilla/atom'
import type { PrimitiveAtom } from '../src/vanilla/atom'
import { createStore } from '../src/vanilla/store'
import { atom } from '../src/vanilla/atom.ts'
import type { PrimitiveAtom } from '../src/vanilla/atom.ts'
import { createStore } from '../src/vanilla/store.ts'

const cleanupFns = new Set<() => void>()
const cleanup = () => {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"build:vanilla:utils": "rollup -c --config-vanilla_utils",
"build:react": "rollup -c --config-react",
"build:react:utils": "rollup -c --config-react_utils",
"postbuild": "yarn copy && yarn patch-ts3.8 && yarn patch-old-ts && yarn patch-esm-ts && yarn patch-readme",
"postbuild": "yarn patch-d-ts && yarn copy && yarn patch-ts3.8 && yarn patch-old-ts && yarn patch-esm-ts && yarn patch-readme",
"prettier": "prettier '*.{js,json,md}' '{src,tests,benchmarks,docs}/**/*.{ts,tsx,md,mdx}' --write",
"prettier:ci": "prettier '*.{js,json,md}' '{src,tests,benchmarks,docs}/**/*.{ts,tsx,md,mdx}' --list-different",
"eslint": "eslint --fix '*.{js,json}' '{src,tests,benchmarks}/**/*.{ts,tsx}'",
Expand All @@ -77,6 +77,7 @@
"test:ci": "yarn node --experimental-vm-modules $(yarn bin jest)",
"test:dev": "yarn node --experimental-vm-modules $(yarn bin jest) --watch --no-coverage",
"test:coverage:watch": "yarn node --experimental-vm-modules $(yarn bin jest) --watch",
"patch-d-ts": "node -e \"var {entries}=require('./rollup.config.js');require('shelljs').find('dist/**/*.d.ts').forEach(f=>{entries.forEach(({find,replacement})=>require('shelljs').sed('-i',new RegExp(' from \\''+find.source.slice(0,-1)+'\\';$'),' from \\''+replacement+'\\';',f));require('shelljs').sed('-i',/ from '(\\.[^']+)\\.ts';$/,' from \\'\\$1\\';',f)})\"",
"copy": "shx cp -r dist/src/* dist/esm && shx cp -r dist/src/* dist && shx rm -rf dist/src && shx rm -rf dist/{src,tests} && downlevel-dts dist dist/ts3.8 --to=3.8 && shx cp package.json readme.md LICENSE dist && json -I -f dist/package.json -e \"this.private=false; this.devDependencies=undefined; this.optionalDependencies=undefined; this.scripts=undefined; this.prettier=undefined; this.jest=undefined;\"",
"patch-ts3.8": "node -e \"require('shelljs').find('dist/ts3.8/**/*.d.ts').forEach(f=>require('fs').appendFileSync(f,'declare type Awaited<T> = T extends Promise<infer V> ? V : T;'))\"",
"patch-old-ts": "shx touch dist/ts_version_3.8_and_above_is_required.d.ts",
Expand Down Expand Up @@ -159,6 +160,7 @@
"@babel/template": "^7.20.7",
"@babel/types": "^7.21.4",
"@redux-devtools/extension": "^3.2.5",
"@rollup/plugin-alias": "^4.0.3",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-replace": "^5.0.2",
Expand Down
15 changes: 14 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
const path = require('path')
const alias = require('@rollup/plugin-alias')
const babelPlugin = require('@rollup/plugin-babel')
const resolve = require('@rollup/plugin-node-resolve')
const replace = require('@rollup/plugin-replace')
const terser = require('@rollup/plugin-terser')
const typescript = require('@rollup/plugin-typescript')
const { default: esbuild } = require('rollup-plugin-esbuild')
const createBabelConfig = require('./babel.config')
const createBabelConfig = require('./babel.config.js')

const extensions = ['.js', '.ts', '.tsx']
const { root } = path.parse(process.cwd())
const entries = [
{ find: /.*\/vanilla\/utils\.ts$/, replacement: 'jotai/vanilla/utils' },
{ find: /.*\/react\/utils\.ts$/, replacement: 'jotai/react/utils' },
{ find: /.*\/vanilla\.ts$/, replacement: 'jotai/vanilla' },
{ find: /.*\/react\.ts$/, replacement: 'jotai/react' },
]

function external(id) {
return !id.startsWith('.') && !id.startsWith(root)
Expand Down Expand Up @@ -54,6 +61,7 @@ function createESMConfig(input, output) {
output: { file: output, format: 'esm' },
external,
plugins: [
alias({ entries }),
resolve({ extensions }),
replace({
...(output.endsWith('.js')
Expand All @@ -78,6 +86,7 @@ function createCommonJSConfig(input, output) {
output: { file: `${output}.js`, format: 'cjs' },
external,
plugins: [
alias({ entries }),
resolve({ extensions }),
replace({
'import.meta.env?.MODE': 'process.env.NODE_ENV',
Expand Down Expand Up @@ -113,6 +122,7 @@ function createUMDConfig(input, output, env) {
},
external,
plugins: [
alias({ entries }),
resolve({ extensions }),
replace({
'import.meta.env?.MODE': JSON.stringify(env),
Expand All @@ -134,6 +144,7 @@ function createSystemConfig(input, output, env) {
},
external,
plugins: [
alias({ entries }),
resolve({ extensions }),
replace({
'import.meta.env?.MODE': JSON.stringify(env),
Expand Down Expand Up @@ -162,3 +173,5 @@ module.exports = function (args) {
createSystemConfig(`src/${c}.ts`, `dist/system/${c}`, 'production'),
]
}

module.exports.entries = entries
4 changes: 2 additions & 2 deletions src/babel/plugin-debug-label.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import babel, { PluginObj } from '@babel/core'
import _templateBuilder from '@babel/template'
import { isAtom } from './utils'
import type { PluginOptions } from './utils'
import { isAtom } from './utils.ts'
import type { PluginOptions } from './utils.ts'

const templateBuilder = (_templateBuilder as any).default || _templateBuilder

Expand Down
4 changes: 2 additions & 2 deletions src/babel/plugin-react-refresh.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import babel, { PluginObj } from '@babel/core'
import _templateBuilder from '@babel/template'
import { isAtom } from './utils'
import type { PluginOptions } from './utils'
import { isAtom } from './utils.ts'
import type { PluginOptions } from './utils.ts'

const templateBuilder = (_templateBuilder as any).default || _templateBuilder

Expand Down
6 changes: 3 additions & 3 deletions src/babel/preset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import babel from '@babel/core'
import pluginDebugLabel from './plugin-debug-label'
import pluginReactRefresh from './plugin-react-refresh'
import { PluginOptions } from './utils'
import pluginDebugLabel from './plugin-debug-label.ts'
import pluginReactRefresh from './plugin-react-refresh.ts'
import { PluginOptions } from './utils.ts'

export default function jotaiPreset(
_: typeof babel,
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'jotai/vanilla'
export * from 'jotai/react'
export * from './vanilla.ts'
export * from './react.ts'
8 changes: 4 additions & 4 deletions src/react.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { Provider, useStore } from './react/Provider'
export { useAtomValue } from './react/useAtomValue'
export { useSetAtom } from './react/useSetAtom'
export { useAtom } from './react/useAtom'
export { Provider, useStore } from './react/Provider.ts'
export { useAtomValue } from './react/useAtomValue.ts'
export { useSetAtom } from './react/useSetAtom.ts'
export { useAtom } from './react/useAtom.ts'
8 changes: 4 additions & 4 deletions src/react/Provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, createElement, useContext, useRef } from 'react'
import type { ReactNode } from 'react'
import { createStore, getDefaultStore } from 'jotai/vanilla'
import type { FunctionComponentElement, ReactNode } from 'react'
import { createStore, getDefaultStore } from '../vanilla.ts'

type Store = ReturnType<typeof createStore>

Expand All @@ -10,7 +10,7 @@ type Options = {
store?: Store
}

export const useStore = (options?: Options) => {
export const useStore = (options?: Options): Store => {
const store = useContext(StoreContext)
return options?.store || store || getDefaultStore()
}
Expand All @@ -21,7 +21,7 @@ export const Provider = ({
}: {
children?: ReactNode
store?: Store
}) => {
}): FunctionComponentElement<{ value: Store | undefined }> => {
const storeRef = useRef<Store>()
if (!store && !storeRef.current) {
storeRef.current = createStore()
Expand Down
6 changes: 3 additions & 3 deletions src/react/useAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type {
ExtractAtomResult,
ExtractAtomValue,
WritableAtom,
} from 'jotai/vanilla'
import { useAtomValue } from './useAtomValue'
import { useSetAtom } from './useSetAtom'
} from '../vanilla.ts'
import { useAtomValue } from './useAtomValue.ts'
import { useSetAtom } from './useSetAtom.ts'

type SetAtom<Args extends any[], Result> = (...args: Args) => Result

Expand Down
4 changes: 2 additions & 2 deletions src/react/useAtomValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import ReactExports, { useDebugValue, useEffect, useReducer } from 'react'
import type { ReducerWithoutAction } from 'react'
import type { Atom, ExtractAtomValue } from 'jotai/vanilla'
import { useStore } from './Provider'
import type { Atom, ExtractAtomValue } from '../vanilla.ts'
import { useStore } from './Provider.ts'

type Store = ReturnType<typeof useStore>

Expand Down
4 changes: 2 additions & 2 deletions src/react/useSetAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type {
ExtractAtomArgs,
ExtractAtomResult,
WritableAtom,
} from 'jotai/vanilla'
import { useStore } from './Provider'
} from '../vanilla.ts'
import { useStore } from './Provider.ts'

type SetAtom<Args extends any[], Result> = (...args: Args) => Result
type Store = ReturnType<typeof useStore>
Expand Down
8 changes: 4 additions & 4 deletions src/react/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { useResetAtom } from './utils/useResetAtom'
export { useReducerAtom } from './utils/useReducerAtom'
export { useAtomCallback } from './utils/useAtomCallback'
export { useHydrateAtoms } from './utils/useHydrateAtoms'
export { useResetAtom } from './utils/useResetAtom.ts'
export { useReducerAtom } from './utils/useReducerAtom.ts'
export { useAtomCallback } from './utils/useAtomCallback.ts'
export { useHydrateAtoms } from './utils/useHydrateAtoms.ts'
6 changes: 3 additions & 3 deletions src/react/utils/useAtomCallback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo } from 'react'
import { useSetAtom } from 'jotai/react'
import { atom } from 'jotai/vanilla'
import type { Getter, Setter } from 'jotai/vanilla'
import { useSetAtom } from '../../react.ts'
import { atom } from '../../vanilla.ts'
import type { Getter, Setter } from '../../vanilla.ts'

type Options = Parameters<typeof useSetAtom>[1]

Expand Down
4 changes: 2 additions & 2 deletions src/react/utils/useHydrateAtoms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useStore } from 'jotai/react'
import type { WritableAtom } from 'jotai/vanilla'
import { useStore } from '../../react.ts'
import type { WritableAtom } from '../../vanilla.ts'

type Store = ReturnType<typeof useStore>
type Options = Parameters<typeof useStore>[0]
Expand Down
4 changes: 2 additions & 2 deletions src/react/utils/useReducerAtom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'
import { useAtom } from 'jotai/react'
import type { PrimitiveAtom } from 'jotai/vanilla'
import { useAtom } from '../../react.ts'
import type { PrimitiveAtom } from '../../vanilla.ts'

type Options = Parameters<typeof useAtom>[1]

Expand Down
6 changes: 3 additions & 3 deletions src/react/utils/useResetAtom.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback } from 'react'
import { useSetAtom } from 'jotai/react'
import type { WritableAtom } from 'jotai/vanilla'
import { RESET } from 'jotai/vanilla/utils'
import { useSetAtom } from '../../react.ts'
import { RESET } from '../../vanilla/utils.ts'
import type { WritableAtom } from '../../vanilla.ts'

type Options = Parameters<typeof useSetAtom>[1]

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'jotai/vanilla/utils'
export * from 'jotai/react/utils'
export * from './vanilla/utils.ts'
export * from './react/utils.ts'
8 changes: 4 additions & 4 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export { atom } from './vanilla/atom'
export type { Atom, WritableAtom, PrimitiveAtom } from './vanilla/atom'
export { createStore, getDefaultStore } from './vanilla/store'
export { atom } from './vanilla/atom.ts'
export type { Atom, WritableAtom, PrimitiveAtom } from './vanilla/atom.ts'
export { createStore, getDefaultStore } from './vanilla/store.ts'
export type {
Getter,
Setter,
ExtractAtomValue,
ExtractAtomArgs,
ExtractAtomResult,
SetStateAction,
} from './vanilla/typeUtils'
} from './vanilla/typeUtils.ts'
2 changes: 1 addition & 1 deletion src/vanilla/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Atom, WritableAtom } from './atom'
import type { Atom, WritableAtom } from './atom.ts'

type AnyValue = unknown
type AnyError = unknown
Expand Down
2 changes: 1 addition & 1 deletion src/vanilla/typeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Atom, PrimitiveAtom, WritableAtom } from './atom'
import type { Atom, PrimitiveAtom, WritableAtom } from './atom.ts'

export type Getter = Parameters<Atom<unknown>['read']>[0]
export type Setter = Parameters<
Expand Down
Loading

0 comments on commit 9297b8b

Please sign in to comment.