From 1ffb6f05932c8c9ea63f0b38bb1be95b2aa4a636 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 11 Aug 2023 03:52:41 +0200 Subject: [PATCH] feat: types for each export --- package.json | 8 -------- rollup.config.mjs | 28 +++++++++++++++++++++++++++- src/core.ts | 2 ++ src/index.ts | 2 ++ src/types.ts | 2 +- test/cf.ts | 5 +---- tsconfig.json | 2 +- 7 files changed, 34 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index e6e1dde77..7a5ede888 100644 --- a/package.json +++ b/package.json @@ -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" ], diff --git a/rollup.config.mjs b/rollup.config.mjs index 6ba803ba0..c657c04ec 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,4 +1,5 @@ // @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' @@ -6,6 +7,8 @@ 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 = [ @@ -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: [ @@ -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)) diff --git a/src/core.ts b/src/core.ts index 3971bd8cc..17a6c631d 100644 --- a/src/core.ts +++ b/src/core.ts @@ -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[] diff --git a/src/index.ts b/src/index.ts index 64df3ef76..f75d8ddf5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)[] diff --git a/src/types.ts b/src/types.ts index 5ec83ddc8..e517ce67c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,7 +9,7 @@ export type BuiltinThemes = keyof typeof themes export type Awaitable = T | Promise export type MaybeGetter = T | (() => Awaitable) -export type ThemeInput = MaybeGetter +export type ThemeInput = MaybeGetter export type LanguageInput = MaybeGetter export interface LanguageRegistration { diff --git a/test/cf.ts b/test/cf.ts index 1740dfda6..647f5f4d8 100644 --- a/test/cf.ts +++ b/test/cf.ts @@ -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)) diff --git a/tsconfig.json b/tsconfig.json index 56db95e25..f9f9529f9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "target": "es2018", "module": "esnext", "lib": ["esnext", "DOM"], - "moduleResolution": "node", + "moduleResolution": "Bundler", "esModuleInterop": true, "strict": true, "strictNullChecks": true,