Skip to content

Commit b3982d7

Browse files
committed
fix(compiler): copy transpileComponent output to all dist directories
Compilers using transpileComponent write directly to the filesystem with a single outputDir. After PR #10125 fixed the outputDir to use bit_roots for correct peer resolution, the compiled files were no longer available in node_modules/<pkg>. This fix copies all compiled files from the outputDir to all other dist directories (injected dirs) after transpileComponent completes, ensuring bundlers like esbuild have their output available in all required locations.
1 parent ced90e9 commit b3982d7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

scopes/compilation/compiler/workspace-compiler.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,52 @@ ${this.compileErrors.map(formatError).join('\n')}`);
196196
);
197197
}
198198

199+
/**
200+
* Copy all compiled files from the outputDir (where the compiler wrote) to all other dist directories.
201+
* This is needed for compilers that use transpileComponent and write directly to the filesystem,
202+
* as they only receive one outputDir but files need to be available in all dist locations.
203+
*/
204+
async copyCompiledFilesToOtherDists() {
205+
const distDirs = await this.distDirs();
206+
if (distDirs.length <= 1) return;
207+
208+
// The compiler writes to the outputDir from getComponentPackagePath + distDirName
209+
const outputDir = await this.workspace.getComponentPackagePath(this.component);
210+
const distDirName = this.compilerInstance.getDistDir?.() || DEFAULT_DIST_DIRNAME;
211+
const sourceDistDirAbs = path.join(outputDir, distDirName);
212+
213+
// Check if the dist directory exists (compiler may not have written anything)
214+
if (!(await fs.pathExists(sourceDistDirAbs))) return;
215+
216+
// Find which distDir corresponds to the outputDir and get the others
217+
const outputDirRelative = path.relative(this.workspace.path, outputDir);
218+
const sourceDistDirRelative = path.join(outputDirRelative, distDirName);
219+
const otherDirs = distDirs.filter((dir) => dir !== sourceDistDirRelative);
220+
221+
if (!otherDirs.length) return;
222+
223+
const matches = await globby(`**/*`, {
224+
cwd: sourceDistDirAbs,
225+
onlyFiles: true,
226+
ignore: ['node_modules/**'],
227+
});
228+
if (!matches.length) return;
229+
230+
await Promise.all(
231+
otherDirs.map(async (distDir) => {
232+
const distDirAbs = path.join(this.workspace.path, distDir);
233+
await Promise.all(
234+
matches.map(async (match) => {
235+
const source = path.join(sourceDistDirAbs, match);
236+
const dest = path.join(distDirAbs, match);
237+
await fs.ensureDir(path.dirname(dest));
238+
await fs.copyFile(source, dest);
239+
})
240+
);
241+
})
242+
);
243+
}
244+
199245
private async compileOneFile(
200246
file: AbstractVinyl,
201247
initiator: CompilationInitiator,
@@ -259,6 +305,8 @@ ${this.compileErrors.map(formatError).join('\n')}`);
259305
outputDir: await this.workspace.getComponentPackagePath(this.component),
260306
initiator,
261307
});
308+
// Copy compiled files to all other dist directories (injected dirs)
309+
await this.copyCompiledFilesToOtherDists();
262310
} catch (error: any) {
263311
this.compileErrors.push({ path: this.componentDir, error });
264312
}

0 commit comments

Comments
 (0)