Skip to content

Commit

Permalink
Wait for close event when writing bundles (#4552)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed May 13, 2020
1 parent fa6946a commit 9f71164
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
9 changes: 7 additions & 2 deletions packages/core/core/src/PackagerRunner.js
Expand Up @@ -526,10 +526,15 @@ function writeFileStream(
? stream.pipe(replaceStream(hashRefToNameHash))
: stream;
let fsStream = fs.createWriteStream(filePath, options);
let fsStreamClosed = new Promise(resolve => {
fsStream.on('close', () => resolve());
});
initialStream
.pipe(fsStream)
// $FlowFixMe
.on('finish', () => resolve(fsStream.bytesWritten))
.on('finish', () =>
// $FlowFixMe
resolve(fsStreamClosed.then(() => fsStream.bytesWritten)),
)
.on('error', reject);
});
}
Expand Down
11 changes: 6 additions & 5 deletions packages/core/fs/src/MemoryFS.js
Expand Up @@ -642,8 +642,9 @@ class WriteStream extends Writable {
filePath: FilePath;
options: ?FileOptions;
buffer: Buffer;

constructor(fs: FileSystem, filePath: FilePath, options: ?FileOptions) {
super();
super({emitClose: true, autoDestroy: true});
this.fs = fs;
this.filePath = filePath;
this.options = options;
Expand All @@ -661,10 +662,10 @@ class WriteStream extends Writable {
}

_final(callback: (error?: Error) => void) {
this.fs.writeFile(this.filePath, this.buffer, this.options).then(
() => callback(),
err => callback(err),
);
this.fs
.writeFile(this.filePath, this.buffer, this.options)
.then(callback)
.catch(callback);
}

get bytesWritten() {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/fs/src/NodeFS.js
Expand Up @@ -43,7 +43,10 @@ export class NodeFS implements FileSystem {

createWriteStream(filePath: string, options: any): WriteStream {
let tmpFilePath = getTempFilePath(filePath);
let stream = fs.createWriteStream(tmpFilePath, options);
let stream = fs.createWriteStream(tmpFilePath, {
...options,
emitClose: true,
});
stream.on('close', () => fs.renameSync(tmpFilePath, filePath));
return stream;
}
Expand Down
Expand Up @@ -22,7 +22,9 @@ export default new Reporter({
Array<NamedBundle>,
> = new DefaultMap(() => []);
for (let bundle of event.bundleGraph.getBundles()) {
bundlesByTarget.get(bundle.target.name).push(bundle);
if (!bundle.isInline) {
bundlesByTarget.get(bundle.target.name).push(bundle);
}
}

let reportsDir = path.join(options.projectRoot, 'parcel-bundle-reports');
Expand Down

0 comments on commit 9f71164

Please sign in to comment.