Skip to content

Commit

Permalink
Error callback (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
devpreview authored Jun 1, 2018
1 parent b404f02 commit 7ff43f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ You can pass a hash of configuration options to `all-chunks-loaded-webpack-plugi
|Name|Type|Default|Description|
|:--:|:--:|:-----:|:----------|
|**`callback`**|`{String}`|`undefined`|Your callback called after all chunks loaded|
|`chunks`|`{String[]}`|`null`|Allows you to callback called only some chunks loaded|
|`excludeChunks`|`{String[]}`|`null`|Allows you to skip callback called some chunks loaded|
|`errorCallback`|`{String}`|`undefined`|Your callback called if some chunk has loading error|
|`chunks`|`{String[]}`|`undefined`|Allows you to callback called only some chunks loaded|
|`excludeChunks`|`{String[]}`|`undefined`|Allows you to skip callback called some chunks loaded|

Here's an example `webpack` config illustrating how to use these options:

Expand Down
14 changes: 14 additions & 0 deletions src/main/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ export class Callback {
_chunksFiles.push(filename);
this.chunksFiles.set(chunk, _chunksFiles);
}

tag.attributes.onload = tag.attributes.onload ? tag.attributes.onload : '';
tag.attributes.onload += "this.onload=null;allChunksLoadedWebpackPlugin('" + chunk + "', '" + filename + "');";
if (this.options.errorCallback) {
tag.attributes.onerror = tag.attributes.onerror ? tag.attributes.onerror : '';
tag.attributes.onerror += "this.onerror=null;allChunksLoadedWebpackPluginError('" + chunk + "', '" + filename + "');";
}
break;
}
}
Expand All @@ -92,6 +97,15 @@ export class Callback {
'}' +
'}' +
'}';
if (this.options.errorCallback) {
loadedScript += 'var allChunksLoadedWebpackPluginHasError = false;' +
'function allChunksLoadedWebpackPluginError(chunk, file) {' +
'if(!allChunksLoadedWebpackPluginHasError) {' +
'allChunksLoadedWebpackPluginHasError = true;' +
'setTimeout(function(){' + this.options.errorCallback + '},0);' +
'}' +
'}';
}
return loadedScript;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Callback } from "./callback";

export interface AllChunksLoadedWebpackPluginOptions {
callback: string;
errorCallback?: string;
chunks?: string[];
excludeChunks?: string[];
}
Expand Down

0 comments on commit 7ff43f1

Please sign in to comment.