Skip to content

Commit

Permalink
Fix import and support tree shaking (#23)
Browse files Browse the repository at this point in the history
* Fix import and support tree shaking

* Fix test

* Fix build
  • Loading branch information
kanziw authored Apr 20, 2024
1 parent 29ffe63 commit 983c259
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Build for type check
run: |
yarn build --noEmit
yarn build
test:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
lib/
coverage/
node_modules/
package.tgz
2 changes: 2 additions & 0 deletions packages/time/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*.*js
/*.d.ts
8 changes: 8 additions & 0 deletions packages/time/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ await Promise.all([
])
```

## delay
```ts
import { delay } from '@kanziw/time'

// It takes about 100ms
await delay(100)
```

## stopwatch

Measure elapsed ms using perf_hooks.performance
Expand Down
55 changes: 46 additions & 9 deletions packages/time/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
{
"name": "@kanziw/time",
"version": "0.2.3",
"version": "0.3.3",
"description": "A collection of utility libraries about time",
"repository": {
"type": "git",
"url": "git+https://github.com/kanziw/kanziwjs.git",
"directory": "packages/time"
},
"publishConfig": {
"access": "public"
},
"homepage": "https://github.com/kanziw/kanziwjs/tree/main/packages/time#readme",
"keywords": [
"time",
Expand All @@ -24,13 +21,53 @@
"name": "Jiwoong Jung",
"email": "kanziwoong@gmail.com"
},
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"exports": {
".": "./index.js"
},
"publishConfig": {
"access": "public",
"type": "commonjs",
"main": "./index.js",
"types": "./index.d.ts",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js",
"require": "./index.cjs"
},
"./cancellableDelay": {
"types": "./cancellableDelay.d.ts",
"import": "./cancellableDelay.js",
"require": "./cancellableDelay.cjs"
},
"./delay": {
"types": "./delay.d.ts",
"import": "./delay.js",
"require": "./delay.cjs"
},
"./stopwatch": {
"types": "./stopwatch.d.ts",
"import": "./stopwatch.js",
"require": "./stopwatch.cjs"
},
"./timezone": {
"types": "./timezone.d.ts",
"import": "./timezone.js",
"require": "./timezone.cjs"
},
"./package.json": "./package.json"
}
},
"files": [
"/*.d.ts",
"/*.*js"
],
"scripts": {
"test": "yarn workspace kanziwjs test packages/time",
"clean": "rimraf lib",
"build": "yarn clean && yarn tsc",
"publish": "yarn build && yarn publish-if-not-published"
"prepack": "yarn build",
"build": "node scripts/build-exports.js && tsc -p tsconfig.build.json",
"publish": "yarn build && yarn publish-if-not-published",
"publish:rc": "yarn build && yarn publish-if-not-published --no-tag-check -- --tag rc"
},
"devDependencies": {
"@types/jest": "^29.5.11",
Expand Down
46 changes: 46 additions & 0 deletions packages/time/scripts/build-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require('fs')
const path = require('path')
const { build } = require('esbuild')

const rootDir = path.join(__dirname, '..')
const srcDir = path.join(rootDir, 'src')
const distDir = path.join(rootDir, '')

try {
fs.rmSync(path.join(distDir, '*.d.ts'), { recursive: true })
fs.rmSync(path.join(distDir, '*.*js'), { recursive: true })
} catch {}

const src = (name) => path.join(srcDir, name)
const modules = fs.readdirSync(srcDir).filter((name) => name !== '__tests__')

/**
* @param {string} content
*/
function rewriteCjsEntries(content) {
return content.replace(/(require\("(?<id>.*)\.js"\))/g, (_match, _group1, id) => {
return `require("${id}.cjs")`
})
}
;(async () => {
const entryPoints = modules.map(src)
await build({
entryPoints,
outdir: distDir,
outExtension: { '.js': '.js' },
format: 'esm',
treeShaking: true,
write: true,
})
const { outputFiles: cjsOutputFiles = [] } = await build({
entryPoints,
outdir: distDir,
outExtension: { '.js': '.cjs' },
format: 'cjs',
treeShaking: true,
write: false,
})
for (const file of cjsOutputFiles) {
fs.writeFileSync(file.path, rewriteCjsEntries(file.text), 'utf8')
}
})()
4 changes: 2 additions & 2 deletions packages/time/src/__tests__/cancellableDelay.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cancellableDelay } from '../cancellableDelay'
import { startStopwatch } from '../stopwatch'
import cancellableDelay from '../cancellableDelay'
import startStopwatch from '../stopwatch'

describe('cancellableDelay', () => {
test('delay', async () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/time/src/__tests__/stopwatch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { setTimeout as delay } from 'timers/promises'

import { startStopwatch } from '../stopwatch'
import delay from '../delay'
import startStopwatch from '../stopwatch'

describe('startStopwatch', () => {
test('end', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/time/src/cancellableDelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type CancellabledDelay<T> = {
promise: Promise<T>
}

export const cancellableDelay = <T>(ms: number, value?: T): CancellabledDelay<Maybe<T>> => {
const cancellableDelay = <T>(ms: number, value?: T): CancellabledDelay<Maybe<T>> => {
/* istanbul ignore next line */
const ret: CancellabledDelay<Maybe<T>> = {
cancel: () => undefined,
Expand All @@ -31,3 +31,5 @@ export const cancellableDelay = <T>(ms: number, value?: T): CancellabledDelay<Ma

return ret
}

export default cancellableDelay
3 changes: 3 additions & 0 deletions packages/time/src/delay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

export default delay
5 changes: 3 additions & 2 deletions packages/time/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './cancellableDelay'
export * from './stopwatch'
export { default as cancellableDelay } from './cancellableDelay'
export { default as delay } from './delay'
export { default as startStopwatch } from './stopwatch'
export * from './timezone'
4 changes: 3 additions & 1 deletion packages/time/src/stopwatch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { performance } from 'perf_hooks'

export const startStopwatch = () => {
const startStopwatch = () => {
const start = performance.now()

return {
Expand All @@ -9,3 +9,5 @@ export const startStopwatch = () => {
},
}
}

export default startStopwatch
4 changes: 4 additions & 0 deletions packages/time/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["src/__tests__"]
}
4 changes: 3 additions & 1 deletion packages/time/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"outDir": "",
"declaration": true,
"emitDeclarationOnly": true
},
"include": ["src"]
}

0 comments on commit 983c259

Please sign in to comment.