Skip to content

Commit

Permalink
feat: adding options.configPath (#118)
Browse files Browse the repository at this point in the history
* feat: adding options.root

So that koa-webpack can be further downstream than one hop from
mainModule while still maintaing some sane default functionality.

* docs: add configMap to options documentation

Also rename the option to "configMap" instead of "root".

* test(options): adding basic test for options.configPath
  • Loading branch information
Ian Walter authored and shellscape committed Jan 22, 2020
1 parent 09f9ce3 commit ea12be5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
};
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { resolve } = require('path');

module.exports = {
mode: 'development',
entry: [resolve(__dirname, 'input.js')],
output: {
path: __dirname,
filename: 'output.js'
}
};
24 changes: 15 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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) }
});
}

Expand Down Expand Up @@ -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);
});

0 comments on commit ea12be5

Please sign in to comment.