Skip to content

Commit

Permalink
change compiler api. compileFile => transpileFile, compileOnCapsules …
Browse files Browse the repository at this point in the history
…=> build (#2860)
  • Loading branch information
davidfirst committed Jul 18, 2020
1 parent 6c882b6 commit 09a5fec
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/extensions/compiler/README.md
Expand Up @@ -25,17 +25,17 @@ getCompiler(): Compiler {
The compiler is responsible for two processes:
1. compile during development
This compilation takes place on the workspace and the dists are saved inside the component dir.
The provider should implement `compileFile` function as follows:
The provider should implement `transpileFile` function as follows:
```
compileFile: (fileContent: string, options: { componentDir: string, filePath: string }) => Array<{ outputText: string, outputPath: string }> | null;
transpileFile: (fileContent: string, options: { componentDir: string, filePath: string }) => Array<{ outputText: string, outputPath: string }> | null;
```
In case the compiler receive an unsupported file, it should return null.

2. compile for build (during the tag command)
This compilation takes place on the isolated capsule.
The provider should implement `compileOnCapsules` function which returns the exit-code and the dist dir.
The provider should implement `build` function which returns the exit-code and the dist dir.
From Compiler interface:
```
compileOnCapsules(context: BuildContext): Promise<BuildResults>;
build(context: BuildContext): Promise<BuildResults>;
```
FYI, this api is going to be changed very soon. It should get components and capsules graph.
6 changes: 3 additions & 3 deletions src/extensions/compiler/compile.ts
Expand Up @@ -72,8 +72,8 @@ export class Compile {
componentsAndNewCompilers: ComponentsAndNewCompilers[]
): Promise<BuildResult[]> {
const build = async ({ component, compilerName: compilerId, compilerInstance }: ComponentsAndNewCompilers) => {
if (!compilerInstance.compileFile) {
throw new Error(`compiler ${compilerId.toString()} doesn't implement "compileFile" interface`);
if (!compilerInstance.transpileFile) {
throw new Error(`compiler ${compilerId.toString()} doesn't implement "transpileFile" interface`);
}
const packageName = componentIdToPackageName(component);
const packageDir = path.join('node_modules', packageName);
Expand All @@ -90,7 +90,7 @@ export class Compile {
const options = { componentDir, filePath: file.relative };
let compileResults;
try {
compileResults = compilerInstance.compileFile(file.contents.toString(), options);
compileResults = compilerInstance.transpileFile(file.contents.toString(), options);
} catch (error) {
compileErrors.push({ path: file.path, error });
return;
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/compiler/compiler.task.ts
Expand Up @@ -10,6 +10,6 @@ export class CompilerTask implements BuildTask {

async execute(context: BuildContext): Promise<BuildResults> {
const compilerInstance: Compiler = context.env.getCompiler();
return compilerInstance.compileOnCapsules(context);
return compilerInstance.build(context);
}
}
18 changes: 9 additions & 9 deletions src/extensions/compiler/types.ts
@@ -1,12 +1,12 @@
import { ConcreteService } from '../environments/services/concrete-service';
import { BuildResults, BuildContext } from '../builder';

export type CompilerOpts = {
componentDir: string;
filePath: string;
export type TranspileOpts = {
componentDir: string; // absolute path of the component's root directory
filePath: string; // relative path of the file inside the component directory
};

export type CompilerOutput =
export type TranspileOutput =
| {
outputText: string;
outputPath: string;
Expand All @@ -15,13 +15,13 @@ export type CompilerOutput =

export interface Compiler extends ConcreteService {
/**
* transpile a single file. this used by Bit for development.
* transpile a single file. this being used during development and get saved into the workspace
*/
compileFile: (fileContent: string, options: CompilerOpts) => CompilerOutput;
transpileFile: (fileContent: string, options: TranspileOpts) => TranspileOutput;

/**
* build component for production use.
* @param context
* compile components inside isolated capsules. this being used during tag for the release.
* meaning, the final package of the component has the dists generated by this method.
*/
compileOnCapsules(context: BuildContext): Promise<BuildResults>;
build(context: BuildContext): Promise<BuildResults>;
}
6 changes: 3 additions & 3 deletions src/extensions/stencil/stencil.compiler.ts
@@ -1,12 +1,12 @@
import { transpileSync, TranspileOptions } from '@stencil/core/compiler';
import { Compiler } from '../compiler';
import { BuildContext, BuildResults } from '../builder';
import { CompilerOutput, CompilerOpts } from '../compiler/types';
import { TranspileOutput, TranspileOpts } from '../compiler/types';

export class StencilCompiler implements Compiler {
constructor(private transpileOpts: TranspileOptions) {}

compileFile(fileContent: string, options: CompilerOpts): CompilerOutput {
transpileFile(fileContent: string, options: TranspileOpts): TranspileOutput {
const output = transpileSync(fileContent, this.transpileOpts);
const path = options.filePath.split('.');
path[path.length - 1] = 'js';
Expand All @@ -21,7 +21,7 @@ export class StencilCompiler implements Compiler {

// TODO: remove this once use context
// eslint-disable-next-line @typescript-eslint/no-unused-vars
compileOnCapsules(context: BuildContext): Promise<BuildResults> {
build(context: BuildContext): Promise<BuildResults> {
throw new Error('Method not implemented.');
}
}
4 changes: 2 additions & 2 deletions src/extensions/typescript/typescript.compiler.ts
Expand Up @@ -18,7 +18,7 @@ export class TypescriptCompiler implements Compiler {
private types: string[]
) {}

compileFile(
transpileFile(
fileContent: string,
options: { componentDir: string; filePath: string }
): { outputText: string; outputPath: string }[] | null {
Expand Down Expand Up @@ -65,7 +65,7 @@ export class TypescriptCompiler implements Compiler {
return outputFiles;
}

async compileOnCapsules({ capsuleGraph }: { capsuleGraph: Network }): Promise<BuildResults> {
async build({ capsuleGraph }: { capsuleGraph: Network }): Promise<BuildResults> {
const capsules = capsuleGraph.capsules;
const capsuleDirs = capsules.getAllCapsuleDirs();

Expand Down

0 comments on commit 09a5fec

Please sign in to comment.