worker-plugin.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  2. var path = _interopDefault(require('path'));
  3. var ParserHelpers = _interopDefault(require('webpack/lib/ParserHelpers'));
  4. var WORKER_PLUGIN_SYMBOL = _interopDefault(require('./symbol.js'));
  5. /**
  6. * Copyright 2018 Google LLC
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. * use this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. * License for the specific language governing permissions and limitations under
  18. * the License.
  19. */
  20. var NAME = 'WorkerPlugin';
  21. var JS_TYPES = ['auto', 'esm', 'dynamic'];
  22. var workerLoader = path.resolve(__dirname, 'loader.js');
  23. var WorkerPlugin = function WorkerPlugin(options) {
  24. this.options = options || {};
  25. this[WORKER_PLUGIN_SYMBOL] = true;
  26. };
  27. WorkerPlugin.prototype.apply = function apply (compiler) {
  28. var this$1 = this;
  29. compiler.hooks.normalModuleFactory.tap(NAME, function (factory) {
  30. var workerId = 0;
  31. for (var i = 0, list = JS_TYPES; i < list.length; i += 1) {
  32. var type = list[i];
  33. factory.hooks.parser.for(("javascript/" + type)).tap(NAME, function (parser) {
  34. parser.hooks.new.for('Worker').tap(NAME, function (expr) {
  35. var dep = parser.evaluateExpression(expr.arguments[0]);
  36. if (!dep.isString()) {
  37. parser.state.module.warnings.push({
  38. message: 'new Worker() will only be bundled if passed a String.'
  39. });
  40. return false;
  41. }
  42. var optsExpr = expr.arguments[1];
  43. var typeModuleExpr;
  44. var opts;
  45. if (optsExpr) {
  46. opts = {};
  47. for (var i = optsExpr.properties.length; i--;) {
  48. var prop = optsExpr.properties[i];
  49. if (prop.type === 'Property' && !prop.computed && !prop.shorthand && !prop.method) {
  50. opts[prop.key.name] = parser.evaluateExpression(prop.value).string;
  51. if (prop.key.name === 'type') {
  52. typeModuleExpr = prop;
  53. }
  54. }
  55. }
  56. }
  57. if (!opts || opts.type !== 'module') {
  58. parser.state.module.warnings.push({
  59. message: ("new Worker() will only be bundled if passed options that include { type: 'module' }." + (opts ? ("\n Received: new Worker(" + (JSON.stringify(dep.string)) + ", " + (JSON.stringify(opts)) + ")") : ''))
  60. });
  61. return false;
  62. }
  63. var loaderOptions = {
  64. name: opts.name || workerId + ''
  65. };
  66. var req = "require(" + (JSON.stringify(workerLoader + '?' + JSON.stringify(loaderOptions) + '!' + dep.string)) + ")";
  67. var id = "__webpack__worker__" + (workerId++);
  68. ParserHelpers.toConstantDependency(parser, id)(expr.arguments[0]);
  69. if (this$1.options.workerType) {
  70. ParserHelpers.toConstantDependency(parser, JSON.stringify(this$1.options.workerType))(typeModuleExpr.value);
  71. } else if (this$1.options.preserveTypeModule !== true) {
  72. ParserHelpers.toConstantDependency(parser, '')(typeModuleExpr);
  73. }
  74. return ParserHelpers.addParsedVariableToModule(parser, id, req);
  75. });
  76. });
  77. }
  78. });
  79. };
  80. module.exports = WorkerPlugin;
  81. //# sourceMappingURL=worker-plugin.js.map