-
-
Notifications
You must be signed in to change notification settings - Fork 179
feature: Add logic to ensure only 1 UglifyJsPlugin's instance per compiler #72
Comments
Does |
@michael-ciniawsky I think so... but if I recall correctly there is differences in this behavior between Filtering duplicates could break plugins that need to be duplicated, such as class MyPlugin {
constructor() {
this.uniquePerCompiler = true;
}
} ...then, in constructor() {
this.uniquePlugins = new WeakSet();
}
apply(...plugins) {
for(let i = 0; i < plugins.length; i++) {
if (!(plugins[i].uniquePerCompiler === true && this.uniquePlugins.has(plugins[i]))) {
this.uniquePlugins.add(plugins[i]);
plugins[i].apply(this);
}
}
} if you did |
@michael-ciniawsky Also, keep in mind that its adding the compiler to a |
I would go with a property that webpack core interprets. That pushes the problem to the right place. |
@bebraw So, problem with that... It only works if its always the Maybe we can solve this by adding a new method to
pluginUnique(uniqueBy, name, fn) {
const events = this.uniquePluginEvents.get(uniqueBy) || [];
if (events.includes(name)) return;
this.uniquePluginEvents.set(uniqueBy, events.concat(name));
this.plugin(name, fn);
} Or, another idea is to allow it to still be opt-in per plugin, but via helpers that live in
class Compiler {
[...]
constructor() {
this.uniquePlugins = new WeakSet();
}
applyUnique(PluginClass) {
if (this.uniquePlugins.has(PluginClass)) {
return true;
}
this.uniquePlugins.add(PluginClass);
}
[...]
}
// Then, in your plugin:
class MyPlugin {
apply(compiler) {
if (compiler.applyUnique(MyPlugin)) return;
}
} |
@hulkish I'm not sure. It's better to get Tobias' input on that. |
What if I want to use the plugin anyway? Like I have different passes of optimizations and want to apply it after each pass? |
@indutny can you give an example? I know there are many plugins that need to be added multiple times (such as |
Several passes of tree shaking? (I don't use it currently, but it may have uses in the future).
Anyway, I think it should be better to just print warning rather than skipping the step altogether. |
@indutny Wouldn't uglify running once after tree shaking solve this? I would hope that nothing would ever "need" to be uglified more than once. Since, it's such a heavy task. I think adding a warning could be more confusing because - once again, many plugins need to be added multiple times. |
Uh, right... bad example. I'll have to think if I can come up with anything else. All in all, it still feels "unsafe" to disable the plugin if it is used twice. In my opinion, warning is better. |
@indutny True, and I totally agree this should not be applied to all plugins. I think it should be instantiated from the plugin itself. As explained in option #2 here: #72 (comment) |
I guess we could a a unique check to the
This way babli-plugin and uglifyjs-plugin could set Anyway there is a case where you want to apply the uglifyjs plugin multiple times: new UglifyJsPlugin({
include: /file\.js$/,
compress: true
})
new UglifyJsPlugin({
include: /other-file\.js$/,
compress: { /* some special options */ }
}) |
I don't think this can be solved generically. As Tobias has shown above, there is no generic case where, even for Uglify, where we know uniqueness should happen. There are two things we can do:
|
Per that use case @sokra mentioned for using UglifyJsPlugin multiple - I feel like it would be better if we even still forced the unique behavior. And, instead provided a way for them to accomplish the same using during a single pass of the plugin''s event handlers. Maybe something like: new UglifyJsPlugin({
optionsPerAsset: [
{
include: /file\.js$/,
compress: true
},
{
include: /other-file\.js$/,
compress: { /* some special options */ }
},
]
}) |
Point is, there is a lot we can do for individual plugins, and with that in mind, having a generic "unique plugin" system seems overkill for handling just the specific cases, but that's just my opinion, and I'm not stuck on it :-) |
@mikesherov I agree, this could just as easily be totally the responsibility of the plugin. That was my original idea but wondered what u folks think. |
Exactly. I'm just saying what I think ;-) |
Closing this in light of webpack/webpack#5248 (comment) |
@hulkish I think I have an example use-case to @indutny's point of multiple uglify instances: Say I have all my application logic in |
Problem:
Currently, I think it's too easy to accidentally apply 2 instances of UglifyJsPlugin to the same compiler/compilation.
Scenario:
webpack.config.prod.js
and include UglifyJsPlugin.--optimize-minimize/-p
argument with webpack cli, so u make a production build script:Since this webpack cli option auto-adds the UglifyJsPlugin:
Solution
Add a guard to the top of
UglifyJsPlugin
class methodapply
which restricts multiple instances of the plugin from being applied to the same compiler, like so:"But... that's user error"
Yes, it absolutely is. However, I have personally come across the mistake several times. My thinking is... the negative impact this potentially introduces is worth safeguarding against.
The text was updated successfully, but these errors were encountered: