Reputation: 9794
I've written a webpack loader that takes a Chrome Extension manifest file as input and spits out the same file but with the version attribute changed to match that of the project's package.json file.
This is my first webpack loader and while reading the docs I discovered that a webpack loader can be either synchronous or asynchronous. From the docs:
A single result can be returned in sync mode. For multiple results the
this.callback()
must be called. In async modethis.async()
must be called. It returnsthis.callback()
if async mode is allowed. Then the loader must return undefined and call the callback.
My loader only returns a single result, but out of sheer curiosity I wrote it so that it can work both synchronously and asynchronously.
Is there any benefit to this loader having the ability to run asynchronously or should it just be a straightforward synchronous loader?
It should also be noted that this loader won't actually write anything to disk, the result of this loader needs to be passed to the file-loader to actually write it to disk. This also leads me to think that it should probably be synchronous rather than asynchronous.
Upvotes: 2
Views: 541
Reputation: 5226
Q: Is there any benefit to this loader having the ability to run asynchronously?
No. I don't find any benefit on your loader to be async
. I assume that there are no IO operations (File read/write) in your loader.
If you have any such IO operations, it would be better to have your loader as async
.
Usually, async
loaders would be helpful when you have to pass values more than one time from your loader. So in your case, it is better to have your loader as sync
Upvotes: 3