Skip to content

Commit

Permalink
feat: Enable the option to overwrite Webpack config by using 'elmapp.…
Browse files Browse the repository at this point in the history
…config.js' configuration file
  • Loading branch information
halfzebra committed Oct 11, 2018
1 parent f456bcb commit 92b5308
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
const explorer = cosmiconfig('elmapp');
const result = explorer.searchSync(appDirectory);
const config = result ? result.config : loadElmJson();
const id = x => x;
const configureWebpack =
typeof config.configureWebpack === 'function' ? config.configureWebpack : id;

// WARNING:
// We support config in elm.json only for legacy reasons.
Expand Down Expand Up @@ -76,5 +79,6 @@ module.exports = {
elm: require.resolve('elm/bin/elm'),
publicUrl: getPublicUrl(config),
servedPath: getServedPath(config),
proxy: config.proxy
proxy: config.proxy,
configureWebpack
};
4 changes: 3 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ function build(previousFileSizes) {
console.log(`Creating an optimized ${process.env.NODE_ENV} build...`);
}

const compiler = webpack(config);
const compiler = webpack(
paths.configureWebpack(config, process.env.NODE_ENV)
);
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
Expand Down
7 changes: 6 additions & 1 deletion scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ choosePort(HOST, DEFAULT_PORT)
const appName = path.basename(path.dirname(paths.elmJson));
const urls = prepareUrls(protocol, HOST, port);
// Create a webpack compiler that is configured with custom messages.
const compiler = createCompiler(webpack, config, appName, urls);
const compiler = createCompiler(
webpack,
paths.configureWebpack(config, process.env.NODE_ENV),
appName,
urls
);
// Load proxy config
const proxyConfig = prepareProxy(paths.proxy, paths.appPublic);
// Serve webpack assets generated by the compiler over a web sever.
Expand Down

2 comments on commit 92b5308

@meleyal
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any documentation on this feature, specifically:

  • where should the elmapp.config.js file go, in the root or in config/?
  • does the config replace or extend the default webpack config?
  • is there an expected format or do you just provide module.exports = {...?

@halfzebra
Copy link
Owner Author

@halfzebra halfzebra commented on 92b5308 Oct 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @meleyal!

Thanks for the interest towards these features.

Unfortunately, they are not yet documented since it's a bit unclear whether the API will stay the same.

elmapp.config.js should be in the root of the project; it should be a normal CommonJS module, which exports a function:

module.exports = {
  configureWebpack: (config, env) => {
    // mutate the config object and return it.
    return config;
  }
}

I hope this helps.

Please feel free to create an issue to raise the awareness, I will see if this can be documented soon.

Please sign in to comment.