This repository was archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
feat: add require.ensure
error callback
#48
Open
jharris4
wants to merge
5
commits into
webpack-contrib:master
Choose a base branch
from
jharris4:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fcaf12e
Add support for the require.ensure error callback added in webpack 2.4
jharris4 e916e33
remove \n from each item and use join("\n") instead of join("")
jharris4 60f59fb
rename successCallback to cb and errorCallback to err
jharris4 107018b
merge changes from 0.5.6
jharris4 df9f44a
fix error not being passed to error callback when query.lazy is false
jharris4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Tobias Koppers @sokra | ||
*/ | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Tobias Koppers @sokra | ||
Modified version, created by Richard Scarrott @richardscarrott | ||
*/ | ||
|
||
var loaderUtils = require("loader-utils"); | ||
|
||
module.exports = function() {}; | ||
|
@@ -14,54 +16,111 @@ module.exports.pitch = function(remainingRequest) { | |
regExp: query.regExp | ||
}; | ||
var chunkName = loaderUtils.interpolateName(this, query.name, options); | ||
var chunkNameParam = ", " + JSON.stringify(chunkName); | ||
var chunkNameParam = ", " + JSON.stringify(chunkName); | ||
} else { | ||
var chunkNameParam = ''; | ||
} | ||
var result; | ||
if(query.lazy) { | ||
result = [ | ||
"module.exports = function(cb) {\n", | ||
" require.ensure([], function(require) {\n", | ||
" cb(require(", loaderUtils.stringifyRequest(this, "!!" + remainingRequest), "));\n", | ||
"module.exports = function(successCallback, errorCallback) {\n", | ||
" require.ensure([], function() {\n", | ||
" successCallback(require(", loaderUtils.stringifyRequest(this, "!!" + remainingRequest), "));\n", | ||
" }, function() {\n", | ||
" if (errorCallback) errorCallback.apply(this, arguments);\n", | ||
" }" + chunkNameParam + ");\n", | ||
"}"]; | ||
"};"]; | ||
} else { | ||
result = [ | ||
"var cbs = [], \n", | ||
" data;\n", | ||
"module.exports = function(cb) {\n", | ||
" if(cbs) cbs.push(cb);\n", | ||
" else cb(data);\n", | ||
"}\n", | ||
"require.ensure([], function(require) {\n", | ||
" data = require(", loaderUtils.stringifyRequest(this, "!!" + remainingRequest), ");\n", | ||
" var callbacks = cbs;\n", | ||
" cbs = null;\n", | ||
" for(var i = 0, l = callbacks.length; i < l; i++) {\n", | ||
" callbacks[i](data);\n", | ||
"var cbs,\n", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove all ending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! I've made the change. |
||
" data,\n", | ||
" error = false;\n", | ||
"module.exports = function(successCallback, errorCallback) {\n", | ||
" errorCallback = errorCallback || function() {};\n", | ||
" if (data) {\n", | ||
" successCallback(data);\n", | ||
" } else {\n", | ||
" if (error) {\n", | ||
" // Try again.\n", | ||
" requireBundle();\n", | ||
" }\n", | ||
" cbs.push({\n", | ||
" success: successCallback,\n", | ||
" error: errorCallback\n", | ||
" });\n", | ||
" }\n", | ||
"}" + chunkNameParam + ");"]; | ||
"};\n", | ||
"function requireBundle() {\n", | ||
" cbs = [];\n", | ||
" require.ensure([], function() {\n", | ||
" data = require(", loaderUtils.stringifyRequest(this, "!!" + remainingRequest), ");\n", | ||
" for(var i = 0, l = cbs.length; i < l; i++) {\n", | ||
" cbs[i].success(data);\n", | ||
" }\n", | ||
" error = false;\n", | ||
" cbs = null;\n", | ||
" }, function() {\n", | ||
" for(var i = 0, l = cbs.length; i < l; i++) {\n", | ||
" cbs[i].error();\n", | ||
" }\n", | ||
" error = true;\n", | ||
" cbs = null;\n", | ||
" }" + chunkNameParam + ");\n", | ||
"}\n", | ||
"requireBundle();" | ||
]; | ||
} | ||
return result.join(""); | ||
} | ||
}; | ||
|
||
/* | ||
Output format: | ||
|
||
var cbs = [], | ||
data; | ||
module.exports = function(cb) { | ||
if(cbs) cbs.push(cb); | ||
else cb(data); | ||
} | ||
require.ensure([], function(require) { | ||
data = require("xxx"); | ||
var callbacks = cbs; | ||
cbs = null; | ||
for(var i = 0, l = callbacks.length; i < l; i++) { | ||
callbacks[i](data); | ||
// lazy | ||
module.exports = function(successCallback, errorCallback) { | ||
require.ensure([], function() {\n", | ||
successCallback(require("xxx")); | ||
}, function() { | ||
if (errorCallback) errorCallback.apply(this, arguments); | ||
}, 'name'); | ||
}; | ||
|
||
// non-lazy...kind of dupes the __webpack_require__.e callback handling a little... | ||
var cbs, | ||
data, | ||
error = false; | ||
module.exports = function(successCallback, errorCallback) { | ||
errorCallback = errorCallback || function() {}; | ||
if (data) { | ||
successCallback(data); | ||
} else { | ||
if (error) { | ||
// Try again. | ||
requireBundle(); | ||
} | ||
cbs.push({ | ||
success: successCallback, | ||
error: errorCallback | ||
}); | ||
} | ||
}); | ||
}; | ||
function requireBundle() { | ||
cbs = []; | ||
require.ensure([], function(require) { | ||
data = require("xxx"); | ||
for(var i = 0, l = cbs.length; i < l; i++) { | ||
cbs[i].success(data); | ||
} | ||
error = false; | ||
cbs = null; | ||
}, function() { | ||
for(var i = 0, l = cbs.length; i < l; i++) { | ||
cbs[i].error(); | ||
} | ||
error = true; | ||
cbs = null; | ||
}); | ||
} | ||
requireBundle(); | ||
|
||
*/ | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function (successCallback, errorCallback)
=>function(cb, err)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you were suggesting rename
successCallback
tocb
anderrorCallback
toerr
so I've done that.