diff --git a/README.md b/README.md index 99957c0..3da7ff6 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,26 @@ const middleware = await koaWebpack({ config }); app.use(middleware); ``` +### configPath + +Type: `String` + +Allows you to specify the absolute path to the Webpack config file to be used. + +Example: + +```js +const path = require('path'); +const koaWebpack = require('koa-webpack'); + +// The Webpack config file would be at "./client/webpack.config.js". +const middleware = await koaWebpack({ + configPath: path.join(__dirname, 'client', 'webpack.config.js') +}); + +app.use(middleware); +``` + ### devMiddleware Type: `Object` diff --git a/lib/index.js b/lib/index.js index 2fc6514..5ea900b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -37,7 +37,7 @@ module.exports = async (opts) => { if (!compiler) { if (!config) { // eslint-disable-next-line import/no-dynamic-require, global-require - config = require(join(root.path, 'webpack.config.js')); + config = require(options.configPath || join(root.path, 'webpack.config.js')); } compiler = webpack(config); diff --git a/lib/validate.js b/lib/validate.js index 712bac8..42a6a81 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -10,13 +10,14 @@ */ const Joi = require('@hapi/joi'); -const { boolean, object, validate } = Joi.bind(); +const { string, boolean, object, validate } = Joi.bind(); module.exports = { validate(options) { const keys = { compiler: [object().allow(null)], config: [object().allow(null)], + configPath: [string().allow(null)], devMiddleware: [object()], hotClient: [boolean(), object().allow(null)] }; diff --git a/test/fixtures/webpack.config.js b/test/fixtures/webpack.config.js new file mode 100644 index 0000000..aa9b50a --- /dev/null +++ b/test/fixtures/webpack.config.js @@ -0,0 +1,10 @@ +const { resolve } = require('path'); + +module.exports = { + mode: 'development', + entry: [resolve(__dirname, 'input.js')], + output: { + path: __dirname, + filename: 'output.js' + } +}; diff --git a/test/test.js b/test/test.js index 9e96a37..9cab9b3 100644 --- a/test/test.js +++ b/test/test.js @@ -9,16 +9,10 @@ const request = require('supertest'); const webpack = require('webpack'); const koaWebpack = require('../lib'); +const config = require('./fixtures/webpack.config'); const defaults = { - config: { - mode: 'development', - entry: [resolve(__dirname, 'fixtures', 'input.js')], - output: { - path: resolve(__dirname, 'fixtures'), - filename: 'output.js' - } - }, + config, devMiddleware: { publicPath: '/', logLevel: 'silent' @@ -32,7 +26,7 @@ function buildOptions(opts) { const options = merge({}, defaults, opts); return merge(options, { config: null, - compiler: webpack(options.config) + ...opts.configPath ? {} : { compiler: webpack(options.config) } }); } @@ -163,3 +157,15 @@ test('continues on if the file is not part of webpack', async (t) => { return close(server, middleware); }); + +test('uses supplied Webpack configuration file', async (t) => { + const { middleware, req, server } = await setup({ + configPath: resolve(__dirname, 'fixtures', 'webpack.config.js') + }); + + const response = await req.get('/output.js').expect(200); + + t.regex(response.text, /Hello World/); + + return close(server, middleware); +});