Skip to content

Commit

Permalink
Merge pull request #63 from getditto/pv/esm-export
Browse files Browse the repository at this point in the history
feat: publish both ESM and CommonJS
  • Loading branch information
pvditto authored Sep 25, 2024
2 parents faf5ffa + 38c9928 commit ea02759
Show file tree
Hide file tree
Showing 22 changed files with 243 additions and 63 deletions.
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,27 @@
"simple-import-sort/exports": "error",
"sort-imports": "off",
"semi": 0,
"import/extensions": ["error", "ignorePackages"],
"prettier/prettier": [
"error",
{
"semi": false
}
]
},
"overrides": [{
"files": ["*.spec.*"],
"rules": {
"import/extensions": "off",
}
}],
"plugins": [
"@typescript-eslint",
"prettier",
"simple-import-sort",
"react-hooks",
"deprecation"
"deprecation",
"eslint-plugin-import"
],
"settings": {
"react": {
Expand Down
22 changes: 17 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@
"name": "@dittolive/react-ditto",
"version": "0.11.1",
"description": "React wrappers for Ditto",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
".": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts"
}
},
"scripts": {
"build": "rimraf dist && tsc",
"build": "rimraf dist && npm run build:esm && npm run build:cjs && npm run build:static-files && scripts/fix-exports-type.js",
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.json",
"build:static-files": "cp package.json CHANGELOG.md README.md dist/",
"test": "karma start",
"types": "tsc -p tsconfig.json",
"types": "tsc -p tsconfig.esm.json",
"lint": "eslint . --ext .ts,.tsx",
"lint:fix": "eslint . --ext .ts,.tsx --fix",
"docs:generate": "rimraf documentation-website/* && typedoc",
"docs:serve": "live-server ./documentation-website"
},
"author": "",
"author": "DittoLive Inc.",
"license": "ISC",
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,6 +56,7 @@
"eslint": "^8.27.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-deprecation": "^2.0.0",
"eslint-plugin-import": "^2.30.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react": "^7.31.10",
"eslint-plugin-react-hooks": "^4.6.0",
Expand Down
23 changes: 23 additions & 0 deletions scripts/fix-exports-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node
// This script defines the type of exports in the dist directory using minimal
// package.json files. Setting the type in the main package.json file does not
// work because we want to support both CommonJS and ES Module exports in the
// same package.

const FS = require('node:fs')

const DIST_DIR = 'dist'

FS.writeFileSync(
`${DIST_DIR}/cjs/package.json`,
JSON.stringify({
type: 'commonjs',
}),
)

FS.writeFileSync(
`${DIST_DIR}/esm/package.json`,
JSON.stringify({
type: 'module',
}),
)
4 changes: 2 additions & 2 deletions src/DittoLazyProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
DittoHash,
RegisterDitto,
UnregisterDitto,
} from './DittoContext'
import { ProviderState, RenderFunction } from './DittoProvider'
} from './DittoContext.js'
import { ProviderState, RenderFunction } from './DittoProvider.js'

