diff --git a/scopes/harmony/aspect-loader/aspect-loader.main.runtime.ts b/scopes/harmony/aspect-loader/aspect-loader.main.runtime.ts index 3231ec4269eb..8e3f599f3368 100644 --- a/scopes/harmony/aspect-loader/aspect-loader.main.runtime.ts +++ b/scopes/harmony/aspect-loader/aspect-loader.main.runtime.ts @@ -1,4 +1,4 @@ -import { join, resolve } from 'path'; +import { join, resolve, extname } from 'path'; import { NativeCompileCache } from '@teambit/toolbox.performance.v8-cache'; import esmLoader from '@teambit/node.utils.esm-loader'; // import findRoot from 'find-root'; @@ -538,15 +538,36 @@ export class AspectLoaderMain { return !isEmpty(files); } + private searchDistFile(rootDir: string, relativePath: string) { + const defaultDistDir = join(rootDir, 'dist'); + const fileExtension = extname(relativePath); + const fileNames = ['ts', 'js', 'tsx', 'jsx'].map((ext) => + relativePath.replace(new RegExp(`${fileExtension}$`), `.${ext}`) + ); + const defaultDistPath = fileNames.map((fileName) => join(defaultDistDir, fileName)); + const found = defaultDistPath.find((distPath) => existsSync(distPath)); + return found; + } + pluginFileResolver(component: Component, rootDir: string) { return (relativePath: string) => { - const compiler = this.getCompiler(component); - if (!compiler) { - return join(rootDir, relativePath); - } + try { + const compiler = this.getCompiler(component); + if (!compiler) { + const distFile = this.searchDistFile(rootDir, relativePath); + return distFile || join(rootDir, relativePath); + } - const dist = compiler.getDistPathBySrcPath(relativePath); - return join(rootDir, dist); + const dist = compiler.getDistPathBySrcPath(relativePath); + return join(rootDir, dist); + } catch (err) { + // This might happen for example when loading an env from the global scope, and the env of the env / aspect is not a core one + this.logger.info( + `pluginFileResolver: got an error during get compiler for component ${component.id.toString()}, probably the env is not loaded yet ${err}` + ); + const distFile = this.searchDistFile(rootDir, relativePath); + return distFile || join(rootDir, relativePath); + } }; } diff --git a/scopes/scope/scope/scope-aspects-loader.ts b/scopes/scope/scope/scope-aspects-loader.ts index 292cc542c711..bd3b923b2acd 100644 --- a/scopes/scope/scope/scope-aspects-loader.ts +++ b/scopes/scope/scope/scope-aspects-loader.ts @@ -21,6 +21,7 @@ import { Component, ComponentID, LoadAspectsOptions, ResolveAspectsOptions } fro import { Logger } from '@teambit/logger'; import { EnvsMain } from '@teambit/envs'; import { NodeLinker } from '@teambit/dependency-resolver'; +import { BitError } from '@teambit/bit-error'; import { ScopeMain } from './scope.main.runtime'; type ManifestOrAspect = ExtensionManifest | Aspect; @@ -225,15 +226,28 @@ needed-for: ${neededFor || ''}`); } private async compileIfNoDist(capsule: Capsule, component: Component) { - const env = this.envs.getEnv(component); - const compiler: Compiler = env.env.getCompiler(); + let compiler: Compiler | undefined; + try { + const env = this.envs.getEnv(component); + compiler = env.env.getCompiler(); + } catch (err: any) { + this.logger.info( + `compileIfNoDist: failed loading compiler for ${component.id.toString()} in capsule ${capsule.path}, error: ${ + err.message + }` + ); + } const distDir = compiler?.distDir || DEFAULT_DIST_DIRNAME; const distExists = existsSync(join(capsule.path, distDir)); if (distExists) return; + if (!compiler) { + throw new BitError(`unable to compile aspect/env ${component.id.toString()}, no compiler found`); + } const compiledCode = ( await Promise.all( component.filesystem.files.flatMap(async (file) => { + // @ts-ignore - we know it's not null, we have throw error above if yes if (!compiler.isFileSupported(file.path)) { return [ { @@ -242,8 +256,9 @@ needed-for: ${neededFor || ''}`); }, ] as TranspileFileOutputOneFile[]; } - + // @ts-ignore - we know it's not null, we have throw error above if yes if (compiler.transpileFile) { + // @ts-ignore - we know it's not null, we have throw error above if yes return compiler.transpileFile(file.contents.toString('utf8'), { filePath: file.path, componentDir: capsule.path, @@ -257,6 +272,7 @@ needed-for: ${neededFor || ''}`); await Promise.all( compact(compiledCode).map((compiledFile) => { + // @ts-ignore - we know it's not null, we have throw error above if yes const path = compiler.getDistPathBySrcPath(compiledFile.outputPath); return capsule?.outputFile(path, compiledFile.outputText); })