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

Reference webpack.DefinePlugin in the readme #53

Open
fregante opened this issue Jun 22, 2019 · 10 comments
Open

Reference webpack.DefinePlugin in the readme #53

fregante opened this issue Jun 22, 2019 · 10 comments

Comments

@fregante
Copy link

fregante commented Jun 22, 2019

webpack.DefinePlugin offer a native way to run simple replacements, which covers the first example.

I can open a PR that adds a link to this issue as documentation in the readme, and add more examples, including dynamic ones like refined-github/refined-github@130c110

With this loader

module.exports = {
  module: {
    rules: [
      {
        test: /fileInWhichJQueryIsUndefined\.js$/,
        loader: 'string-replace-loader',
        options: {
          search: '$',
          replace: 'window.jQuery',
        }
      }
    ]
  }
}

With DefinePlugin

module.exports = {
  plugins: [
    new webpack.DefinePlugin({
      $: 'window.jQuery'
    })
  ]
}
@fregante fregante changed the title Reference webpack.DefinePlugin in the readme` Reference webpack.DefinePlugin in the readme Jun 22, 2019
@pietia
Copy link

pietia commented Aug 13, 2019

@fregante Isn't that DefinePlugin can operate only on entry points while string-replace-loader can update any file ?

@fregante
Copy link
Author

DefinePlugin will operate on any file in the dependency graph. I think that’s the same for this loader, I don’t think it will actually pick files directly from the file system

@pietia
Copy link

pietia commented Aug 13, 2019

@fregante i would have to double check it but i didn't have luck to make DefinePlugin (search / replace functionality ) work with any other file than entry point.

@fregante
Copy link
Author

In Refined GitHub we replace a variable in sub-dependencies of the entry point:

https://github.com/sindresorhus/refined-github/blob/f8c2fab4cfd8f85f19552a897f1a6df49ea441d9/webpack.config.ts#L108-L110

@pietia
Copy link

pietia commented Aug 14, 2019

Thanks. Unfortunately, our project architecture is much more complicated and the file we would like to update with DefinePlugin is not referenced from entry point .

@fregante
Copy link
Author

fregante commented Aug 14, 2019

our project architecture is much more complicated

Right. As I said in my first paragraph:

webpack.DefinePlugin offer a native way to run simple replacements

What you want to do probably isn't covered by that plugin — which, by the way, works on any JS file; its import position doesn't matter as long as you use it as it's supposed to be used.

@pietia
Copy link

pietia commented Aug 14, 2019

I've refactored my project so that i can use DefinePlugin , so - case closed.

@Va1
Copy link
Owner

Va1 commented Apr 8, 2020

hi folks. the mentioned webpack.DefinePlugin is, first of all, a plugin, while this codebase is a loader. their use-cases seem to overlap, but are not equal. i am not sure of why it should be referenced here in this readme, if it is extensively documented in webpack docs and is, apparently, a standard way of performing replacements and some other things. why?

@fregante
Copy link
Author

fregante commented Apr 8, 2020

Because why use a 3rd party loader when you can do it "natively" and with less config?

string-replace-loader is useful for full string replacements, but 2 examples in the readme are a plain variable replacement that Webpack supports.

If the intent of this module is to help the user, let's direct them to better solutions.

@Va1
Copy link
Owner

Va1 commented Apr 12, 2020

as i mentioned in the previous comment, string-replace-loader is a loader on contrast to DefinePlugin being a plugin. this means, that it can be used for specific files via regex (as loaders are defined with test property) and, what's more important, in specific positions in the loader chain (before loader A does it's job, but over results loader B has produced, if defined between em).

this, i believe, makes both just different tools, use-cases of which intersect but are not equal by any means. which invalidates the "why 3rd party loader" question, in my opinion. examples defined in readme are plain variable replacement ones, but this does not deny the point i wrote above, as well as does not mean that more complex examples should be added to justify its existence (or does it? and does this module lack examples overall?).

what may be a justified thing to do is to reference the DefinePlugin in a form like "yo btw, if what you're doing is a simple & global variable replacement, you may be able to achieve it via DefinePlugin, read more here". but the emphasis on examples seems a bit over the top to me (i might be wrong tho).

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

No branches or pull requests

3 participants