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