UNPKG

1.73 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const { join } = require('path')
5
6/** Assign default values to any option not specified by consuming applicaiton */
7module.exports = function decorateOptions({ paths = {}, target, ...rest } = {}) {
8 const { NODE_ENV } = process.env
9
10 const flags = {
11 mode: NODE_ENV,
12 storybook: target && target.includes('storybook'),
13 electron: target && target.includes('electron'),
14 development: NODE_ENV !== 'production',
15 production: NODE_ENV === 'production',
16 }
17
18 // Determine default paths for context and src directories which are used to
19 // build other paths, this enables configuring the src dir to a custom path
20 // without having to configure every single other src dependent path
21 const context = paths.context || fs.realpathSync(process.cwd())
22 const src = paths.src || join(context, 'src', flags.electron ? 'renderer' : '')
23
24 const defaults = {
25 chunkHash: flags.production ? '.[chunkhash]' : '',
26 devServer: {},
27 flags,
28 publicPath: '/',
29 sassOptions: {},
30 paths: {
31 context,
32 output: join(context, 'public'),
33 static: join(context, 'static'),
34 appIndex: join(src, 'index.js'),
35 htmlTemplate: join(src, 'index.html'),
36 iconSpriteIncludes: [join(src, 'media/icons')],
37 jsLoaderIncludes: [src],
38 },
39 }
40
41 if (flags.electron) {
42 // In Electron we always load the files from disk because it's always fast
43 // and simplifies the electron loading configs.
44 defaults.chunkHash = ''
45 defaults.paths.output = join(context, 'src/build')
46 }
47
48 // Return package defaults merged with project overrides
49 return {
50 ...defaults,
51 ...rest,
52 paths: {
53 ...defaults.paths,
54 ...paths,
55 },
56 }
57}