Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 95 additions & 36 deletions index.js
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() {};
Expand All @@ -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",
Copy link
Member

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)

Copy link
Author

@jharris4 jharris4 Dec 21, 2017

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 to cb and errorCallback to err so I've done that.

" 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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all ending \n in the code snippets and instead result.join('\n') them on line 73

Copy link
Author

Choose a reason for hiding this comment

The 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();

*/
*/
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.5.5",
"author": "Tobias Koppers @sokra",
"description": "bundle loader module for webpack",
"peerDependencies": {
"webpack": "^2.4.0"
},
"dependencies": {
"loader-utils": "^1.0.2"
},
Expand Down