0

I have an entry array in my webpack config:

entry: {
    'main': [
        'webpack-hot-middleware/client?path=some-query'
        'my-module/my-file',
    ]

Inside of my code (node_modules/my-module/my-file.js) I attempt to require that initial third party file.

var client = require('webpack-hot-middleware/client');

Because I don't require it with the same querystring, webpack treats it as a separate asset/module, and inlines webpack-hot-middleware/client twice in the output bundle. This means I'm working with a new instance of the code, while I want to access the original instance. I don't have access to the third party code so I need to do it in my own library.

Currently the only solution I have is to duplicate the query string:

entry: {
    'main': [
        'webpack-hot-middleware/client?path=some-query'
        'my-module/my-file?path=some-query',
    ]

And then require it using the __resourceQuery exposed to every Webpack file:

var client = require('webpack-hot-middleware/client' + __resourceQuery);

This requires me to duplicate the query string into my module, which is undesired, especially because my module won't use the querystring params (and might want to use its own, which isn't allowed here).

Andy Ray
  • 30,372
  • 14
  • 101
  • 138

1 Answers1

0

You should be able to make this work with a webpack resolver alias: https://webpack.github.io/docs/configuration.html#resolve-alias

Glenjamin
  • 7,150
  • 6
  • 25
  • 26
  • Hmm..wouldn't that mean the user would have to move their webpack-hot-middleware query string into the alias section? I'd prefer not to make the user change how/where they configure the middleware to use my plugin – Andy Ray Jun 29 '16 at 20:02
  • Ah, I didn't realise this was for a plugin. Perhaps we could add something to the hot middleware client to make the singleton available for client-side plugins. – Glenjamin Jun 30 '16 at 00:01
  • I can do it in a plugin by looking through the entry options, but I have no idea how to inject my file after that http://stackoverflow.com/questions/38111292/how-to-append-a-file-to-a-bundle-in-webpack-or-inject-required-code – Andy Ray Jun 30 '16 at 00:43