Skip to content

Commit 25d03c1

Browse files
committed
Support passing customized webpack config via graphpack.config.js
1 parent 7c5c1f7 commit 25d03c1

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

bin/graphpack.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ const nodemon = require('nodemon');
33
const path = require('path');
44
const { once } = require('ramda');
55
const webpack = require('webpack');
6-
const webpackConfig = require('../config/webpack.config');
6+
const { loadWebpackConfig } = require('../config');
77

8-
const compiler = webpack(webpackConfig);
9-
10-
const startDevServer = () => {
8+
const startDevServer = ({ compiler, config }) => {
119
const serverPaths = Object.keys(compiler.options.entry).map(entry =>
1210
path.join(compiler.options.output.path, `${entry}.js`),
1311
);
1412
compiler.watch(
15-
webpackConfig.watchOptions,
13+
config.watchOptions,
1614
once((error, stats) => {
1715
if (error || stats.hasErrors()) {
1816
throw Error(error || stats.toJson().errors);
@@ -26,14 +24,25 @@ const startDevServer = () => {
2624
);
2725
};
2826

29-
const createProductionBuild = () => {
27+
const createProductionBuild = ({ compiler }) => {
3028
compiler.run((error, stats) => {
3129
if (error || stats.hasErrors()) {
3230
throw Error(error || stats.toJson().errors);
3331
}
3432
});
3533
};
3634

37-
require('yargs')
38-
.command(['$0', 'dev'], 'Start graphpack dev server', {}, startDevServer)
39-
.command('build', 'Create production build', {}, createProductionBuild).argv;
35+
const startGraphPack = async () => {
36+
const config = await loadWebpackConfig();
37+
const compiler = webpack(config);
38+
39+
require('yargs')
40+
.command(['$0', 'dev'], 'Start graphpack dev server', {}, () =>
41+
startDevServer({ compiler, config }),
42+
)
43+
.command('build', 'Create production build', {}, () =>
44+
createProductionBuild({ compiler }),
45+
).argv;
46+
};
47+
48+
startGraphPack();

config/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const cosmiconfig = require('cosmiconfig');
2+
const defaultConfig = require('./webpack.config');
3+
4+
const explorer = cosmiconfig('graphpack').search();
5+
6+
const loadWebpackConfig = async () => {
7+
const result = await explorer;
8+
const userConfig = result
9+
? typeof result.config === 'function'
10+
? result.config(defaultConfig.mode)
11+
: result.config
12+
: {};
13+
14+
if (typeof userConfig.webpack === 'function') {
15+
return userConfig.webpack(defaultConfig);
16+
}
17+
18+
return defaultConfig;
19+
};
20+
21+
exports.loadWebpackConfig = loadWebpackConfig;

config/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const path = require('path');
33
const webpack = require('webpack');
44
const nodeExternals = require('webpack-node-externals');
55

6+
const IS_DEV = process.env.NODE_ENV !== 'production';
7+
68
module.exports = {
79
entry: {
810
// We take care of setting up the server under ./server.js
@@ -12,7 +14,7 @@ module.exports = {
1214
// its node_modules dependencies. This creates an externals function that
1315
// ignores node_modules when bundling in Webpack.
1416
externals: [nodeExternals({ whitelist: [/^graphpack$/] })],
15-
mode: 'development',
17+
mode: IS_DEV ? 'development' : 'production',
1618
module: {
1719
rules: [
1820
{

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@babel/preset-env": "^7.1.0",
1414
"apollo-server": "2.1.0",
1515
"babel-loader": "^8.0.4",
16+
"cosmiconfig": "^5.0.6",
1617
"friendly-errors-webpack-plugin": "^1.7.0",
1718
"graphql": "^0.13.2",
1819
"graphql-tag": "^2.9.2",

0 commit comments

Comments
 (0)