Skip to content

The open alternative to Salesforce, designed for AI.

3.7 KB
Raw Blame
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';

import { init, parse } from 'cjs-module-lexer';
import * as esbuild from 'esbuild';
import ts from 'typescript';
import { isDefined } from '@/utils/validation/isDefined';

import { getCommonFrontComponentBuildOptions } from './get-common-front-component-build-options';
import { createJsxRuntimeRemoteWrapperPlugin } from './jsx-runtime-remote-wrapper-plugin';
import { cssInjectionPlugin } from './css-injection-plugin';
import { type SharedDependenciesExportNames } from './shared-dependencies-export-names.type';

export const enumerateSharedDependenciesExportNames = async ({
  appPath,
  specifier,
  build = esbuild.build,
  buildOptions = {
    ...getCommonFrontComponentBuildOptions(),
    plugins: [createJsxRuntimeRemoteWrapperPlugin(), cssInjectionPlugin],
  },
}: {
  appPath: string;
  specifier: string;
  build?: (options: esbuild.BuildOptions) => Promise<esbuild.BuildResult>;
  buildOptions?: esbuild.BuildOptions;
}): Promise<SharedDependenciesExportNames> => {
  const isWrappedJsxRuntime = specifier === 'react/jsx-runtime';
  const result = await build({
    ...buildOptions,
    absWorkingDir: appPath,
    plugins: (buildOptions.plugins ?? []).filter(
      (plugin) =>
        isWrappedJsxRuntime || plugin.name !== 'jsx-runtime-remote-wrapper',
    ),
    entryPoints: [specifier],
    write: false,
    outfile: 'shared-dependency-probe.js',
    outExtension: undefined,
    external: [],
    sourcemap: false,
    metafile: true,
  });
  const output = Object.values(result.metafile?.outputs ?? {}).find((entry) =>
    isDefined(entry.entryPoint),
  );

  if (!isDefined(output?.entryPoint)) {
    throw new Error('Missing dependency entry point.');
  }

  const exports = new Set(output.exports);
  const visited = new Set<string>();
  const collectReexportedNames = async (path: string): Promise<void> => {
    if (visited.has(path)) {
      return;
    }
    visited.add(path);

    const input = result.metafile?.inputs[path];

    if (!isDefined(input)) {
      throw new Error(`Missing dependency input metadata for "${path}".`);
    }

    const source = await readFile(resolve(appPath, path), 'utf8');
    let reexports: string[];

    if (input.format === 'cjs') {
      await init();
      const transformed = await esbuild.transform(source, {
        define: buildOptions.define,
        minifySyntax: true,
        loader: 'js',
      });
      const parsed = parse(transformed.code);

      for (const name of parsed.exports) {
        if (name !== '__esModule') {
          exports.add(name);
        }
      }
      reexports = parsed.reexports;
    } else {
      const sourceFile = ts.createSourceFile(
        path,
        source,
        ts.ScriptTarget.Latest,
      );
      reexports = sourceFile.statements.flatMap((statement) => {
        if (
          !ts.isExportDeclaration(statement) ||
          isDefined(statement.exportClause) ||
          !isDefined(statement.moduleSpecifier) ||
          !ts.isStringLiteral(statement.moduleSpecifier)
        ) {
          return [];
        }

        return [statement.moduleSpecifier.text];
      });
    }

    for (const reexport of reexports) {
      const imported = input.imports.find(
        (entry) => entry.original === reexport && !entry.external,
      );

      if (isDefined(imported)) {
        await collectReexportedNames(imported.path);
      }
    }
  };

  // The virtual JSX wrapper declares all of its exports in the build metadata.
  if (!isWrappedJsxRuntime) {
    await collectReexportedNames(output.entryPoint);
  }

  return {
    namedExports: [...exports].filter((name) => name !== 'default').sort(),
    hasDefaultExport: exports.has('default'),
  };
};