Skip to content

Commit

Permalink
Improve webworkify handling of named functions where name is in quotes (
Browse files Browse the repository at this point in the history
  • Loading branch information
robwalch committed Nov 15, 2022
1 parent 50c6b16 commit 6f76930
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/demux/webworkify-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const webpackBootstrapFunc = function () {// webpackBootstrap
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ var __webpack_require__ = function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
Expand Down Expand Up @@ -82,20 +82,23 @@ const webpackBootstrapFunc = function () {// webpackBootstrap
/******/ var result = __webpack_require__(ENTRY_MODULE)
/******/ return result.default || result
}
webpackBootstrapFunc.toString();

var webpackBootstrapFuncArr = webpackBootstrapFunc.toString().split('ENTRY_MODULE');
var moduleNameReqExp = '[\\.|\\-|\\+|\\w|\/|@]+';
var dependencyRegExp = '\\(\\s*(\/\\*.*?\\*\/)?\\s*.*?(' + moduleNameReqExp + ').*?\\)';

function quoteRegExp(str) {
return (str + '').replace(/[.?*+^$[\]\\(){}|-]/g, '\\$&');
}

function isNumeric(n) {
return !isNaN(1 * n);
}

function getModuleDependencies(sources, module, queueName) {
var retval = {};
retval[queueName] = [];
var fnString = module.toString();
var fnString = module.toString().replace(/^"[^"]+"/,'function');;
var wrapperSignature = fnString.match(/^function\s?\w*\(\w+,\s*\w+,\s*(\w+)\)/) || fnString.match(/^\(\w+,\s*\w+,\s*(\w+)\)\s?\=\s?\>/);
if (!wrapperSignature) return retval;
var webpackRequireName = wrapperSignature[1];
Expand Down Expand Up @@ -129,6 +132,7 @@ function hasValuesInQueues(queues) {
var keys = Object.keys(queues);
return keys.reduce((hasValues, key) => hasValues || queues[key].length > 0, false);
}

function getRequiredModules(sources, moduleId) {
var modulesQueue = {
main: [moduleId]
Expand Down Expand Up @@ -160,11 +164,12 @@ function getRequiredModules(sources, moduleId) {
}
return requiredModules;
}

function getWebpackString(requiredModules, sources, entryModule, key) {
const moduleString = requiredModules[key].map((id) => `"${id}": ${sources[key][id].toString()}`).join(",");
const webpackBootstrapFuncArr = webpackBootstrapFunc.toString().split("ENTRY_MODULE");
const moduleString = requiredModules[key].map((id) => `"${id}": ${sources[key][id].toString().replace(/^"[^"]+"/,'function')}`).join(",");
return `${webpackBootstrapFuncArr[0]}{${moduleString}}${webpackBootstrapFuncArr[1]}"${entryModule}"${webpackBootstrapFuncArr[2]}`;
}

export default function (moduleId, options) {
options = options || {};
var sources = {
Expand All @@ -181,7 +186,7 @@ export default function (moduleId, options) {
sources[module][entryModule] = '(function(module, exports, __webpack_require__) { module.exports = __webpack_require__; })';
src = src + `var ${module} = (${getWebpackString(requiredModules, sources, entryModule, modules)})();\n`;
});
src = src + `((${getWebpackString(requiredModules, sources, moduleId, "main")})())(self);`;
src = src + `new ((${getWebpackString(requiredModules, sources, moduleId, 'main')})())(self);`;
var blob = new window.Blob([src], {
type: 'text/javascript'
});
Expand Down

0 comments on commit 6f76930

Please sign in to comment.