source: trip-planner-front/node_modules/webpack-dev-server/lib/utils/addEntries.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[6a3a178]1'use strict';
2
3const webpack = require('webpack');
4const createDomain = require('./createDomain');
5
6/**
7 * A Entry, it can be of type string or string[] or Object<string | string[],string>
8 * @typedef {(string[] | string | Object<string | string[],string>)} Entry
9 */
10
11/**
12 * Add entries Method
13 * @param {?Object} config - Webpack config
14 * @param {?Object} options - Dev-Server options
15 * @param {?Object} server
16 * @returns {void}
17 */
18function addEntries(config, options, server) {
19 if (options.inline !== false) {
20 // we're stubbing the app in this method as it's static and doesn't require
21 // a server to be supplied. createDomain requires an app with the
22 // address() signature.
23
24 const app = server || {
25 address() {
26 return { port: options.port };
27 },
28 };
29
30 /** @type {string} */
31 const domain = createDomain(options, app);
32 /** @type {string} */
33 const sockHost = options.sockHost ? `&sockHost=${options.sockHost}` : '';
34 /** @type {string} */
35 const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
36 /** @type {string} */
37 const sockPort = options.sockPort ? `&sockPort=${options.sockPort}` : '';
38 /** @type {string} */
39 const clientEntry = `${require.resolve(
40 '../../client/'
41 )}?${domain}${sockHost}${sockPath}${sockPort}`;
42
43 /** @type {(string[] | string)} */
44 let hotEntry;
45
46 if (options.hotOnly) {
47 hotEntry = require.resolve('webpack/hot/only-dev-server');
48 } else if (options.hot) {
49 hotEntry = require.resolve('webpack/hot/dev-server');
50 }
51 /**
52 * prependEntry Method
53 * @param {Entry} originalEntry
54 * @param {Entry} additionalEntries
55 * @returns {Entry}
56 */
57 const prependEntry = (originalEntry, additionalEntries) => {
58 if (typeof originalEntry === 'function') {
59 return () =>
60 Promise.resolve(originalEntry()).then((entry) =>
61 prependEntry(entry, additionalEntries)
62 );
63 }
64
65 if (typeof originalEntry === 'object' && !Array.isArray(originalEntry)) {
66 /** @type {Object<string,string>} */
67 const clone = {};
68
69 Object.keys(originalEntry).forEach((key) => {
70 // entry[key] should be a string here
71 const entryDescription = originalEntry[key];
72 if (typeof entryDescription === 'object' && entryDescription.import) {
73 clone[key] = Object.assign({}, entryDescription, {
74 import: prependEntry(entryDescription.import, additionalEntries),
75 });
76 } else {
77 clone[key] = prependEntry(entryDescription, additionalEntries);
78 }
79 });
80
81 return clone;
82 }
83
84 // in this case, entry is a string or an array.
85 // make sure that we do not add duplicates.
86 /** @type {Entry} */
87 const entriesClone = additionalEntries.slice(0);
88 [].concat(originalEntry).forEach((newEntry) => {
89 if (!entriesClone.includes(newEntry)) {
90 entriesClone.push(newEntry);
91 }
92 });
93 return entriesClone;
94 };
95
96 /**
97 *
98 * Description of the option for checkInject method
99 * @typedef {Function} checkInjectOptionsParam
100 * @param {Object} _config - compilerConfig
101 * @return {Boolean}
102 */
103
104 /**
105 *
106 * @param {Boolean | checkInjectOptionsParam} option - inject(Hot|Client) it is Boolean | fn => Boolean
107 * @param {Object} _config
108 * @param {Boolean} defaultValue
109 * @return {Boolean}
110 */
111 // eslint-disable-next-line no-shadow
112 const checkInject = (option, _config, defaultValue) => {
113 if (typeof option === 'boolean') return option;
114 if (typeof option === 'function') return option(_config);
115 return defaultValue;
116 };
117
118 // eslint-disable-next-line no-shadow
119 [].concat(config).forEach((config) => {
120 /** @type {Boolean} */
121 const webTarget = [
122 'web',
123 'webworker',
124 'electron-renderer',
125 'node-webkit',
126 undefined, // eslint-disable-line
127 null,
128 ].includes(config.target);
129 /** @type {Entry} */
130 const additionalEntries = checkInject(
131 options.injectClient,
132 config,
133 webTarget
134 )
135 ? [clientEntry]
136 : [];
137
138 if (hotEntry && checkInject(options.injectHot, config, true)) {
139 additionalEntries.push(hotEntry);
140 }
141
142 config.entry = prependEntry(config.entry || './src', additionalEntries);
143
144 if (options.hot || options.hotOnly) {
145 config.plugins = config.plugins || [];
146 if (
147 !config.plugins.find(
148 // Check for the name rather than the constructor reference in case
149 // there are multiple copies of webpack installed
150 (plugin) => plugin.constructor.name === 'HotModuleReplacementPlugin'
151 )
152 ) {
153 config.plugins.push(new webpack.HotModuleReplacementPlugin());
154 }
155 }
156 });
157 }
158}
159
160module.exports = addEntries;
Note: See TracBrowser for help on using the repository browser.