|
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * @import {TsConfigSourceFile} from 'typescript' |
5 | | - * @import {Plugin} from 'unified' |
6 | 5 | */ |
7 | 6 |
|
8 | | -const {pathToFileURL} = require('node:url') |
9 | 7 | const { |
10 | 8 | createMdxLanguagePlugin, |
11 | 9 | resolveRemarkPlugins |
12 | 10 | } = require('@mdx-js/language-service') |
13 | 11 | const { |
14 | | - createAsyncLanguageServicePlugin |
15 | | -} = require('@volar/typescript/lib/quickstart/createAsyncLanguageServicePlugin.js') |
16 | | -const {loadPlugin} = require('load-plugin') |
| 12 | + createLanguageServicePlugin |
| 13 | +} = require('@volar/typescript/lib/quickstart/createLanguageServicePlugin.js') |
| 14 | +const {createJiti} = require('jiti') |
17 | 15 | const {default: remarkFrontmatter} = require('remark-frontmatter') |
18 | 16 | const {default: remarkGfm} = require('remark-gfm') |
19 | 17 |
|
20 | | -const plugin = createAsyncLanguageServicePlugin( |
21 | | - ['.mdx'], |
22 | | - 2 /* JSX */, |
23 | | - async (ts, info) => { |
24 | | - if (info.project.projectKind !== ts.server.ProjectKind.Configured) { |
25 | | - return { |
26 | | - languagePlugins: [ |
27 | | - createMdxLanguagePlugin([ |
28 | | - [remarkFrontmatter, ['toml', 'yaml']], |
29 | | - remarkGfm |
30 | | - ]) |
31 | | - ] |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - const cwd = info.project.getCurrentDirectory() |
36 | | - const configFile = /** @type {TsConfigSourceFile} */ ( |
37 | | - info.project.getCompilerOptions().configFile |
38 | | - ) |
39 | | - |
40 | | - const commandLine = ts.parseJsonSourceFileConfigFileContent( |
41 | | - configFile, |
42 | | - ts.sys, |
43 | | - cwd, |
44 | | - undefined, |
45 | | - configFile.fileName |
46 | | - ) |
47 | | - |
48 | | - const plugins = await resolveRemarkPlugins( |
49 | | - commandLine.raw?.mdx, |
50 | | - (name) => |
51 | | - /** @type {Promise<Plugin>} */ ( |
52 | | - loadPlugin(name, {prefix: 'remark', from: pathToFileURL(cwd) + '/'}) |
53 | | - ) |
54 | | - ) |
55 | | - |
| 18 | +const plugin = createLanguageServicePlugin((ts, info) => { |
| 19 | + if (info.project.projectKind !== ts.server.ProjectKind.Configured) { |
56 | 20 | return { |
57 | 21 | languagePlugins: [ |
58 | | - createMdxLanguagePlugin( |
59 | | - plugins || [[remarkFrontmatter, ['toml', 'yaml']], remarkGfm], |
60 | | - Boolean(commandLine.raw?.mdx?.checkMdx), |
61 | | - commandLine.options.jsxImportSource |
62 | | - ) |
| 22 | + createMdxLanguagePlugin([ |
| 23 | + [remarkFrontmatter, ['toml', 'yaml']], |
| 24 | + remarkGfm |
| 25 | + ]) |
63 | 26 | ] |
64 | 27 | } |
65 | 28 | } |
66 | | -) |
| 29 | + |
| 30 | + const cwd = info.project.getCurrentDirectory() |
| 31 | + const configFile = /** @type {TsConfigSourceFile} */ ( |
| 32 | + info.project.getCompilerOptions().configFile |
| 33 | + ) |
| 34 | + |
| 35 | + const commandLine = ts.parseJsonSourceFileConfigFileContent( |
| 36 | + configFile, |
| 37 | + ts.sys, |
| 38 | + cwd, |
| 39 | + undefined, |
| 40 | + configFile.fileName |
| 41 | + ) |
| 42 | + |
| 43 | + const jiti = createJiti(configFile.fileName) |
| 44 | + |
| 45 | + const plugins = resolveRemarkPlugins( |
| 46 | + commandLine.raw?.mdx, |
| 47 | + (name) => jiti(name).default |
| 48 | + ) |
| 49 | + |
| 50 | + return { |
| 51 | + languagePlugins: [ |
| 52 | + createMdxLanguagePlugin( |
| 53 | + plugins || [[remarkFrontmatter, ['toml', 'yaml']], remarkGfm], |
| 54 | + Boolean(commandLine.raw?.mdx?.checkMdx), |
| 55 | + commandLine.options.jsxImportSource |
| 56 | + ) |
| 57 | + ] |
| 58 | + } |
| 59 | +}) |
67 | 60 |
|
68 | 61 | module.exports = plugin |
0 commit comments