UNPKG

3.61 kBSource Map (JSON)View Raw
1{"version":3,"file":"comlink-loader.js","sources":["../src/index.js"],"sourcesContent":["/**\n * Copyright 2018 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\nimport path from 'path';\nimport loaderUtils from 'loader-utils';\nimport slash from 'slash';\n\nconst comlinkLoaderSpecificOptions = [\n 'multiple', 'multi', // @todo: remove these\n 'singleton'\n];\n\nexport default function loader () { }\n\nloader.pitch = function (request) {\n const options = loaderUtils.getOptions(this) || {};\n const singleton = options.singleton;\n const workerLoaderOptions = {};\n for (let i in options) {\n if (comlinkLoaderSpecificOptions.indexOf(i) === -1) {\n workerLoaderOptions[i] = options[i];\n }\n }\n\n const workerLoader = `!worker-loader?${JSON.stringify(workerLoaderOptions)}!${slash(path.resolve(__dirname, 'comlink-worker-loader.js'))}`;\n\n const remainingRequest = JSON.stringify(workerLoader + '!' + request);\n\n // ?singleton mode: export an instance of the worker\n if (singleton === true) {\n return `\n module.exports = require('comlink').wrap(require(${remainingRequest})());\n ${options.module === false ? '' : 'module.exports.__esModule = true;'}\n `.replace(/\\n\\s*/g, '');\n }\n\n // ?singleton=false mode: always return a new worker from the factory\n if (singleton === false) {\n return `\n module.exports = function () {\n return require('comlink').wrap(require(${remainingRequest})());\n };\n `.replace(/\\n\\s*/g, '');\n }\n\n return `\n var wrap = require('comlink').wrap,\n Worker = require(${remainingRequest}),\n inst;\n module.exports = function f() {\n if (this instanceof f) return wrap(Worker());\n return inst || (inst = wrap(Worker()));\n };\n `.replace(/\\n\\s*/g, '');\n};\n"],"names":["const","comlinkLoaderSpecificOptions","loader","pitch","request","options","loaderUtils","getOptions","singleton","workerLoaderOptions","let","i","indexOf","workerLoader","JSON","stringify","slash","path","resolve","__dirname","remainingRequest","module","replace"],"mappings":";;;;;;AAAA;;;;;;;;;;;;;;;AAgBA,AAIAA,IAAMC,4BAA4B,GAAG,CACnC,UADmC,EACvB,OADuB;AAEnC,WAFmC,CAArC;AAKA,AAAe,SAASC,MAAT,GAAmB;;AAElCA,MAAM,CAACC,KAAP,GAAe,UAAUC,OAAV,EAAmB;MAC1BC,OAAO,GAAGC,WAAW,CAACC,UAAZ,CAAuB,IAAvB,KAAgC,EAAhD;MACMC,SAAS,GAAGH,OAAO,CAACG,SAA1B;MACMC,mBAAmB,GAAG,EAA5B;;OACKC,IAAIC,CAAT,IAAcN,OAAd,EAAuB;QACjBJ,4BAA4B,CAACW,OAA7B,CAAqCD,CAArC,MAA4C,CAAC,CAAjD,EAAoD;MAClDF,mBAAmB,CAACE,CAAD,CAAnB,GAAyBN,OAAO,CAACM,CAAD,CAAhC;;;;MAIEE,YAAY,GAAI,qBAAiBC,IAAI,CAACC,SAAL,CAAeN,mBAAf,EAAoC,UAAGO,KAAK,CAACC,IAAI,CAACC,OAAL,CAAaC,SAAb,EAAwB,0BAAxB,CAAD,EAAnF;MAEMC,gBAAgB,GAAGN,IAAI,CAACC,SAAL,CAAeF,YAAY,GAAG,GAAf,GAAqBT,OAApC,CAAzB,CAZgC;;MAe5BI,SAAS,KAAK,IAAlB,EAAwB;WACd,+DAC6CY,gBAAiB,sBAClEf,OAAO,CAACgB,MAAR,KAAmB,KAAnB,GAA2B,EAA3B,GAAgC,oCAAoC,aACtEC,OAHK,CAGG,QAHH,EAGa,EAHb,CAAP;GAhB8B;;;MAuB5Bd,SAAS,KAAK,KAAlB,EAAyB;WACf,6FAEqCY,gBAAiB,4BAE5DE,OAJK,CAIG,QAJH,EAIa,EAJb,CAAP;;;SAOM,0EAEiBF,gBAAiB,6KAMxCE,OARK,CAQG,QARH,EAQa,EARb,CAAP;CA/BF;;;;"}
\No newline at end of file