index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = wdm;
  6. var _schemaUtils = require("schema-utils");
  7. var _mimeTypes = _interopRequireDefault(require("mime-types"));
  8. var _middleware = _interopRequireDefault(require("./middleware"));
  9. var _getFilenameFromUrl = _interopRequireDefault(require("./utils/getFilenameFromUrl"));
  10. var _setupHooks = _interopRequireDefault(require("./utils/setupHooks"));
  11. var _setupWriteToDisk = _interopRequireDefault(require("./utils/setupWriteToDisk"));
  12. var _setupOutputFileSystem = _interopRequireDefault(require("./utils/setupOutputFileSystem"));
  13. var _ready = _interopRequireDefault(require("./utils/ready"));
  14. var _options = _interopRequireDefault(require("./options.json"));
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. const noop = () => {};
  17. function wdm(compiler, options = {}) {
  18. (0, _schemaUtils.validate)(_options.default, options, {
  19. name: "Dev Middleware",
  20. baseDataPath: "options"
  21. });
  22. const {
  23. mimeTypes
  24. } = options;
  25. if (mimeTypes) {
  26. const {
  27. types
  28. } = _mimeTypes.default; // mimeTypes from user provided options should take priority
  29. // over existing, known types
  30. _mimeTypes.default.types = { ...types,
  31. ...mimeTypes
  32. };
  33. }
  34. const context = {
  35. state: false,
  36. stats: null,
  37. callbacks: [],
  38. options,
  39. compiler,
  40. watching: null
  41. }; // eslint-disable-next-line no-param-reassign
  42. context.logger = context.compiler.getInfrastructureLogger("webpack-dev-middleware");
  43. (0, _setupHooks.default)(context);
  44. if (options.writeToDisk) {
  45. (0, _setupWriteToDisk.default)(context);
  46. }
  47. (0, _setupOutputFileSystem.default)(context); // Start watching
  48. if (context.compiler.watching) {
  49. context.watching = context.compiler.watching;
  50. } else {
  51. let watchOptions;
  52. if (Array.isArray(context.compiler.compilers)) {
  53. watchOptions = context.compiler.compilers.map(childCompiler => childCompiler.options.watchOptions || {});
  54. } else {
  55. watchOptions = context.compiler.options.watchOptions || {};
  56. }
  57. context.watching = context.compiler.watch(watchOptions, error => {
  58. if (error) {
  59. // TODO: improve that in future
  60. // For example - `writeToDisk` can throw an error and right now it is ends watching.
  61. // We can improve that and keep watching active, but it is require API on webpack side.
  62. // Let's implement that in webpack@5 because it is rare case.
  63. context.logger.error(error);
  64. }
  65. });
  66. }
  67. const instance = (0, _middleware.default)(context); // API
  68. instance.getFilenameFromUrl = url => (0, _getFilenameFromUrl.default)(context, url);
  69. instance.waitUntilValid = (callback = noop) => {
  70. (0, _ready.default)(context, callback);
  71. };
  72. instance.invalidate = (callback = noop) => {
  73. (0, _ready.default)(context, callback);
  74. context.watching.invalidate();
  75. };
  76. instance.close = (callback = noop) => {
  77. context.watching.close(callback);
  78. };
  79. instance.context = context;
  80. return instance;
  81. }