Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve compile performance by not creating any capsule during development #2700

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions e2e/harmony/compile.e2e.4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ chai.use(require('chai-fs'));
before(() => {
helper.command.runCmd('bit compile');
});
it('should not write dists files inside the capsule as it is not needed for development', () => {
const capsule = helper.command.getCapsuleOfComponent('comp1');
expect(path.join(capsule, 'dist')).to.not.be.a.path();
it('should not create a capsule as it is not needed for development', () => {
const capsulesJson = helper.command.runCmd('bit capsule-list -j');
const capsules = JSON.parse(capsulesJson);
capsules.capsules.forEach(c => expect(c).to.not.have.string('comp1'));
});
it('should write the dists files inside the node-modules of the component', () => {
const nmComponent = path.join(
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/compile/compile.cmd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class CompileCmd implements Command {
const compileResults = await this.compile.compile(components, { verbose, noCache });
// eslint-disable-next-line no-console
console.log('compileResults', compileResults);
return <div>Compile has been completed successfully</div>;
const output = `${compileResults.length} components have been compiled successfully`;
return <div>{output}</div>;
}

async json([components]: CLIArgs, { verbose, noCache }: Flags) {
Expand Down
81 changes: 54 additions & 27 deletions src/extensions/compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import pMapSeries from 'p-map-series';
import { Workspace } from '../workspace';
import { DEFAULT_DIST_DIRNAME } from './../../constants';
import ConsumerComponent from '../../consumer/component';
import { BitId } from '../../bit-id';
import { BitId, BitIds } from '../../bit-id';
import { ResolvedComponent } from '../workspace/resolved-component';
import buildComponent from '../../consumer/component-ops/build-component';
import { Component } from '../component';
Expand Down Expand Up @@ -84,47 +84,66 @@ export class Compile {
dontPrintEnvMsg?: boolean,
buildOnCapsules = false
): Promise<BuildResult[]> {
if (!buildOnCapsules) {
return this.compileOnWorkspace(componentsIds, noCache, verbose, dontPrintEnvMsg);
}
const componentsAndCapsules = await getComponentsAndCapsules(componentsIds, this.workspace);
logger.debug(`compilerExt, completed created of capsules for ${componentsIds.join(', ')}`);
const idsAndFlows = new IdsAndFlows();
const componentsWithLegacyCompilers: ComponentAndCapsule[] = [];
const componentsAndNewCompilers: ComponentsAndNewCompilers[] = [];
componentsAndCapsules.forEach(c => {
const compileCore = c.component.config.extensions.findCoreExtension('compile');
const compileComponent = c.component.config.extensions.findExtension('compile');
const compileComponentExported = c.component.config.extensions.findExtension('bit.core/compile', true);
const compileExtension = compileCore || compileComponent || compileComponentExported;
const compileConfig = compileExtension?.config;
const compileConfig = this.getCompilerConfig(c.consumerComponent);
const taskName = this.getTaskNameFromCompiler(compileConfig, c.component.config.extensions);
const value = taskName ? [taskName] : [];
if (buildOnCapsules) {
if (compileConfig) {
idsAndFlows.push({ id: c.consumerComponent.id, value });
} else {
componentsWithLegacyCompilers.push(c);
}
// if there is no componentDir (e.g. author that added files, not dir), then we can't write the dists
// inside the component dir.
} else if (compileConfig && compileConfig.compiler && c.consumerComponent.componentMap?.getComponentDir()) {
const compilerInstance = this.getCompilerInstance(compileConfig.compiler, c.component.config.extensions);
const compilerId = this.getCompilerBitId(compileConfig.compiler, c.component.config.extensions);
componentsAndNewCompilers.push({ component: c.consumerComponent, compilerInstance, compilerId });
if (compileConfig) {
idsAndFlows.push({ id: c.consumerComponent.id, value });
} else {
componentsWithLegacyCompilers.push(c);
}
});
let newCompilersResultOnCapsule: BuildResult[] = [];
let newCompilersResultOnWorkspace: BuildResult[] = [];
let oldCompilersResult: BuildResult[] = [];
if (componentsAndNewCompilers.length) {
newCompilersResultOnWorkspace = await this.compileWithNewCompilersOnWorkspace(componentsAndNewCompilers);
}
if (idsAndFlows.length) {
newCompilersResultOnCapsule = await this.compileWithNewCompilersOnCapsules(
idsAndFlows,
componentsAndCapsules.map(c => c.consumerComponent)
);
}
if (componentsWithLegacyCompilers.length) {
const components = componentsWithLegacyCompilers.map(c => c.consumerComponent);
oldCompilersResult = await this.compileWithLegacyCompilers(components, noCache, verbose, dontPrintEnvMsg);
}

return [...newCompilersResultOnCapsule, ...oldCompilersResult];
}

async compileOnWorkspace(
componentsIds: string[] | BitId[], // when empty, it compiles all
noCache?: boolean,
verbose?: boolean,
dontPrintEnvMsg?: boolean
): Promise<BuildResult[]> {
const bitIds = getBitIds(componentsIds, this.workspace);
const { components } = await this.workspace.consumer.loadComponents(BitIds.fromArray(bitIds));
const componentsWithLegacyCompilers: ConsumerComponent[] = [];
const componentsAndNewCompilers: ComponentsAndNewCompilers[] = [];
components.forEach(c => {
const compileConfig = this.getCompilerConfig(c);
// if there is no componentDir (e.g. author that added files, not dir), then we can't write the dists
// inside the component dir.
if (compileConfig && compileConfig.compiler && c.componentMap?.getComponentDir()) {
const compilerInstance = this.getCompilerInstance(compileConfig.compiler, c.extensions);
const compilerId = this.getCompilerBitId(compileConfig.compiler, c.extensions);
componentsAndNewCompilers.push({ component: c, compilerInstance, compilerId });
} else {
componentsWithLegacyCompilers.push(c);
}
});
let newCompilersResultOnWorkspace: BuildResult[] = [];
let oldCompilersResult: BuildResult[] = [];
if (componentsAndNewCompilers.length) {
newCompilersResultOnWorkspace = await this.compileWithNewCompilersOnWorkspace(componentsAndNewCompilers);
}
if (componentsWithLegacyCompilers.length) {
oldCompilersResult = await this.compileWithLegacyCompilers(
componentsWithLegacyCompilers,
Expand All @@ -134,7 +153,16 @@ export class Compile {
);
}

return [...newCompilersResultOnWorkspace, ...newCompilersResultOnCapsule, ...oldCompilersResult];
return [...newCompilersResultOnWorkspace, ...oldCompilersResult];
}

private getCompilerConfig(component: ConsumerComponent): Record<string, any> | undefined {
const extensions = component.extensions;
const compileCore = extensions.findCoreExtension('compile');
const compileComponent = extensions.findExtension('compile');
const compileComponentExported = extensions.findExtension('bit.core/compile', true);
const compileExtension = compileCore || compileComponent || compileComponentExported;
return compileExtension?.config;
}

private async compileWithNewCompilersOnWorkspace(
Expand Down Expand Up @@ -328,7 +356,7 @@ cd ${reportResult.result.value.capsule.wrkDir} && node ${SCRIPT_FILENAME} ${task
}

async compileWithLegacyCompilers(
componentsAndCapsules: ComponentAndCapsule[],
components: ConsumerComponent[],
noCache?: boolean,
verbose?: boolean,
dontPrintEnvMsg?: boolean
Expand Down Expand Up @@ -358,11 +386,10 @@ cd ${reportResult.result.value.capsule.wrkDir} && node ${SCRIPT_FILENAME} ${task
// };
// const buildResults = await pMapSeries(componentsAndCapsules, build);

const components = componentsAndCapsules.map(c => c.consumerComponent);
// const components = componentsAndCapsules.map(c => c.consumerComponent);
logger.debugAndAddBreadCrumb('scope.buildMultiple', 'using the legacy build mechanism');
const build = async (component: ConsumerComponent) => {
if (component.compiler) loader.start(`building component - ${component.id}`);
//
await component.build({
scope: this.workspace.consumer.scope,
consumer: this.workspace.consumer,
Expand Down