1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const lodash_1 = require("lodash");
|
5 | const memory_fs_1 = tslib_1.__importDefault(require("memory-fs"));
|
6 | const path_1 = tslib_1.__importDefault(require("path"));
|
7 | const webpack_1 = tslib_1.__importDefault(require("webpack"));
|
8 | const webpack_dev_middleware_1 = tslib_1.__importDefault(require("webpack-dev-middleware"));
|
9 | const webpack_hot_middleware_1 = tslib_1.__importDefault(require("webpack-hot-middleware"));
|
10 | exports.HMR_ENTRY = "webpack-hot-middleware/client";
|
11 | exports.getHmrPluginsByVersion = () => {
|
12 | const version = require("webpack/package.json").version;
|
13 | const majarVersion = String(version).split(".")[0];
|
14 | switch (majarVersion) {
|
15 | case "1":
|
16 | throw new Error("not support webpack@1");
|
17 | case "2":
|
18 | default:
|
19 | return [
|
20 | webpack_1.default.HotModuleReplacementPlugin
|
21 | ];
|
22 | }
|
23 | };
|
24 | const concatHMREntry = (entry) => [exports.HMR_ENTRY].concat(entry);
|
25 | const isOneOfPlugins = (PluginList, plugin) => lodash_1.reduce(PluginList, (result, Plugin) => (result || (plugin instanceof Plugin)), false);
|
26 | exports.patchEntryWithHMR = (entry) => {
|
27 | if (lodash_1.isObject(entry)) {
|
28 | return lodash_1.mapValues(entry, concatHMREntry);
|
29 | }
|
30 | return concatHMREntry(entry);
|
31 | };
|
32 | exports.patchPlugins = (plugins) => {
|
33 | const hmrPlugins = exports.getHmrPluginsByVersion();
|
34 | const cleanedPlugins = lodash_1.dropWhile(plugins, (plugin) => isOneOfPlugins(hmrPlugins, plugin));
|
35 | return lodash_1.concat(cleanedPlugins, lodash_1.map(hmrPlugins, (Plugin) => new Plugin()));
|
36 | };
|
37 | exports.patchWebConfigWithHMR = (webpackConfig) => (Object.assign(Object.assign({}, webpackConfig), { entry: exports.patchEntryWithHMR(webpackConfig.entry), plugins: exports.patchPlugins(webpackConfig.plugins) }));
|
38 | exports.createMiddlewaresForWebpack = (webpackConfig, index, hot = false) => {
|
39 | const patchedWebpackConfig = hot
|
40 | ? exports.patchWebConfigWithHMR(webpackConfig)
|
41 | : webpackConfig;
|
42 | const bundler = webpack_1.default(patchedWebpackConfig);
|
43 | const fs = new memory_fs_1.default();
|
44 | bundler.outputFileSystem = fs;
|
45 | const devMiddleware = webpack_dev_middleware_1.default(bundler, {
|
46 | publicPath: (patchedWebpackConfig.output || {}).publicPath,
|
47 | stats: patchedWebpackConfig.stats || {
|
48 | colors: true,
|
49 | reasons: false,
|
50 | hash: false,
|
51 | version: false,
|
52 | timings: true,
|
53 | chunks: false,
|
54 | chunkModules: false,
|
55 | cached: false
|
56 | }
|
57 | });
|
58 | const devServerMiddlewares = [
|
59 | devMiddleware,
|
60 | ((req, res, next) => {
|
61 | if (req.method === "GET" && req.url === "/") {
|
62 | devMiddleware.waitUntilValid(() => {
|
63 | const indexFile = path_1.default.join(webpackConfig.output.path, index);
|
64 | res.end(fs.readFileSync(indexFile));
|
65 | });
|
66 | }
|
67 | else {
|
68 | next();
|
69 | }
|
70 | })
|
71 | ];
|
72 | if (hot) {
|
73 | return [
|
74 | ...devServerMiddlewares,
|
75 | webpack_hot_middleware_1.default(bundler)
|
76 | ];
|
77 | }
|
78 | return devServerMiddlewares;
|
79 | };
|