Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

2.5 KB
Raw Blame
import { relative, resolve, sep } from 'node:path';

const SCHEMA_VERSION = 1;
const VIRTUAL_MODULE_PREFIX = '\0';

const toPosixPath = (value) => value.split(sep).join('/');

const getPackageOwner = (moduleId) => {
  const marker = '/node_modules/';
  const markerIndex = moduleId.lastIndexOf(marker);

  if (markerIndex === -1) {
    return null;
  }

  const packagePath = moduleId.slice(markerIndex + marker.length);
  const segments = packagePath.split('/');
  const packageName = segments[0]?.startsWith('@') ? segments.slice(0, 2).join('/') : segments[0];

  return packageName ? `package:${packageName}` : null;
};

/**
 * Keep first-party paths; collapse dependencies to package IDs to ignore pnpm layout changes. Exclude ownerless virtual
 * modules; byte budgets still cover them.
 */
export const getModuleSourceOwner = (moduleId, projectRoot) => {
  if (typeof moduleId !== 'string' || moduleId.length === 0 || moduleId.startsWith(VIRTUAL_MODULE_PREFIX)) {
    return null;
  }

  const pathWithoutQuery = moduleId.split('?')[0];
  const normalizedPath = toPosixPath(pathWithoutQuery);
  const packageOwner = getPackageOwner(normalizedPath);

  if (packageOwner) {
    return packageOwner;
  }

  const normalizedRoot = resolve(projectRoot);
  const relativePath = toPosixPath(relative(normalizedRoot, pathWithoutQuery));

  if (relativePath === '' || relativePath.startsWith('../') || relativePath === '..') {
    return null;
  }

  return `source:${relativePath}`;
};

export const createChunkSourceManifest = (bundle, projectRoot) => {
  const chunks = {};

  for (const output of Object.values(bundle)) {
    if (output.type !== 'chunk') {
      continue;
    }

    const sourceOwners = new Set(
      output.moduleIds
        .map((moduleId) => getModuleSourceOwner(moduleId, projectRoot))
        .filter((sourceOwner) => sourceOwner !== null)
    );

    chunks[output.fileName] = {
      facadeSource: getModuleSourceOwner(output.facadeModuleId, projectRoot),
      sourceOwners: [...sourceOwners].sort(),
    };
  }

  return {
    chunks: Object.fromEntries(Object.entries(chunks).sort(([left], [right]) => left.localeCompare(right))),
    schemaVersion: SCHEMA_VERSION,
  };
};

export const chunkSourceManifest = ({ projectRoot }) => ({
  name: 'invokeai-chunk-source-manifest',
  generateBundle(_outputOptions, bundle) {
    const manifest = createChunkSourceManifest(bundle, projectRoot);

    this.emitFile({
      fileName: '.vite/chunk-sources.json',
      source: `${JSON.stringify(manifest, null, 2)}\n`,
      type: 'asset',
    });
  },
});