export interface DittoLazyProviderProps {
initOptions?: InitOptions
Expand Down
4 changes: 2 additions & 2 deletions src/DittoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import React, {
useState,
} from 'react'

import { DittoHash, RegisterDitto, UnregisterDitto } from '.'
import { DittoContext } from './DittoContext'
import { DittoContext } from './DittoContext.js'
import { DittoHash, RegisterDitto, UnregisterDitto } from './index.js'

export type RenderFunction = (providerState: ProviderState) => ReactNode

Expand Down
6 changes: 3 additions & 3 deletions src/identity/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './useOfflinePlaygroundIdentity'
export * from './useOnlineIdentity'
export * from './useOnlinePlaygroundIdentity'
export * from './useOfflinePlaygroundIdentity.js'
export * from './useOnlineIdentity.js'
export * from './useOnlinePlaygroundIdentity.js'
16 changes: 8 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './DittoContext'
export * from './DittoLazyProvider'
export * from './DittoProvider'
export * from './identity'
export * from './mutations'
export * from './presence'
export * from './queries'
export * from './useDitto'
export * from './DittoContext.js'
export * from './DittoLazyProvider.js'
export * from './DittoProvider.js'
export * from './identity/index.js'
export * from './mutations/index.js'
export * from './presence/index.js'
export * from './queries/index.js'
export * from './useDitto.js'
2 changes: 1 addition & 1 deletion src/mutations/index.tsx
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useMutations'
export * from './useMutations.js'
2 changes: 1 addition & 1 deletion src/mutations/useMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@dittolive/ditto'
import { useCallback } from 'react'

import { useDitto } from '../useDitto'
import { useDitto } from '../useDitto.js'

export interface UpdateParams {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/presence/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './useConnectionStatus'
export * from './useConnectionStatus.js'
2 changes: 1 addition & 1 deletion src/presence/useConnectionStatus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ConnectionType, Observer } from '@dittolive/ditto'
import { useEffect, useRef, useState } from 'react'

import { useDitto } from '../useDitto'
import { useDitto } from '../useDitto.js'

export interface ConnectionStatusParams {
/**
Expand Down
12 changes: 6 additions & 6 deletions src/queries/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './useCollections'
export * from './useLazyPendingCursorOperation'
export * from './useLazyPendingIDSpecificOperation'
export * from './usePendingCursorOperation'
export * from './usePendingIDSpecificOperation'
export * from './useRemotePeers'
export * from './useCollections.js'
export * from './useLazyPendingCursorOperation.js'
export * from './useLazyPendingIDSpecificOperation.js'
export * from './usePendingCursorOperation.js'
export * from './usePendingIDSpecificOperation.js'
export * from './useRemotePeers.js'
2 changes: 1 addition & 1 deletion src/queries/useCollections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@dittolive/ditto'
import { useEffect, useRef, useState } from 'react'

import { useDitto } from '../useDitto'
import { useDitto } from '../useDitto.js'

export interface CollectionsQueryParams {
/**
Expand Down
4 changes: 2 additions & 2 deletions src/queries/useLazyPendingCursorOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from '@dittolive/ditto'
import { useEffect, useRef, useState } from 'react'

import { useDittoContext } from '../DittoContext'
import { LiveQueryParams } from './usePendingCursorOperation'
import { useDittoContext } from '../DittoContext.js'
import { LiveQueryParams } from './usePendingCursorOperation.js'

export interface LazyPendingCursorOperationReturn {
/** The initialized Ditto instance if one could be found for the provided path. */
Expand Down
4 changes: 2 additions & 2 deletions src/queries/useLazyPendingIDSpecificOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
} from '@dittolive/ditto'
import { useEffect, useRef, useState } from 'react'

import { useDittoContext } from '../DittoContext'
import { UsePendingIDSpecificOperationParams } from './usePendingIDSpecificOperation'
import { useDittoContext } from '../DittoContext.js'
import { UsePendingIDSpecificOperationParams } from './usePendingIDSpecificOperation.js'

export interface LazyPendingIDSpecificOperationReturn {
/** The initialized Ditto instance if one could be found for the provided path. */
Expand Down
4 changes: 2 additions & 2 deletions src/queries/usePendingCursorOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
} from '@dittolive/ditto'
import { useEffect, useRef, useState } from 'react'

import { useDitto } from '../useDitto'
import { useVersion } from './useVersion'
import { useDitto } from '../useDitto.js'
import { useVersion } from './useVersion.js'

export interface LiveQueryParams {
collection: string
Expand Down
2 changes: 1 addition & 1 deletion src/queries/usePendingIDSpecificOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@dittolive/ditto'
import { useEffect, useRef, useState } from 'react'

import { useDitto } from '../useDitto'
import { useDitto } from '../useDitto.js'

export interface UsePendingIDSpecificOperationParams {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/queries/useRemotePeers.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Ditto, Observer, Peer } from '@dittolive/ditto'
import { useEffect, useState } from 'react'

import { useDitto } from '../useDitto'
import { useDitto } from '../useDitto.js'

export interface UsePeersParams {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/useDitto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Ditto } from '@dittolive/ditto'
import { useEffect, useState } from 'react'

import { useDittoContext } from '.'
import { useDittoContext } from './index.js'

export interface DittoHookProps {
ditto: Ditto | null
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "node",
"declarationDir": "./dist/esm",
"outDir": "./dist/esm"
}
}
20 changes: 8 additions & 12 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es6", "dom"],
"target": "es6",
"lib": ["ESNext", "dom"],
"allowJs": true,
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"inlineSources": true,
"jsx": "react",
"esModuleInterop": true,
"declaration": true,
"declarationDir": "./dist",
"declarationDir": "./dist/cjs",
"declarationMap": true,
"outDir": "./dist",
"sourceRoot": "./src"
"outDir": "./dist/cjs"
},
"compileOnSave": false,
"exclude": [
"node_modules"
],
"include": [
"src"
]
}
"exclude": ["node_modules"],
"include": ["src"]
}
Loading

0 comments on commit ea02759

Please sign in to comment.