aboutsummaryrefslogtreecommitdiff
path: root/node_modules/webpack-serve/lib/options.js
blob: 867ce255bec842438689aa93fb009be3798325de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const fs = require('fs');
const path = require('path');

const merge = require('lodash/merge');
const weblog = require('webpack-log');

const { load } = require('./config');

const defaults = {
  clipboard: true,
  compiler: null,
  config: {},
  content: [],
  dev: { publicPath: '/' },
  flags: {},
  host: 'localhost',
  hot: {},
  http2: false,
  https: null,
  // https: {
  //   key: fs.readFileSync('...key'),
  //   cert: fs.readFileSync('...cert'),
  //   pfx: ...,
  //   passphrase: ...
  // },
  logLevel: 'info',
  logTime: false,
  open: false,
  // open: { app: <String>, path: <String> }
  port: 8080,
  protocol: 'http',
};

function parse(value) {
  try {
    return JSON.parse(value);
  } catch (e) {
    if (typeof value === 'string') {
      return value;
    }

    process.emitWarning(e);
    return null;
  }
}

function pull(obj, prefix) {
  let result;
  for (const key of Object.keys(obj)) {
    if (key.indexOf(prefix) === 0) {
      if (!result) {
        result = {};
      }
      const name = key.replace(prefix, '').toLowerCase();
      result[name] = obj[key];
    }
  }
  return result;
}

module.exports = (opts) => {
  const flags = opts.flags || {};
  const nodeVersion = parseInt(process.version.substring(1), 10);

  return load(opts).then((configs) => {
    const [first] = configs;
    const options = merge({}, defaults, opts, flags, first.serve);
    const httpsFlags = pull(flags, 'https');
    const openFlags = pull(flags, 'open');

    if (httpsFlags) {
      options.https = httpsFlags;
    }

    if (options.https) {
      const { https } = options;
      for (const property of ['cert', 'key', 'pfx']) {
        const value = https[property];
        const isBuffer = value instanceof Buffer;

        if (value && !isBuffer) {
          // lstat seems to have a hard limit at 260
          if (value.length < 260 && fs.lstatSync(value).isFile()) {
            https[property] = fs.readFileSync(path.resolve(value));
          }
        }
      }

      if (https.pass) {
        https.passphrase = https.pass;
        delete https.pass;
      }
      options.protocol = 'https';
      options.https = https;
    }

    /* istanbul ignore if */
    if (openFlags) {
      if (!openFlags.path) {
        openFlags.path = '/';
      }
      options.open = openFlags;
    }

    if (typeof options.content === 'string') {
      options.content = [options.content];
    }

    if (!options.content || !options.content.length) {
      // if no context was specified in a config, and no --content options was
      // used, then we need to derive the context, and content location, from
      // the compiler.
      if (first.context) {
        options.content = [].concat(first.context);
      }

      options.content.push(process.cwd());
    }

    /* istanbul ignore if */
    if (options.http2 && nodeVersion < 9) {
      throw new TypeError(
        'webpack-serve: The `http2` option can only be used with Node v9 and higher.'
      );
    }

    if (flags.dev && typeof flags.dev === 'string') {
      options.dev = parse(flags.dev);

      if (typeof options.dev !== 'object') {
        throw new TypeError(
          'webpack-serve: The --dev flag must be a string contianing a valid JSON object.'
        );
      }
    }

    if (flags.hot && typeof flags.hot === 'string') {
      options.hot = parse(flags.hot);

      if (typeof options.hot !== 'object') {
        throw new TypeError(
          'webpack-serve: The --hot flag must be a string contianing a valid JSON object.'
        );
      }
    }

    if (flags.hotClient === false) {
      options.hot = false;
    } else {
      /* istanbul ignore if */
      if (flags.hot === false) {
        options.hot = Object.assign({}, options.hot, { hot: false });
      }

      /* istanbul ignore if */
      if (flags.reload === false) {
        options.hot = Object.assign({}, options.hot, { reload: false });
      }
    }

    // because you just know someone is going to do this
    if (options.hot === true) {
      options.hot = defaults.hot;
    }

    if (options.hot) {
      if (options.hot.host) {
        const hotHost = options.hot.host.server || options.hot.host;
        if (hotHost !== options.host) {
          throw new TypeError(
            'webpack-serve: The `hot.host` (or `hot.host.server`) property must match the `host` option.'
          );
        }
      } else {
        options.hot.host = options.host;
      }
    }

    /* istanbul ignore if */
    if (flags.openApp) {
      options.open = Object.assign({}, options.open, {
        app: parse(flags.openApp),
      });
    }

    /* istanbul ignore if */
    if (flags.openPath) {
      options.open = Object.assign({}, options.open, {
        path: flags.openPath,
      });
    }

    if (flags.logLevel) {
      options.logLevel = flags.logLevel;
      options.dev = Object.assign(options.dev, { logLevel: options.logLevel });
      options.hot = Object.assign(options.hot, { logLevel: options.logLevel });
    }

    if (flags.logTime) {
      options.logTime = true;
      options.dev = Object.assign(options.dev, { logTime: true });
      options.hot = Object.assign(options.hot, { logTime: true });
    }

    // initialize the shared log
    weblog({
      name: 'serve',
      id: 'webpack-serve',
      level: options.logLevel,
      timestamp: options.logTime,
    });

    // cleanup - doing this here so as not to mutate the options passed in.
    delete options.config;

    // this isn't part of the webpack options schema, and doesn't need to be
    delete configs[0].serve; // eslint-disable-line no-param-reassign

    return { configs, options };
  });
};

// this is exported so we can get coverage on the function
// not intended to be used externally
module.exports.parse = parse;