Skip to content

Commit c1ff717

Browse files
committed
refactor(core): split up Promise.all calls and ternaries
Builds each list of promises before awaiting it, imports the Shiki themes in a step of their own, and replaces the ternaries resolving and configuring Markdown plugins with `if` statements, so each step reads on its own. Assisted-by: Claude Opus 5.5 <[email protected]>
1 parent 0919aff commit c1ff717

3 files changed

Lines changed: 44 additions & 46 deletions

File tree

‎packages/core/src/plugins/shiki/highlighter.mjs‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ const highlighters = new Map();
6363
* @returns {Promise<Array<T>>}
6464
*/
6565
const importList = async options => {
66-
const imported = await Promise.all(
67-
options.map(option =>
68-
typeof option === 'string' ? importFromURL(option) : option
69-
)
66+
const imports = options.map(option =>
67+
typeof option === 'string' ? importFromURL(option) : option
7068
);
7169

70+
const imported = await Promise.all(imports);
71+
7272
return imported.flat();
7373
};
7474

@@ -86,7 +86,9 @@ const importTheme = async (theme, scheme) => {
8686

8787
if (typeof theme === 'string') {
8888
if (theme in bundledThemes) {
89-
imported = (await bundledThemes[theme]()).default;
89+
const { default: bundledTheme } = await bundledThemes[theme]();
90+
91+
imported = bundledTheme;
9092
} else {
9193
imported = await importFromURL(theme);
9294
}
@@ -109,17 +111,11 @@ const importHighlighter = async ({
109111
}) => {
110112
engine ??= createEngine();
111113

112-
const [regexEngine, importedLangs, importedTransformers, importedThemes] =
113-
await Promise.all([
114-
engine,
115-
importList(langs),
116-
importList(transformers),
117-
themes &&
118-
Promise.all([
119-
importTheme(themes.light, 'light'),
120-
importTheme(themes.dark, 'dark'),
121-
]),
122-
]);
114+
const [regexEngine, importedLangs, importedTransformers] = await Promise.all([
115+
engine,
116+
importList(langs),
117+
importList(transformers),
118+
]);
123119

124120
const coreOptions = {
125121
engine: regexEngine,
@@ -131,10 +127,13 @@ const importHighlighter = async ({
131127
const highlighterOptions = { transformers: importedTransformers };
132128

133129
// Without themes of its own, the highlighter has a default light and dark one
134-
if (importedThemes) {
135-
const [light, dark] = importedThemes;
130+
if (themes) {
131+
const [light, dark] = await Promise.all([
132+
importTheme(themes.light, 'light'),
133+
importTheme(themes.dark, 'dark'),
134+
]);
136135

137-
coreOptions.themes = importedThemes;
136+
coreOptions.themes = [light, dark];
138137
highlighterOptions.themes = { light: light.name, dark: dark.name };
139138
highlighterOptions.defaultColor = 'light';
140139
}

‎packages/core/src/utils/configuration/index.mjs‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,16 @@ export const getDefaultConfig = (generators, config) =>
9393
*/
9494
const resolveMarkdownPlugins = (config, filePath) =>
9595
Object.fromEntries(
96-
Object.entries(config).map(([name, value]) => [
97-
name,
98-
value?.markdown
99-
? {
100-
...value,
101-
markdown: resolveMarkdown(
102-
value.markdown,
103-
`${name}.markdown`,
104-
filePath
105-
),
106-
}
107-
: value,
108-
])
96+
Object.entries(config).map(([name, value]) => {
97+
if (value?.markdown) {
98+
const label = `${name}.markdown`;
99+
const markdown = resolveMarkdown(value.markdown, label, filePath);
100+
101+
return [name, { ...value, markdown }];
102+
}
103+
104+
return [name, value];
105+
})
109106
);
110107

111108
/**
@@ -170,10 +167,13 @@ const configureMarkdown = (generator, markdown = {}, global) => {
170167
continue;
171168
}
172169

173-
configured[list] = [
174-
...(list === 'remarkPlugins' && renders ? [] : global.markdown[list]),
175-
...(markdown[list] ?? []),
176-
];
170+
const plugins = markdown[list] ?? [];
171+
172+
if (list === 'remarkPlugins' && renders) {
173+
configured[list] = plugins;
174+
} else {
175+
configured[list] = [...global.markdown[list], ...plugins];
176+
}
177177
}
178178

179179
return generator.markdown && configured;

‎packages/core/src/utils/markdown/plugins.mjs‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,17 @@ export const loadMarkdownPlugins = async (generator, markdown = {}) => {
195195
const own = {};
196196

197197
// The lists import at once, so a slow plugin (Shiki) doesn't hold the rest
198-
await Promise.all(
199-
PLUGIN_LISTS.map(async list => {
200-
const plugins = configureList(pipeline[list], markdown[list]);
198+
const loading = PLUGIN_LISTS.map(async list => {
199+
const plugins = configureList(pipeline[list], markdown[list]);
200+
const imports = plugins.map(({ entry }) => importPlugin(entry));
201201

202-
const imported = await Promise.all(
203-
plugins.map(({ entry }) => importPlugin(entry))
204-
);
202+
const imported = await Promise.all(imports);
205203

206-
configured[list] = imported;
207-
own[list] = imported.filter((_, index) => !plugins[index].added);
208-
})
209-
);
204+
configured[list] = imported;
205+
own[list] = imported.filter((_, index) => !plugins[index].added);
206+
});
207+
208+
await Promise.all(loading);
210209

211210
loadedPipelines.set(generator.name, { generator, key, configured, own });
212211
};

0 commit comments

Comments
 (0)