UNPKG

2.44 kBJavaScriptView Raw
1function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
2
3var path = _interopDefault(require('path'));
4var loaderUtils = _interopDefault(require('loader-utils'));
5var slash = _interopDefault(require('slash'));
6
7/**
8 * Copyright 2018 Google LLC
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
11 * use this file except in compliance with the License. You may obtain a copy of
12 * the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19 * License for the specific language governing permissions and limitations under
20 * the License.
21 */
22var comlinkLoaderSpecificOptions = ['multiple', 'multi', // @todo: remove these
23'singleton'];
24function loader() {}
25
26loader.pitch = function (request) {
27 var options = loaderUtils.getOptions(this) || {};
28 var singleton = options.singleton;
29 var workerLoaderOptions = {};
30
31 for (var i in options) {
32 if (comlinkLoaderSpecificOptions.indexOf(i) === -1) {
33 workerLoaderOptions[i] = options[i];
34 }
35 }
36
37 var workerLoader = "!worker-loader?" + (JSON.stringify(workerLoaderOptions)) + "!" + (slash(path.resolve(__dirname, 'comlink-worker-loader.js')));
38 var remainingRequest = JSON.stringify(workerLoader + '!' + request); // ?singleton mode: export an instance of the worker
39
40 if (singleton === true) {
41 return ("\n module.exports = require('comlink').wrap(require(" + remainingRequest + ")());\n " + (options.module === false ? '' : 'module.exports.__esModule = true;') + "\n ").replace(/\n\s*/g, '');
42 } // ?singleton=false mode: always return a new worker from the factory
43
44
45 if (singleton === false) {
46 return ("\n module.exports = function () {\n return require('comlink').wrap(require(" + remainingRequest + ")());\n };\n ").replace(/\n\s*/g, '');
47 }
48
49 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, '');
50};
51
52module.exports = loader;
53//# sourceMappingURL=comlink-loader.js.map