Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Async configuration via webpack.config.js #2697

Closed
aanno opened this issue Jun 26, 2016 · 4 comments
Closed

Feature request: Async configuration via webpack.config.js #2697

aanno opened this issue Jun 26, 2016 · 4 comments

Comments

@aanno
Copy link

aanno commented Jun 26, 2016

Feature request: Async configuration via webpack.config.js

Webpack version:
1.12.2

Please tell us about your environment:
Linux (OS independent problem)

Current behavior:
Configuration in webpack.config.js must define an object as module.exports.

Expected/desired behavior:
Configuration in webpack.config.js should also support async resolving of module.exports (e.g. by assigning a promise, future and/or callback to module.exports).

What is the motivation / use case for changing the behavior?
I stepped upon the same problem as described in defineplugin-from-async.

In my case I would like to include some git information (branch, last commit, current value of git-describe) as a constant into the bundled javascript code. With help of nodegit, it is relatively simple to retrieve this - but nodegit is an asynchronous node library.

While there is a work-around (by running webpack through the node API, see link above), my feeling is that allowing an async configuration webpack would simplify more advanced use-cases.

@bebraw
Copy link
Contributor

bebraw commented Jun 26, 2016

Yeah. With webpack 2 you get

exports default function(options) {
    return {
        // ...
        devtool: options.dev ? "cheap-module-eval-source-map" : "hidden-source-map"
    };
}

Expanding to

exports default function(options, cb) {
    cb(null, {
        // ...
        devtool: options.dev ? "cheap-module-eval-source-map" : "hidden-source-map"
    });
}

would seem like a good move to me.

@sokra
Copy link
Member

sokra commented Jun 27, 2016

You can return a Promise with webpack@2

@sokra sokra closed this as completed Jun 27, 2016
@mathieumg
Copy link

It also works with Webpack 1. It doesn't work yet with Webpack 1's development server, however. See webpack/webpack-dev-server#698 for that.

@trusktr
Copy link

trusktr commented Mar 23, 2017

For reference, for anyone stumbling here, returning a promise like what @sokra said looks something like this:

const configPromise = new Promise(function(resolve, reject) {

    // ... do async stuff ...

    // then at some point call `resolve()` with the config object:
    resolve({
        entry: { /* ... */ },
        resolve: { /* ... */ },
        // etc
    })
})

module.exports = configPromise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants