index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = loader;
  6. exports.pitch = pitch;
  7. var _options = require('./options.json');
  8. var _options2 = _interopRequireDefault(_options);
  9. var _loaderUtils = require('loader-utils');
  10. var _loaderUtils2 = _interopRequireDefault(_loaderUtils);
  11. var _schemaUtils = require('schema-utils');
  12. var _schemaUtils2 = _interopRequireDefault(_schemaUtils);
  13. var _NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
  14. var _NodeTargetPlugin2 = _interopRequireDefault(_NodeTargetPlugin);
  15. var _SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
  16. var _SingleEntryPlugin2 = _interopRequireDefault(_SingleEntryPlugin);
  17. var _WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin');
  18. var _WebWorkerTemplatePlugin2 = _interopRequireDefault(_WebWorkerTemplatePlugin);
  19. var _workers = require('./workers/');
  20. var _workers2 = _interopRequireDefault(_workers);
  21. var _Error = require('./Error');
  22. var _Error2 = _interopRequireDefault(_Error);
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. /* eslint-disable
  25. import/first,
  26. import/order,
  27. comma-dangle,
  28. linebreak-style,
  29. no-param-reassign,
  30. no-underscore-dangle,
  31. prefer-destructuring
  32. */
  33. function loader() {}
  34. function pitch(request) {
  35. const options = _loaderUtils2.default.getOptions(this) || {};
  36. (0, _schemaUtils2.default)(_options2.default, options, 'Worker Loader');
  37. if (!this.webpack) {
  38. throw new _Error2.default({
  39. name: 'Worker Loader',
  40. message: 'This loader is only usable with webpack'
  41. });
  42. }
  43. this.cacheable(false);
  44. const cb = this.async();
  45. const filename = _loaderUtils2.default.interpolateName(this, options.name || '[hash].worker.js', {
  46. context: options.context || this.rootContext || this.options.context,
  47. regExp: options.regExp
  48. });
  49. const worker = {};
  50. worker.options = {
  51. filename,
  52. chunkFilename: `[id].${filename}`,
  53. namedChunkFilename: null
  54. };
  55. worker.compiler = this._compilation.createChildCompiler('worker', worker.options);
  56. // Tapable.apply is deprecated in tapable@1.0.0-x.
  57. // The plugins should now call apply themselves.
  58. new _WebWorkerTemplatePlugin2.default(worker.options).apply(worker.compiler);
  59. if (this.target !== 'webworker' && this.target !== 'web') {
  60. new _NodeTargetPlugin2.default().apply(worker.compiler);
  61. }
  62. new _SingleEntryPlugin2.default(this.context, `!!${request}`, 'main').apply(worker.compiler);
  63. const subCache = `subcache ${__dirname} ${request}`;
  64. worker.compilation = compilation => {
  65. if (compilation.cache) {
  66. if (!compilation.cache[subCache]) {
  67. compilation.cache[subCache] = {};
  68. }
  69. compilation.cache = compilation.cache[subCache];
  70. }
  71. };
  72. if (worker.compiler.hooks) {
  73. const plugin = { name: 'WorkerLoader' };
  74. worker.compiler.hooks.compilation.tap(plugin, worker.compilation);
  75. } else {
  76. worker.compiler.plugin('compilation', worker.compilation);
  77. }
  78. worker.compiler.runAsChild((err, entries, compilation) => {
  79. if (err) return cb(err);
  80. if (entries[0]) {
  81. worker.file = entries[0].files[0];
  82. worker.factory = (0, _workers2.default)(worker.file, compilation.assets[worker.file].source(), options);
  83. if (options.fallback === false) {
  84. delete this._compilation.assets[worker.file];
  85. }
  86. return cb(null, `module.exports = function() {\n return ${worker.factory};\n};`);
  87. }
  88. return cb(null, null);
  89. });
  90. }