Skip to content

Dynamic entry is broken in webpack-dev-server #1318

@mikegreiling

Description

@mikegreiling

Dynamic entries do not seem to be well supported by webpack-dev-server.

The DynamicEntryPlugin is a poorly documented webpack feature, but the webpack entry option can be defined as a function that returns either an entry object or one that returns a Promise which resolves to an object.

e.g.

// webpack.config.js
export default {
  entry: async () => {
    const entries = {};
    // generate entries
    return entries;
  }
  //...
}

This was sort of addressed with #801 and #802 in which the following lines were added in /lib/util/addDevServerEntrypoints.js:

if (typeof wpOpt.entry === 'function') {
  wpOpt.entry = wpOpt.entry(devClient);
}

However this is incorrect for several reasons:

  1. For one, the entry function is not expected to accept any arguments, so passing devClient into the function is a bit confusing. This won't necessarily add the devClient to the resulting entry object unless it was defined by the user in such a way that it uses that argument. Why not append it to the final entry object yourself so you can be sure it is included?

  2. This also redefines the wpOpt.entry as a static object by running the function, rather than keeping it dynamic by, say, wrapping the function in another closure that can re-run and re-append devClient each time it is invoked.

  3. Async functions are completely broken. Since this code resolves the function, an async wpOpt.entry function would resolve to a Promise object and webpack will throw an error because it does not expect this.

I'd be happy to open a PR to fix this if it would be accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions