Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ESM #70

Merged
merged 2 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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