Compiler API

transpileFile

transpileFile: (fileContent: string, options: { componentDir: string, filePath: string }) => Array<{ outputText: string, outputPath: string }> | null;
CopiedCopy

The transpilation methods are used by the Compiler Service to transpile files in the workspace. Implement either the transpileFile method or the transpileComponent method.

  • transpileFile - transpiles a single file, agnostic to the component's other files.
  • transpileComponent - transpiles all the component's files. Use this when it's not possible to transpile files without their context (i.e, without having access to the component's other files). One example of this is the angular compiler (ngPackager and ngcc) which requires the entire context, in order for it to perform.

Gets a fileContent and returns the compiled content and the output path.

note

Whenever the compiler receives an unsupported file, it should return null.

transpileComponent

transpileComponent: (params: { component: ConsumerComponent, componentDir: string, outputDir: string }) => Promise<void>;
CopiedCopy

Gets the component itself, the component dir (source dir) and the output dir (the component path in the node_modules directory). The function should compile the files, and write them into the output folder.

note

The function should create a sub folder in the output dir to put the files inside.

displayConfig (optional)

displayConfig?(): string;
CopiedCopy

The displayConfig function helps the user to see the compiler config when using the bit env <comp-id> command. It should return a string representation of the compiler config (for example JSON.stringify of the tsconfig file).

$ bit env ui/text

// ...
teambit.compilation/compiler

configured compiler: teambit.typescript/typescript (TypeScript @ 4.4.2)
compiler config:
{
  "compilerOptions": {
    "lib": [
      "es2019",
      "DOM",
      "ES6",
      "DOM.Iterable"
    ],
    "target": "es2015",
    "module": "commonjs",
    "jsx": "react",
    "declaration": true,
    "sourceMap": true,
    "skipLibCheck": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "outDir": "dist"
  },
  "exclude": [
    "dist"
  ]
}
CopiedCopy

build

build(context: BuildContext): Promise<BuiltTaskResult>;
CopiedCopy

Implements the logic of the compilation task. This method will be used by the Compiler Aspect to create a Build Task (that is, this method does not directly create a new Build Task).

The build runs on a capsules network. For each processed component, ComponentResult should be returned. You should also return an array of ArtifactDefinition . The artifacts are the compiled files written into the capsules, or any other files created by the compiler (for example the ts compiler also generate d.ts files).

distDir

distDir: string;
CopiedCopy

The relative path of the dist directory inside the capsule. e.g. "dist".

distGlobPatterns (optional)

distGlobPatterns?: string[];
CopiedCopy

Determines which of the generated files are to be saved in the bit objects (when tagging a component).

e.g. distGlobPatterns = [${this.distDir}/**, !${this.distDir}/tsconfig.tsbuildinfo];

See fast-glob for the supported glob patters syntax.

artifactName (optional)

artifactName?: string;
CopiedCopy

Defaults to "dist".

Useful when the build pipeline has multiple compiler tasks of the same compiler. For instance, when using the same Babel compiler for two different tasks, one for creating "es5" files, and the second for creating "esm". The artifact names would be "es5" and "esm" accordingly.

createTask (optional)

An API to create a Compiler task via the concrete compiler implementation (instead of doing so via the Compiler Aspect).

createTask?(name?: string): BuildTask;
CopiedCopy

pre/post build (optional)

Runs before the build pipeline has started. This is useful when some preparations are needed to be done on all envs, before the build starts.

preBuild?(context: BuildContext): Promise<void>;
CopiedCopy

Runs after the build pipeline completes (on all envs). Useful for "cleanups".

postBuild?(context: BuildContext, tasksResults: TaskResultsList): Promise<void>;
CopiedCopy

Shared methods and properties

name (optional)

name?: string;
CopiedCopy

The name of the compiler.

shouldCopyNonSupportedFiles (optional)

shouldCopyNonSupportedFiles?: boolean;
CopiedCopy

Determines whether unsupported files (such as assets) should be copied into the dist directory.

version

Returns the version of the current compiler instance (e.g. '4.0.1'). This represent for example the typescript or babel version.

version(): string;
CopiedCopy

displayName

Returns the display name of the current compiler instance (e.g. 'TypeScript').

displayName(): string;
CopiedCopy

getDistPathBySrcPath

Given a source file, Return its compiled parallel in the dists. e.g. index.ts => dist/index.js Both, the return path and the given path are relative paths. For example, this is used to calculate the main dist file by the main file of the component. It is also used to calculate different links Bit references to, when bundling the component.

getDistPathBySrcPath(srcPath: string): string;
CopiedCopy

isFileSupported

Only supported files matching get compiled. Others, are copied to the dist dir.

isFileSupported(filePath: string): boolean;
CopiedCopy