Skip to content

Commit c3a24c0

Browse files
committed
feat!: migrate cli to full ESM
1 parent 7d69362 commit c3a24c0

14 files changed

+39
-32
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// Auto fix
1818
"editor.codeActionsOnSave": {
19-
"source.fixAll.eslint": true,
19+
"source.fixAll.eslint": "explicit",
2020
"source.organizeImports": false
2121
},
2222

packages/client/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"url": "https://github.com/slidevjs/slidev"
1212
},
1313
"bugs": "https://github.com/slidevjs/slidev/issues",
14+
"exports": {
15+
"./package.json": "./package.json",
16+
"./*": "./*"
17+
},
1418
"engines": {
1519
"node": ">=18.0.0"
1620
},

packages/slidev/bin/slidev.js

-15
This file was deleted.

packages/slidev/bin/slidev.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
'use strict'
3+
import ('../dist/cli.mjs')

packages/slidev/node/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ injectPreparserExtensionLoader(async (headmatter?: Record<string, unknown>, file
4646
return await loadSetups(roots, 'preparser.ts', { filepath, headmatter }, [], mergeArrays)
4747
})
4848

49-
const cli = yargs
49+
const cli = yargs(process.argv.slice(2))
5050
.scriptName('slidev')
5151
.usage('$0 [args]')
5252
.version(version)

packages/slidev/node/options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { dirname, join, resolve } from 'node:path'
22
import process from 'node:process'
3+
import { fileURLToPath } from 'node:url'
34
import type Vue from '@vitejs/plugin-vue'
45
import type VueJsx from '@vitejs/plugin-vue-jsx'
56
import type Icons from 'unplugin-icons/vite'
@@ -19,6 +20,7 @@ import { getThemeMeta, promptForThemeInstallation, resolveThemeName } from './th
1920
import { getAddons } from './addons'
2021

2122
const debug = _debug('slidev:options')
23+
const __dirname = dirname(fileURLToPath(import.meta.url))
2224

