Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve webworkify handling of named functions where name is in quotes #5036

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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