From 7ff43f1e27aa76e5c521d2b857e0948f2b457a98 Mon Sep 17 00:00:00 2001 From: Alexey Savchuk Date: Sat, 2 Jun 2018 00:46:40 +0300 Subject: [PATCH] Error callback (#4) --- README.md | 5 +++-- src/main/callback.ts | 14 ++++++++++++++ src/main/plugin.ts | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 165fbda..1d7b733 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/main/callback.ts b/src/main/callback.ts index d78df04..c7c51b4 100644 --- a/src/main/callback.ts +++ b/src/main/callback.ts @@ -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; } } @@ -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; } diff --git a/src/main/plugin.ts b/src/main/plugin.ts index 1e4adf8..cbcaa05 100644 --- a/src/main/plugin.ts +++ b/src/main/plugin.ts @@ -3,6 +3,7 @@ import { Callback } from "./callback"; export interface AllChunksLoadedWebpackPluginOptions { callback: string; + errorCallback?: string; chunks?: string[]; excludeChunks?: string[]; }