Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
fix: .d.mts for all
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 11, 2023
1 parent 093bf38 commit 3c63c24
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"types": "./dist/index.d.mts",
"import": "./dist/index.mjs"
},
"./core": {
"types": "./dist/core.d.ts",
"types": "./dist/core.d.mts",
"import": "./dist/core.mjs"
},
"./dist/*": "./dist/*",
"./*": "./dist/*"
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"types": "./dist/index.d.mts",
"files": [
"dist"
],
Expand Down
10 changes: 6 additions & 4 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default defineConfig([
input: [
'src/index.ts',
'src/core.ts',
'src/wasm.ts',
],
output: {
dir: 'dist',
Expand All @@ -51,13 +52,14 @@ export default defineConfig([
input: [
'src/core.ts',
'src/index.ts',
'src/wasm.ts',
'src/types.ts',
],
output: {
dir: 'dist',
format: 'esm',
chunkFileNames: 'types/[name].d.ts',
entryFileNames: f => `${f.name.replace('src/', '')}.d.ts`,
chunkFileNames: 'types/[name].d.mts',
entryFileNames: f => `${f.name.replace('src/', '')}.d.mts`,
},
plugins: [
dts({
Expand All @@ -74,15 +76,15 @@ export default defineConfig([
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',
'import { LanguageRegistration } from \'../types.mjs\';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',
'import { ThemeRegisterationRaw } from \'../types.mjs\';declare const reg: ThemeRegisterationRaw;export default reg',
'utf-8',
)),
)
Expand Down
17 changes: 7 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ import type { BuiltinLanguages, BuiltinThemes, LanguageInput, ThemeInput } from
import { themes } from './vendor/themes'
import { languages } from './vendor/langs'
import { getHighlighter as getCoreHighlighter } from './core'
import { loadWasm } from './oniguruma'
import { getWasmInlined } from './wasm'

export * from './types'

export {
loadWasm,
getWasmInlined,
}

export interface HighlighterOptions {
themes?: (ThemeInput | BuiltinThemes)[]
langs?: (LanguageInput | BuiltinLanguages)[]
}

let _onigurumaPromise: Promise<{ data: ArrayBuffer }> | null = null
export async function getWasmInlined(): Promise<{ data: ArrayBuffer }> {
if (!_onigurumaPromise) {
// @ts-expect-error anyway
_onigurumaPromise = import('vscode-oniguruma/release/onig.wasm')
.then(r => ({ data: r.default as ArrayBuffer }))
}
return _onigurumaPromise
}

export async function getHighlighter(options: HighlighterOptions = {}) {
const _themes = (options.themes ?? ['nord']).map((i) => {
if (typeof i === 'string')
Expand Down
9 changes: 9 additions & 0 deletions src/wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let _onigurumaPromise: Promise<{ data: ArrayBuffer }> | null = null
export async function getWasmInlined(): Promise<{ data: ArrayBuffer }> {
if (!_onigurumaPromise) {
// @ts-expect-error anyway
_onigurumaPromise = import('vscode-oniguruma/release/onig.wasm')
.then(r => ({ data: r.default as ArrayBuffer }))
}
return _onigurumaPromise
}
7 changes: 2 additions & 5 deletions test/core.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { describe, expect, it } from 'vitest'
import { getHighlighter } from '../src/core'

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

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

// @ts-expect-error no-types
Expand All @@ -13,8 +10,8 @@ import onig from '../dist/onig.mjs'
describe('should', () => {
it('exported', async () => {
const shiki = await getHighlighter({
themes: [nord as any],
langs: [js as any],
themes: [nord],
langs: [js],
loadWasm: {
instantiator: obj => WebAssembly.instantiate(onig, obj),
},
Expand Down

0 comments on commit 3c63c24

Please sign in to comment.