From a95e90a5a8ee66f06c9df5d7d0f3595e7e9f6f93 Mon Sep 17 00:00:00 2001 From: David First Date: Sat, 30 May 2020 00:33:05 -0400 Subject: [PATCH 1/2] improve compile performance by not creating any capsule during development --- src/extensions/compile/compile.cmd.tsx | 3 +- src/extensions/compile/compile.ts | 81 +++++++++++++++++--------- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/extensions/compile/compile.cmd.tsx b/src/extensions/compile/compile.cmd.tsx index 452daa266570..0dbae12a34c8 100644 --- a/src/extensions/compile/compile.cmd.tsx +++ b/src/extensions/compile/compile.cmd.tsx @@ -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
Compile has been completed successfully
; + const output = `${compileResults.length} components have been compiled successfully`; + return
{output}
; } async json([components]: CLIArgs, { verbose, noCache }: Flags) { diff --git a/src/extensions/compile/compile.ts b/src/extensions/compile/compile.ts index d21fb2e6b035..efc124fc0719 100644 --- a/src/extensions/compile/compile.ts +++ b/src/extensions/compile/compile.ts @@ -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'; @@ -84,47 +84,66 @@ export class Compile { dontPrintEnvMsg?: boolean, buildOnCapsules = false ): Promise { + 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 { + 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, @@ -134,7 +153,16 @@ export class Compile { ); } - return [...newCompilersResultOnWorkspace, ...newCompilersResultOnCapsule, ...oldCompilersResult]; + return [...newCompilersResultOnWorkspace, ...oldCompilersResult]; + } + + private getCompilerConfig(component: ConsumerComponent): Record | 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( @@ -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 @@ -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, From 052425b34a82eb46dd97c5aa27adca2f55037b82 Mon Sep 17 00:00:00 2001 From: David First Date: Sat, 30 May 2020 09:09:29 -0400 Subject: [PATCH 2/2] fix e2e-tests --- e2e/harmony/compile.e2e.4.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/e2e/harmony/compile.e2e.4.ts b/e2e/harmony/compile.e2e.4.ts index d9c3a732731e..78c603e36497 100644 --- a/e2e/harmony/compile.e2e.4.ts +++ b/e2e/harmony/compile.e2e.4.ts @@ -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(