1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = wdm;
|
7 |
|
8 | var _schemaUtils = require("schema-utils");
|
9 |
|
10 | var _mimeTypes = _interopRequireDefault(require("mime-types"));
|
11 |
|
12 | var _middleware = _interopRequireDefault(require("./middleware"));
|
13 |
|
14 | var _setupHooks = _interopRequireDefault(require("./utils/setupHooks"));
|
15 |
|
16 | var _setupWriteToDisk = _interopRequireDefault(require("./utils/setupWriteToDisk"));
|
17 |
|
18 | var _setupOutputFileSystem = _interopRequireDefault(require("./utils/setupOutputFileSystem"));
|
19 |
|
20 | var _ready = _interopRequireDefault(require("./utils/ready"));
|
21 |
|
22 | var _options = _interopRequireDefault(require("./options.json"));
|
23 |
|
24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25 |
|
26 | const noop = () => {};
|
27 |
|
28 | function 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;
|
41 |
|
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 | };
|
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);
|
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 |
|
80 |
|
81 |
|
82 |
|
83 | context.logger.error(error);
|
84 | }
|
85 | });
|
86 | }
|
87 |
|
88 | const instance = (0, _middleware.default)(context);
|
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 |