Fixes #42: Handle cross origin web workers automatically

This commit is contained in:
Alexandru Dima 2019-12-19 12:26:26 +01:00
parent f6223d69e6
commit 802217b877
No known key found for this signature in database
GPG key ID: 6E58D7B045760DA0
3 changed files with 35 additions and 1 deletions

View file

@ -190,7 +190,16 @@ function createLoaderRules(languages: IFeatureDefinition[], features: IFeatureDe
return {
getWorkerUrl: function (moduleId, label) {
var pathPrefix = ${pathPrefix};
return (pathPrefix ? stripTrailingSlash(pathPrefix) + '/' : '') + paths[label];
var result = (pathPrefix ? stripTrailingSlash(pathPrefix) + '/' : '') + paths[label];
if (/^(http:)|(https:)|(file:)/.test(result)) {
var currentUrl = String(window.location);
var currentOrigin = currentUrl.substr(0, currentUrl.length - window.location.hash.length - window.location.search.length - window.location.pathname.length);
if (result.substring(0, currentOrigin.length) !== currentOrigin) {
var js = '/*' + label + '*/importScripts("' + result + '");';
return 'data:text/javascript;charset=utf-8,' + encodeURIComponent(js);
}
}
return result;
}
};
})(${JSON.stringify(workerPaths, null, 2)})`,