Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. const mime = require('mime');
  3. const createContext = require('./lib/context');
  4. const middleware = require('./lib/middleware');
  5. const reporter = require('./lib/reporter');
  6. const { setFs, toDisk } = require('./lib/fs');
  7. const { getFilenameFromUrl, noop, ready } = require('./lib/util');
  8. const defaults = {
  9. logLevel: 'info',
  10. logTime: false,
  11. logger: null,
  12. mimeTypes: null,
  13. reporter,
  14. stats: {
  15. colors: true,
  16. context: process.cwd()
  17. },
  18. watchOptions: {
  19. aggregateTimeout: 200
  20. },
  21. writeToDisk: false
  22. };
  23. module.exports = function wdm(compiler, opts) {
  24. const options = Object.assign({}, defaults, opts);
  25. if (options.lazy) {
  26. if (typeof options.filename === 'string') {
  27. const filename = options.filename
  28. .replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&') // eslint-disable-line no-useless-escape
  29. .replace(/\\\[[a-z]+\\\]/ig, '.+');
  30. options.filename = new RegExp(`^[/]{0,1}${filename}$`);
  31. }
  32. }
  33. // defining custom MIME type
  34. if (options.mimeTypes) {
  35. mime.define(options.mimeTypes);
  36. }
  37. const context = createContext(compiler, options);
  38. // start watching
  39. if (!options.lazy) {
  40. const watching = compiler.watch(options.watchOptions, (err) => {
  41. if (err) {
  42. context.log.error(err.stack || err);
  43. if (err.details) {
  44. context.log.error(err.details);
  45. }
  46. }
  47. });
  48. context.watching = watching;
  49. } else {
  50. context.state = true;
  51. }
  52. if (options.writeToDisk) {
  53. toDisk(context);
  54. }
  55. setFs(context, compiler);
  56. return Object.assign(middleware(context), {
  57. close(callback) {
  58. callback = callback || noop;
  59. if (context.watching) {
  60. context.watching.close(callback);
  61. } else {
  62. callback();
  63. }
  64. },
  65. context,
  66. fileSystem: context.fs,
  67. getFilenameFromUrl: getFilenameFromUrl.bind(this, context.options.publicPath, context.compiler),
  68. invalidate(callback) {
  69. callback = callback || noop;
  70. if (context.watching) {
  71. ready(context, callback, {});
  72. context.watching.invalidate();
  73. } else {
  74. callback();
  75. }
  76. },
  77. waitUntilValid(callback) {
  78. callback = callback || noop;
  79. ready(context, callback, {});
  80. }
  81. });
  82. };