Skip to content

Commit

Permalink
Merge pull request redwoodjs#312 from Idered/feat/custom-webpack-config
Browse files Browse the repository at this point in the history
Handle custom webpack config
  • Loading branch information
peterp authored Mar 22, 2020
2 parents cbfaf23 + 809492d commit 8230dc5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/core/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const Dotenv = require('dotenv-webpack')
const { getConfig, getPaths } = require('@redwoodjs/internal')
const { existsSync } = require('fs')
const merge = require('webpack-merge')

const redwoodConfig = getConfig()
const redwoodPaths = getPaths()
Expand Down Expand Up @@ -197,3 +199,18 @@ module.exports = (webpackEnv) => {
},
}
}

module.exports['mergeUserWebpackConfig'] = (mode, baseConfig) => {
const redwoodPaths = getPaths()
const hasCustomConfig = existsSync(redwoodPaths.web.webpack)
if (!hasCustomConfig) {
return baseConfig
}
const userWebpackConfig = require(redwoodPaths.web.webpack)

if (typeof userWebpackConfig === 'function') {
return userWebpackConfig(baseConfig, {mode})
}

return merge(baseConfig, userWebpackConfig)
}
7 changes: 5 additions & 2 deletions packages/core/config/webpack.development.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const merge = require('webpack-merge')
const escapeRegExp = require('lodash.escaperegexp')
const { getConfig } = require('@redwoodjs/internal')

const webpackConfig = require('./webpack.common')

const { mergeUserWebpackConfig } = webpackConfig
const redwoodConfig = getConfig()

module.exports = merge(webpackConfig('development'), {
const baseConfig = merge(webpackConfig('development'), {
devServer: {
// https://webpack.js.org/configuration/dev-server/
hot: true,
Expand Down Expand Up @@ -34,3 +35,5 @@ module.exports = merge(webpackConfig('development'), {
splitChunks: false,
},
})

module.exports = mergeUserWebpackConfig('development', baseConfig)
4 changes: 3 additions & 1 deletion packages/core/config/webpack.production.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const webpackConfig = require('./webpack.common')
const { mergeUserWebpackConfig } = webpackConfig
const baseConfig = webpackConfig('production')

module.exports = webpackConfig('production')
module.exports = mergeUserWebpackConfig('production', baseConfig)
2 changes: 2 additions & 0 deletions packages/internal/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const PATH_WEB_DIR_LAYOUTS = 'web/src/layouts/'
const PATH_WEB_DIR_PAGES = 'web/src/pages/'
const PATH_WEB_DIR_COMPONENTS = 'web/src/components'
const PATH_WEB_DIR_SRC = 'web/src'
const PATH_WEB_DIR_CONFIG = 'web/config/webpack.config.js'

/**
* Search the parent directories for the Redwood configuration file.
Expand Down Expand Up @@ -59,6 +60,7 @@ export const getPaths = (BASE_DIR: string = getBaseDir()): Paths => {
components: path.join(BASE_DIR, PATH_WEB_DIR_COMPONENTS),
layouts: path.join(BASE_DIR, PATH_WEB_DIR_LAYOUTS),
src: path.join(BASE_DIR, PATH_WEB_DIR_SRC),
webpack: path.join(BASE_DIR, PATH_WEB_DIR_CONFIG),
},
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/internal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Paths = {
components: string
layouts: string
src: string
webpack: string
}
api: {
db: string
Expand Down

0 comments on commit 8230dc5

Please sign in to comment.