2325
export interface SlidevEntryOptions {
2426
/**

packages/slidev/node/plugins/loaders.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import process from 'node:process'
33
import type { Connect, ModuleNode, Plugin, Update, ViteDevServer } from 'vite'
44
import { isString, notNullish, objectMap, range, slash, uniq } from '@antfu/utils'
55
import fg from 'fast-glob'
6-
import fs, { existsSync } from 'fs-extra'
6+
import fs from 'fs-extra'
77
import Markdown from 'markdown-it'
88
import { bold, gray, red, yellow } from 'kolorist'
99

@@ -552,7 +552,7 @@ defineProps<{ no: number | string }>()`)
552552
]
553553

554554
for (const style of styles) {
555-
if (existsSync(style)) {
555+
if (fs.existsSync(style)) {
556556
imports.push(`import "${toAtFS(style)}"`)
557557
continue
558558
}

packages/slidev/node/plugins/markdown-it-prism.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// forked from https://github.com/jGleitz/markdown-it-prism
22
// it's modified to output line wrapper `<div class="line" />` for each line
33

4+
import { createRequire } from 'node:module'
45
import type { Grammar } from 'prismjs'
56
import Prism from 'prismjs'
6-
import loadLanguages from 'prismjs/components/'
7+
import loadLanguages from 'prismjs/components/index.js'
78
import type MarkdownIt from 'markdown-it'
89
import * as htmlparser2 from 'htmlparser2'
910
import { escapeVueInCode } from './markdown'
1011

12+
const require = createRequire(import.meta.url)
13+
1114
interface Options {
1215
plugins: string[]
1316
/**
@@ -90,7 +93,6 @@ function loadPrismLang(lang: string): Grammar | undefined {
9093
*/
9194
function loadPrismPlugin(name: string): void {
9295
try {
93-
// eslint-disable-next-line ts/no-require-imports
9496
require(`prismjs/plugins/${name}/prism-${name}`)
9597
}
9698
catch (e) {

packages/slidev/node/plugins/setupNode.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve } from 'node:path'
2-
import { pathExists } from 'fs-extra'
2+
import { fileURLToPath } from 'node:url'
3+
import fs from 'fs-extra'
34
import { isObject } from '@antfu/utils'
45
import jiti from 'jiti'
56

@@ -26,8 +27,8 @@ export async function loadSetups<T, R extends object>(
2627
let returns = initial
2728
for (const root of roots) {
2829
const path = resolve(root, 'setup', name)
29-
if (await pathExists(path)) {
30-
const { default: setup } = jiti(__filename)(path)
30+
if (await fs.pathExists(path)) {
31+
const { default: setup } = jiti(fileURLToPath(import.meta.url))(path)
3132
const result = await setup(arg)
3233
if (result !== null) {
3334
returns = typeof merge === 'function'

packages/slidev/node/plugins/unocss.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve } from 'node:path'
22
import { existsSync } from 'node:fs'
3+
import { fileURLToPath } from 'node:url'
34
import { uniq } from '@antfu/utils'
45
import type { Theme } from '@unocss/preset-uno'
56
import type { UserConfig } from '@unocss/core'
@@ -26,7 +27,7 @@ export async function createUnocssPlugin(
2627

2728
const configs = configFiles
2829
.map((i) => {
29-
const loaded = jiti(__filename)(i)
30+
const loaded = jiti(fileURLToPath(import.meta.url))(i)
3031
const config = 'default' in loaded ? loaded.default : loaded
3132
return config
3233
})

packages/slidev/node/plugins/windicss.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { dirname, resolve } from 'node:path'
22
import { existsSync } from 'node:fs'
3+
import { fileURLToPath } from 'node:url'
34
import { slash, uniq } from '@antfu/utils'
45
import type { WindiCssOptions } from 'vite-plugin-windicss'
56
import jiti from 'jiti'
@@ -22,7 +23,7 @@ export async function createWindiCSSPlugin(
2223
])
2324

2425
const configFile = configFiles.find(i => existsSync(i))!
25-
let config = jiti(__filename)(configFile) as WindiCssOptions
26+
let config = jiti(fileURLToPath(import.meta.url))(configFile) as WindiCssOptions
2627
if (config.default)
2728
config = config.default
2829

packages/slidev/node/utils.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import { join } from 'node:path'
1+
import { dirname, join } from 'node:path'
2+
import { createRequire } from 'node:module'
3+
import { fileURLToPath } from 'node:url'
24
import { ensurePrefix, slash } from '@antfu/utils'
35
import isInstalledGlobally from 'is-installed-globally'
4-
import { sync as resolve } from 'resolve'
6+
import resolve from 'resolve'
57
import globalDirs from 'global-dirs'
68
import type Token from 'markdown-it/lib/token'
79
import type { ResolvedFontOptions } from '@slidev/types'
810

11+
const require = createRequire(import.meta.url)
12+
const __dirname = dirname(fileURLToPath(import.meta.url))
13+
914
export function toAtFS(path: string) {
1015
return `/@fs${ensurePrefix('/', slash(path))}`
1116
}
@@ -14,8 +19,9 @@ export function resolveImportPath(importName: string, ensure: true): string
1419
export function resolveImportPath(importName: string, ensure?: boolean): string | undefined
1520
export function resolveImportPath(importName: string, ensure = false) {
1621
try {
17-
return resolve(importName, {
22+
return resolve.sync(importName, {
1823
preserveSymlinks: false,
24+
basedir: __dirname,
1925
})
2026
}
2127
catch {}
@@ -40,7 +46,10 @@ export function resolveImportPath(importName: string, ensure = false) {
4046

4147
export function resolveGlobalImportPath(importName: string): string {
4248
try {
43-
return resolve(importName, { preserveSymlinks: false, basedir: __dirname })
49+
return resolve.sync(importName, {
50+
preserveSymlinks: false,
51+
basedir: __dirname,
52+
})
4453
}
4554
catch {}
4655

packages/slidev/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"module": "dist/index.mjs",
2323
"types": "dist/index.d.ts",
2424
"bin": {
25-
"slidev": "./bin/slidev.js"
25+
"slidev": "./bin/slidev.mjs"
2626
},
2727
"files": [
2828
"bin",

tsup.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { defineConfig } from 'tsup'
33
export default defineConfig({
44
format: [
55
'esm',
6-
'cjs',
76
],
87
target: 'node18',
98
splitting: true,

0 commit comments

Comments
 (0)