Repository: remix-run/react-router
File: packages/react-router-dev/vite/route-chunks.ts (TypeScript)

Code before:
  return getOrSetFromCache(
    cache,
    `${cacheKey}::omitChunkedExports::${exportNames.join(
      ","
    )}::${JSON.stringify(generateOptions)}`,
    code,
    () => {
      let exportDependencies = getExportDependencies(code, cache, cacheKey);

      let omittedStatements = new Set<Statement>();
      let omittedExportedVariableDeclarators = new Set<VariableDeclarator>();

      for (let exportName of exportNames) {
        let dependencies = exportDependencies.get(exportName);

        // If the export is not chunkable then its code will still remain in the
        // main chunk, so we need to keep its top level statements.
        if (
          !dependencies ||
          !hasChunkableExport(code, exportName, cache, cacheKey)
        ) {
          continue;
        }

        // Now that we know the export is chunkable, add all of its top level
        // non-module statements to the set of statements to be omitted from the
        // main chunk. Note that we don't include top level module statements in
        // this step because we perform more fine-grained filtering of module
        // statements below.
        for (let statement of dependencies.topLevelNonModuleStatements) {
          omittedStatements.add(statement);
        }

        // We also want to omit any exported variable declarators that belong to
        // the chunked export.
        for (let declarator of dependencies.exportedVariableDeclarators) {
          omittedExportedVariableDeclarators.add(declarator);
        }
      }

      let ast = codeToAst(code, cache, cacheKey);

      let omittedStatementsArray = Array.from(omittedStatements);
      let omittedExportedVariableDeclaratorsArray = Array.from(
        omittedExportedVariableDeclarators
      );

      function isChunkable(exportName: string): boolean {
        return hasChunkableExport(code, exportName, cache, cacheKey);
      }


Review comment (suggestion):
Now that we have an `omittedExportNames` array, this could be cleaned up a bit.

Code after:
  return getOrSetFromCache(
    cache,
    `${cacheKey}::omitChunkedExports::${exportNames.join(
      ","
    )}::${JSON.stringify(generateOptions)}`,
    code,
    () => {
      const isChunkable = (exportName: string): boolean =>
        hasChunkableExport(code, exportName, cache, cacheKey);

      const isOmitted = (exportName: string): boolean =>
        exportNames.includes(exportName) && isChunkable(exportName);

      const isRetained = (exportName: string): boolean =>
        !isOmitted(exportName);

      let exportDependencies = getExportDependencies(code, cache, cacheKey);

      let allExportNames = Array.from(exportDependencies.keys());
      let omittedExportNames = allExportNames.filter(isOmitted);
      let retainedExportNames = allExportNames.filter(isRetained);

      let omittedStatements = new Set<Statement>();
      let omittedExportedVariableDeclarators = new Set<VariableDeclarator>();

      for (let omittedExportName of omittedExportNames) {
        let dependencies = exportDependencies.get(omittedExportName);

        invariant(
          dependencies,
          `Expected dependencies for ${omittedExportName}`
        );

        // Now that we know the export is chunkable, add all of its top level
        // non-module statements to the set of statements to be omitted from the
        // main chunk. Note that we don't include top level module statements in
        // this step because we perform more fine-grained filtering of module
        // statements below.
        for (let statement of dependencies.topLevelNonModuleStatements) {
          omittedStatements.add(statement);
        }

        // We also want to omit any exported variable declarators that belong to
        // the chunked export.
        for (let declarator of dependencies.exportedVariableDeclarators) {
          omittedExportedVariableDeclarators.add(declarator);
        }
      }

      let ast = codeToAst(code, cache, cacheKey);