Home / Function/ compile() — react Function Reference

compile() — react Function Reference

Architecture documentation for the compile() function in preprocessor.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  4da78376_6529_2635_7d60_6a02b4dee48f["compile()"]
  a354a7f6_7479_049c_82ce_7a1242a52f58["preprocessor.js"]
  4da78376_6529_2635_7d60_6a02b4dee48f -->|defined in| a354a7f6_7479_049c_82ce_7a1242a52f58
  style 4da78376_6529_2635_7d60_6a02b4dee48f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/jest/typescript/preprocessor.js lines 27–103

function compile(content, contentFilename) {
  let output = null;
  const compilerHost = {
    fileExists(filename) {
      return ts.sys.fileExists(filename);
    },
    getCanonicalFileName(filename) {
      return filename;
    },
    getCurrentDirectory() {
      return '';
    },
    getDefaultLibFileName: () => 'lib.d.ts',
    getNewLine: () => ts.sys.newLine,
    getSourceFile(filename, languageVersion) {
      let source;
      const libRegex = /lib\.(.+\.)?d\.ts$/;
      const jestRegex = /jest\.d\.ts/;
      const reactRegex =
        /(?:React|ReactDOM|ReactDOMClient|ReactInternalAct|PropTypes)(?:\.d)?\.ts$/;

      // `path.normalize` is used to turn forward slashes in
      // the file path into backslashes on Windows.
      filename = path.normalize(filename);
      if (filename.match(libRegex)) {
        source = fs
          .readFileSync(require.resolve('typescript/lib/' + filename))
          .toString();
      } else if (filename.match(jestRegex)) {
        source = fs.readFileSync(path.join(__dirname, 'jest.d.ts')).toString();
      } else if (filename === contentFilename) {
        source = content;
      } else if (reactRegex.test(filename)) {
        // TypeScript will look for the .d.ts files in each ancestor directory,
        // so there may not be a file at the referenced path as it climbs the
        // hierarchy.
        try {
          source = fs.readFileSync(filename).toString();
        } catch (e) {
          if (e.code === 'ENOENT') {
            return undefined;
          }
          throw e;
        }
      } else {
        throw new Error('Unexpected filename ' + filename);
      }
      return ts.createSourceFile(filename, source, 'ES5', '0');
    },
    readFile(filename) {
      return ts.sys.readFile(filename);
    },
    useCaseSensitiveFileNames() {
      return ts.sys.useCaseSensitiveFileNames;
    },
    writeFile(name, text, writeByteOrderMark) {
      if (output === null) {
        output = text;
      } else {
        throw new Error('Expected only one dependency.');
      }
    },
  };
  const program = ts.createProgram(
    ['lib.d.ts', 'jest.d.ts', contentFilename],
    tsOptions,
    compilerHost
  );
  const emitResult = program.emit();
  const errors = ts
    .getPreEmitDiagnostics(program)
    .concat(emitResult.diagnostics);
  if (errors.length) {
    throw new Error(errors.map(formatErrorMessage).join('\n'));
  }
  return output;
}

Domain

Subdomains

Frequently Asked Questions

What does compile() do?
compile() is a function in the react codebase, defined in scripts/jest/typescript/preprocessor.js.
Where is compile() defined?
compile() is defined in scripts/jest/typescript/preprocessor.js at line 27.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free