1 | const path = require('path')
|
2 | const fs = require('fs')
|
3 |
|
4 | const merge = require('webpack-merge')
|
5 | const HtmlWebpackPlugin = require('html-webpack-plugin')
|
6 |
|
7 | const SERVER_RENDERING = process.env.GNOLL_SERVER_RENDERING
|
8 |
|
9 | const paths = require('./paths')
|
10 |
|
11 | const findConfig = options => {
|
12 | if (options.config) return path.resolve(paths.root, options.config)
|
13 | const rootConfigPath = path.join(paths.root, 'webpack.config.js')
|
14 | if (fs.existsSync(rootConfigPath)) return rootConfigPath
|
15 | return '../config/webpack'
|
16 | }
|
17 |
|
18 | const getTemplate = options => {
|
19 | if (process.env.GNOLL_SERVER_RENDERING) return null
|
20 | if (options.html === false) return null
|
21 | if (typeof options.html === 'string') {
|
22 | return path.resolve(paths.root, options.html)
|
23 | }
|
24 | const defaultTemplate = path.join(paths.root, 'src/index.html')
|
25 | if (fs.existsSync(defaultTemplate)) return defaultTemplate
|
26 | return null
|
27 | }
|
28 |
|
29 | const mergeWithOptions = (config, options) => {
|
30 | const optionsConfig = {}
|
31 | if (options.entry) {
|
32 |
|
33 | optionsConfig.entry = { main: [options.entry] }
|
34 | }
|
35 | if (options.out) {
|
36 | optionsConfig.output = { path: path.resolve(paths.root, options.out) }
|
37 | }
|
38 | const template = getTemplate(options)
|
39 | if (template) optionsConfig.plugins = [new HtmlWebpackPlugin({ template })]
|
40 | return merge(config, optionsConfig)
|
41 | }
|
42 |
|
43 | module.exports = options => {
|
44 | const configPath = findConfig(options)
|
45 |
|
46 |
|
47 | const config = require(configPath)
|
48 | return mergeWithOptions(config, options)
|
49 | }
|