loader.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  2. var loaderUtils = _interopDefault(require('loader-utils'));
  3. var SingleEntryPlugin = _interopDefault(require('webpack/lib/SingleEntryPlugin'));
  4. var WebWorkerTemplatePlugin = _interopDefault(require('webpack/lib/webworker/WebWorkerTemplatePlugin'));
  5. var FetchCompileWasmTemplatePlugin = _interopDefault(require('webpack/lib/web/FetchCompileWasmTemplatePlugin'));
  6. var WORKER_PLUGIN_SYMBOL = _interopDefault(require('./symbol.js'));
  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. */
  22. var NAME = 'WorkerPluginLoader';
  23. var hasWarned = false;
  24. function pitch(request) {
  25. this.cacheable(false);
  26. var cb = this.async();
  27. var compilerOptions = this._compiler.options || {};
  28. var pluginOptions = compilerOptions.plugins.find(function (p) { return p[WORKER_PLUGIN_SYMBOL]; }).options;
  29. if (pluginOptions.globalObject == null && !hasWarned && compilerOptions.output && compilerOptions.output.globalObject === 'window') {
  30. hasWarned = true;
  31. console.warn('Warning (worker-plugin): output.globalObject is set to "window". It must be set to "self" to support HMR in Workers.');
  32. }
  33. var options = loaderUtils.getOptions(this) || {};
  34. var chunkFilename = compilerOptions.output.chunkFilename.replace(/\.([a-z]+)$/i, '.worker.$1');
  35. var workerOptions = {
  36. filename: chunkFilename.replace(/\[(?:chunkhash|contenthash)(:\d+(?::\d+)?)?\]/g, '[hash$1]'),
  37. chunkFilename: chunkFilename,
  38. globalObject: pluginOptions.globalObject || 'self'
  39. };
  40. var plugins = (pluginOptions.plugins || []).map(function (plugin) {
  41. if (typeof plugin !== 'string') {
  42. return plugin;
  43. }
  44. var found = compilerOptions.plugins.find(function (p) { return p.constructor.name === plugin; });
  45. if (!found) {
  46. console.warn(("Warning (worker-plugin): Plugin \"" + plugin + "\" is not found."));
  47. }
  48. return found;
  49. });
  50. var workerCompiler = this._compilation.createChildCompiler(NAME, workerOptions, plugins);
  51. workerCompiler.context = this._compiler.context;
  52. new WebWorkerTemplatePlugin(workerOptions).apply(workerCompiler);
  53. new FetchCompileWasmTemplatePlugin({
  54. mangleImports: compilerOptions.optimization.mangleWasmImports
  55. }).apply(workerCompiler);
  56. new SingleEntryPlugin(this.context, request, options.name).apply(workerCompiler);
  57. var subCache = "subcache " + __dirname + " " + request;
  58. workerCompiler.hooks.compilation.tap(NAME, function (compilation) {
  59. if (compilation.cache) {
  60. if (!compilation.cache[subCache]) { compilation.cache[subCache] = {}; }
  61. compilation.cache = compilation.cache[subCache];
  62. }
  63. });
  64. workerCompiler.runAsChild(function (err, entries, compilation) {
  65. if (!err && compilation.errors && compilation.errors.length) {
  66. err = compilation.errors[0];
  67. }
  68. var entry = entries && entries[0] && entries[0].files[0];
  69. if (!err && !entry) { err = Error(("WorkerPlugin: no entry for " + request)); }
  70. if (err) { return cb(err); }
  71. return cb(null, ("module.exports = __webpack_public_path__ + " + (JSON.stringify(entry))));
  72. });
  73. }
  74. var loader = {
  75. pitch: pitch
  76. };
  77. exports.pitch = pitch;
  78. exports.default = loader;
  79. //# sourceMappingURL=loader.js.map