UNPKG

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