Repository: th-ch/youtube-music
File: vite-plugins/plugin-importer.mts (TypeScript)

Code before:
          writer.writeLine(
            `import ${snakeToCamel(name)}PluginImport, { pluginStub as ${snakeToCamel(name)}PluginStubImport } from "./${relativePath}";`,
          );
          writer.writeLine(
            `const ${snakeToCamel(name)}Plugin = () => Promise.resolve(${snakeToCamel(name)}PluginImport);`,
          );
          writer.writeLine(
            `const ${snakeToCamel(name)}PluginStub = () => Promise.resolve(${snakeToCamel(name)}PluginStubImport);`,
          );
        }
      }

      writer.blankLine();

      // Context-specific exports
      writer.writeLine(`let ${mode}PluginsCache;`);
      writer.writeLine(`export const ${mode}Plugins = async () => {`);
      writer.writeLine(
        `  if (${mode}PluginsCache) return ${mode}PluginsCache;`,
      );
      writer.writeLine(`  ${mode}PluginsCache = {`);
      for (const { name } of plugins) {
        const checkMode = mode === 'main' ? 'backend' : mode;
        // HACK: To avoid situation like importing renderer plugins in main
        writer.writeLine(
          `    ...((await ${snakeToCamel(name)}Plugin())['${checkMode}'] ? { "${name}": await ${snakeToCamel(name)}Plugin() } : {}),`,
        );
      }
      writer.writeLine('  };');
      writer.writeLine(`  return ${mode}PluginsCache;`);
      writer.writeLine('};');
      writer.blankLine();

      // All plugins export (stub only) // Omit<Plugin, 'backend' | 'preload' | 'renderer'>
      writer.writeLine('let allPluginsCache;');
      writer.writeLine('export const allPlugins = async () => {');
      writer.writeLine('  if (allPluginsCache) return allPluginsCache;');
      writer.writeLine('  allPluginsCache = {');
      for (const { name } of plugins) {
        writer.writeLine(
          `    "${name}": await ${snakeToCamel(name)}PluginStub(),`,
        );
      }
      writer.writeLine('  };');
      writer.writeLine('  return allPluginsCache;');
      writer.writeLine('};');
      writer.blankLine();
    },
    { overwrite: true },
  );


Review comment (suggestion):
I suggest using `.then` to avoid unnecessary promise invocations

```suggestion
          `    ...(await ${snakeToCamel(name)}Plugin().then((plg) => (plg['${checkMode}'] ? { "${name}": plg } : {}))),`,
```

Code after:
          writer.writeLine(
            `import ${kebabToCamel(name)}PluginImport, { pluginStub as ${kebabToCamel(name)}PluginStubImport } from "./${relativePath}";`,
          );
          writer.writeLine(
            `const ${kebabToCamel(name)}Plugin = () => Promise.resolve(${kebabToCamel(name)}PluginImport);`,
          );
          writer.writeLine(
            `const ${kebabToCamel(name)}PluginStub = () => Promise.resolve(${kebabToCamel(name)}PluginStubImport);`,
          );
        }
      }

      writer.blankLine();

      // Context-specific exports
      writer.writeLine(`let ${mode}PluginsCache = null;`);
      writer.writeLine(`export const ${mode}Plugins = async () => {`);
      writer.writeLine(
        `  if (${mode}PluginsCache) return await ${mode}PluginsCache;`,
      );
      writer.writeLine(
        '  const { promise, resolve } = Promise.withResolvers();',
      );
      writer.writeLine('  ' + `${mode}PluginsCache = promise;`);
      writer.writeLine('  const pluginEntries = await Promise.all([');
      for (const { name } of plugins) {
        const checkMode = mode === 'main' ? 'backend' : mode;
        // HACK: To avoid situation like importing renderer plugins in main
        writer.writeLine(
          `    ${kebabToCamel(name)}Plugin().then((plg) => plg['${checkMode}'] ? ["${name}", plg] : null),`,
        );
      }
      writer.writeLine('  ]);');
      writer.writeLine(
        '  resolve(pluginEntries.filter((entry) => entry).reduce((acc, [name, plg]) => { acc[name] = plg; return acc; }, {}));',
      );
      writer.writeLine(`  return await ${mode}PluginsCache;`);
      writer.writeLine('};');
      writer.blankLine();

      // All plugins export (stub only) // Omit<Plugin, 'backend' | 'preload' | 'renderer'>
      writer.writeLine('let allPluginsCache = null;');
      writer.writeLine('export const allPlugins = async () => {');
      writer.writeLine('  if (allPluginsCache) return await allPluginsCache;');
      writer.writeLine(
        '  const { promise, resolve } = Promise.withResolvers();',
      );
      writer.writeLine('  allPluginsCache = promise;');
      writer.writeLine('  const stubEntries = await Promise.all([');
      for (const { name } of plugins) {
        writer.writeLine(