Skip to content

Commit

Permalink
feat: support ESM (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Jul 16, 2023
1 parent 5064e92 commit 4173cfe
Show file tree
Hide file tree
Showing 3 changed files with 435 additions and 6 deletions.
73 changes: 73 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
This script is heavily inspired by `built.ts` used in @kaze-style/react.
https://github.com/taishinaritomi/kaze-style/blob/main/scripts/build.ts
MIT License
Copyright (c) 2022 Taishi Naritomi
*/

import { exec } from 'child_process'
import fs from 'fs'
import path from 'path'
import { build } from 'esbuild'
import type { Plugin, PluginBuild, BuildOptions } from 'esbuild'
import glob from 'glob'

const entryPoints = glob.sync('./src/**/*.ts', {
ignore: ['./src/types.ts'],
})

/*
This plugin is inspired by the following.
https://github.com/evanw/esbuild/issues/622#issuecomment-769462611
*/
const addExtension = (extension: string = '.js', fileExtension: string = '.ts'): Plugin => ({
name: 'add-extension',
setup(build: PluginBuild) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.importer) {
const p = path.join(args.resolveDir, args.path)
let tsPath = `${p}${fileExtension}`

let importPath = ''
if (fs.existsSync(tsPath)) {
importPath = args.path + extension
} else {
tsPath = path.join(args.resolveDir, args.path, `index${fileExtension}`)
if (fs.existsSync(tsPath)) {
importPath = `${args.path}/index${extension}`
}
}
return { path: importPath, external: true }
}
})
},
})

const commonOptions: BuildOptions = {
entryPoints,
logLevel: 'info',
platform: 'node',
}

const cjsBuild = () =>
build({
...commonOptions,
outbase: './src',
outdir: './dist',
format: 'cjs',
})

const esmBuild = () =>
build({
...commonOptions,
bundle: true,
outbase: './src',
outdir: './dist',
format: 'esm',
outExtension: { '.js': '.mjs' },
plugins: [addExtension('.mjs')],
})

Promise.all([esmBuild(), cjsBuild()])

exec(`tsc --emitDeclarationOnly --declaration`)
22 changes: 17 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./serve-static": "./dist/serve-static.js",
"./vercel": "./dist/vercel.js"
"./serve-static": {
"types": "./dist/serve-static.d.ts",
"require": "./dist/serve-static.js",
"import": "./dist/serve-static.mjs"
},
"./vercel": {
"types": "./dist/vercel.d.ts",
"require": "./dist/vercel.js",
"import": "./dist/vercel.mjs"
}
},
"typesVersions": {
"*": {
Expand All @@ -30,7 +39,7 @@
},
"scripts": {
"test": "jest",
"build": "rimraf dist && tsc",
"build": "rimraf dist && tsx ./build.ts",
"postbuild": "publint",
"prerelease": "yarn build && yarn test",
"release": "np"
Expand All @@ -51,16 +60,19 @@
},
"dependencies": {},
"devDependencies": {
"@types/glob": "^8.1.0",
"@types/jest": "^29.5.3",
"@types/node": "^18.7.16",
"@types/supertest": "^2.0.12",
"esbuild": "^0.18.13",
"hono": "^3.3.0",
"jest": "^29.6.1",
"np": "^7.7.0",
"publint": "^0.1.16",
"rimraf": "^3.0.2",
"supertest": "^6.2.4",
"ts-jest": "^29.1.1",
"tsx": "^3.12.7",
"typescript": "^4.8.3"
}
}
}
Loading

0 comments on commit 4173cfe

Please sign in to comment.