Skip to content

Commit

Permalink
feat: types for each export
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 11, 2023
1 parent de1b61c commit 1ffb6f0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 15 deletions.
8 changes: 0 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./dist/index.d.ts"
]
}
},
"files": [
"dist"
],
Expand Down
28 changes: 27 additions & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// @ts-check
import { basename, dirname, join } from 'node:path'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import copy from 'rollup-plugin-copy'
import dts from 'rollup-plugin-dts'
import esbuild from 'rollup-plugin-esbuild'
import json from '@rollup/plugin-json'
import { defineConfig } from 'rollup'
import fs from 'fs-extra'
import fg from 'fast-glob'
import { wasmPlugin } from './build/wasm.mjs'

const plugins = [
Expand Down Expand Up @@ -48,11 +51,12 @@ export default defineConfig([
input: [
'src/core.ts',
'src/index.ts',
'src/types.ts',
],
output: {
dir: 'dist',
format: 'esm',
chunkFileNames: 'chunks/[name].d.ts',
chunkFileNames: 'types/[name].d.ts',
entryFileNames: f => `${f.name.replace('src/', '')}.d.ts`,
},
plugins: [
Expand All @@ -62,6 +66,28 @@ export default defineConfig([
copy({
targets: [{ src: './node_modules/vscode-oniguruma/release/onig.wasm', dest: 'dist' }],
}),
{
name: 'post',
async buildEnd() {
await fs.writeFile('dist/onig.d.ts', 'declare const binary: ArrayBuffer; export default binary;', 'utf-8')
const langs = await fg('dist/languages/*.mjs', { absolute: true })
await Promise.all(
langs.map(file => fs.writeFile(
join(dirname(file), `${basename(file, '.mjs')}.d.mts`),
'import { LanguageRegistration } from \'../types\';declare const reg: LanguageRegistration;export default reg',
'utf-8',
)),
)
const themes = await fg('dist/themes/*.mjs', { absolute: true })
await Promise.all(
themes.map(file => fs.writeFile(
join(dirname(file), `${basename(file, '.mjs')}.d.mts`),
'import { ThemeRegisterationRaw } from \'../types\';declare const reg: ThemeRegisterationRaw;export default reg',
'utf-8',
)),
)
},
},
],
onwarn: (warning, warn) => {
if (!/Circular/.test(warning.message))
Expand Down
2 changes: 2 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { tokenizeWithTheme } from './themedTokenizer'
import { renderToHtml } from './renderer'
import { toShikiTheme } from './normalize'

export * from './types'

export interface CoreHighlighterOptions {
themes: ThemeInput[]
langs: LanguageInput[]
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { themes } from './vendor/themes'
import { languages } from './vendor/langs'
import { getHighlighter as getCoreHighlighter } from './core'

export * from './types'

export interface HighlighterOptions {
themes?: (ThemeInput | BuiltinThemes)[]
langs?: (LanguageInput | BuiltinLanguages)[]
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type BuiltinThemes = keyof typeof themes
export type Awaitable<T> = T | Promise<T>
export type MaybeGetter<T> = T | (() => Awaitable<T>)

export type ThemeInput = MaybeGetter<ThemeRegisteration | ThemeRegisteration>
export type ThemeInput = MaybeGetter<ThemeRegisteration | ThemeRegisterationRaw>
export type LanguageInput = MaybeGetter<LanguageRegistration>

export interface LanguageRegistration {
Expand Down
5 changes: 1 addition & 4 deletions test/cf.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { getHighlighter, loadWasm } from '../src/core'

// @ts-expect-error no-types
import nord from '../dist/themes/nord.mjs'

// @ts-expect-error no-types
import js from '../dist/languages/javascript.mjs'

// @ts-expect-error no-types
// @ts-expect-error anyway
import wasm from '../dist/onig.wasm'

await loadWasm(obj => WebAssembly.instantiate(wasm, obj))
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "es2018",
"module": "esnext",
"lib": ["esnext", "DOM"],
"moduleResolution": "node",
"moduleResolution": "Bundler",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
Expand Down

0 comments on commit 1ffb6f0

Please sign in to comment.