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

fix(install) - fix package.json generation during bit install for esm components #9006

Merged
merged 1 commit into from
Jul 8, 2024
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
14 changes: 10 additions & 4 deletions scopes/harmony/aspect-loader/aspect-loader.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,23 +538,29 @@ export class AspectLoaderMain {
return !isEmpty(files);
}

private searchDistFile(rootDir: string, relativePath: string) {
private searchDistFile(rootDir: string, relativePath: string, replaceNotFound = false) {
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;
if (found) return found;
if (!replaceNotFound) return null;
const jsFileName = relativePath.replace(new RegExp(`${fileExtension}$`), `.js`);
const finalPath = join(defaultDistDir, jsFileName);
return finalPath;
}

pluginFileResolver(component: Component, rootDir: string) {
return (relativePath: string) => {
const replaceNotFound = relativePath.endsWith('.ts') || relativePath.endsWith('.tsx');

try {
const compiler = this.getCompiler(component);
if (!compiler) {
const distFile = this.searchDistFile(rootDir, relativePath);
const distFile = this.searchDistFile(rootDir, relativePath, replaceNotFound);
return distFile || join(rootDir, relativePath);
}

Expand All @@ -565,7 +571,7 @@ export class AspectLoaderMain {
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);
const distFile = this.searchDistFile(rootDir, relativePath, replaceNotFound);
return distFile || join(rootDir, relativePath);
}
};
Expand Down
10 changes: 5 additions & 5 deletions scopes/harmony/aspect-loader/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class Plugins {
const aspect = Aspect.create({
id: this.component.id.toString(),
});

aspect.addRuntime({
provider: async () => {
await Promise.all(
Expand All @@ -63,10 +62,10 @@ export class Plugins {
}

async registerPluginWithTryCatch(plugin: Plugin, aspect: Aspect) {
const isModule = isEsmModule(plugin.path);
const module = isModule ? await this.loadModule(plugin.path) : undefined;

try {
const isModule = isEsmModule(plugin.path);
const module = isModule ? await this.loadModule(plugin.path) : undefined;

if (isModule && !module) {
this.logger.consoleFailure(
`failed to load plugin ${plugin.path}, make sure to use 'export default' to expose your plugin`
Expand Down Expand Up @@ -124,7 +123,8 @@ export class Plugins {
: component.filesystem.byRegex(pluginDef.pattern);

return files.map((file) => {
return new Plugin(pluginDef, resolvePath ? resolvePath(file.relative) : file.path);
const resolvedPath = resolvePath ? resolvePath(file.relative) : file.path;
return new Plugin(pluginDef, resolvedPath);
});
});

Expand Down