Skip to content

Add a priority parameter to the Encore.addPlugin() method #177

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

Merged
merged 1 commit into from
Oct 12, 2017

Conversation

Lyrkan
Copy link
Collaborator

@Lyrkan Lyrkan commented Oct 8, 2017

Following the discussion in #165 this PR adds a priority parameter to the Encore.addPlugin() method in order to allow people to choose where their custom plugins will be added in the generated Webpack config (fixes #163).

Examples:

const Encore = require('@symfony/webpack-encore');

// Without setting the priority a plugin will be
// appended to the other plugins.
Encore.addPlugin(new CustomPlugin());
const Encore = require('@symfony/webpack-encore');

// If two plugins need to be sorted relatively to
// eachother (CustomPluginB will be placed before
// CustomPluginA):
Encore
    .addPlugin(new CustomPluginA(), 100);
    .addPlugin(new CustomPluginB(), 50)
;
const Encore = require('@symfony/webpack-encore');
const PluginPriorities = require('@symfony/webpack-encore/lib/plugins/plugin-priorities.js');

// For now all plugins added by Encore have a
// priority of 0 (the same as the default priority for
// custom plugins), but constants are available in
// order to avoid BC breaks:
Encore.addPlugin(new CustomPlugin(), PluginPriorities.UglifyJsPlugin);

@Lyrkan Lyrkan force-pushed the add-plugin-priorities branch from 0cc9f35 to 8ea0d72 Compare October 8, 2017 21:28
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks absolutely perfect! Except for reversing the order... which is actually very minor. Great work!

index.js Outdated
* For example, if a plugin has a priority of 0 and you want to add
* another plugin before it, then:
*
* .addPlugin(new MyWebpackPlugin(), -10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to reverse the sorting: priority means that a higher number wins. So -10 should run after plugins with priority 0. This is also consistent with Symfony's priorities.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, fixed :)

* const PluginPriorities = require('@symfony/webpack-encore/lib/plugins/plugin-priorities.js');
*
* Encore.addPlugin(new MyWebpackPlugin(), PluginPriorities.DefinePlugin);
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great docs, btw. And nice idea to introduce the optional PluginPriorities.

@Lyrkan Lyrkan force-pushed the add-plugin-priorities branch from 8ea0d72 to b7461a1 Compare October 12, 2017 16:55
@weaverryan
Copy link
Member

Awesome, thank you @Lyrkan!

@weaverryan weaverryan merged commit b7461a1 into symfony:master Oct 12, 2017
weaverryan added a commit that referenced this pull request Oct 12, 2017
…d (Lyrkan)

This PR was merged into the master branch.

Discussion
----------

Add a priority parameter to the Encore.addPlugin() method

Following the discussion in #165 this PR adds a `priority` parameter to the `Encore.addPlugin()` method in order to allow people to choose where their custom plugins will be added in the generated Webpack config (fixes #163).

__Examples:__

```js
const Encore = require('@symfony/webpack-encore');

// Without setting the priority a plugin will be
// appended to the other plugins.
Encore.addPlugin(new CustomPlugin());
```

```js
const Encore = require('@symfony/webpack-encore');

// If two plugins need to be sorted relatively to
// eachother (CustomPluginB will be placed before
// CustomPluginA):
Encore
    .addPlugin(new CustomPluginA(), 100);
    .addPlugin(new CustomPluginB(), 50)
;
```

```js
const Encore = require('@symfony/webpack-encore');
const PluginPriorities = require('@symfony/webpack-encore/lib/plugins/plugin-priorities.js');

// For now all plugins added by Encore have a
// priority of 0 (the same as the default priority for
// custom plugins), but constants are available in
// order to avoid BC breaks:
Encore.addPlugin(new CustomPlugin(), PluginPriorities.UglifyJsPlugin);
```

Commits
-------

b7461a1 Add a priority parameter to the Encore.addPlugin() method
@stof
Copy link
Member

stof commented Apr 11, 2018

I think we need to use different priorities for the core plugins, to allow hooking in the middle (for instance, we want to keep the manifest one late, but now, we can either register first or last only)

@Lyrkan
Copy link
Collaborator Author

Lyrkan commented Apr 11, 2018

@stof The reason for that choice is explained in #165 (comment): only the ManifestPlugin seems to require being placed after (nearly) everything else.

I'm not against setting a different priority for each plugin (I even thought about doing so initially) but is it really necessary?

@stof
Copy link
Member

stof commented Apr 11, 2018

but then, at least the manifest plugin should have a different priority

@Lyrkan
Copy link
Collaborator Author

Lyrkan commented Apr 11, 2018

Adding a plugin before the manifest plugin can already be done by using:

Encore.addPlugin(
    new MyPlugin(),
    PluginPriorities.WebpackManifestPlugin + 1
);

If the order doesn't matter for the current other built-in plugins (I may be wrong on that though) it shouldn't be an issue to add the new plugin before all of them.

As en example #221 is going to use the CopyWebpackPlugin which needs to be added before the manifest plugin. In this case it makes sense to use a different priority.

@Lyrkan
Copy link
Collaborator Author

Lyrkan commented Apr 11, 2018

Just realized I said that the manifest plugin needed to be added after everything else in my first comment... I blame tiredness because that's definitely not what I meant.

It must only be placed after some plugins that emit additional assets, which is the case for the copy-webpack-plugin and the webpack-concat-plugin.

@stof
Copy link
Member

stof commented Apr 12, 2018

@Lyrkan but this will add it before all core plugins. So currently, the priority system is only useful to put plugins before or after the core ones (which could be handled with a boolean rather than a priority)

@Lyrkan
Copy link
Collaborator Author

Lyrkan commented Apr 12, 2018

Currently yes since the order doesn't seem to matter for the core plugins we provide right now.

The idea behind that PR is to have something flexible enough in case other plugins (not necessarily core ones) require to be added before/after each others. And if later on we find out that an external plugin needs to be placed between two core plugins we could still modify those two plugins' priority.

#221 will also introduce the copy-webpack-plugin which will not have a priority of 0 since it has to be added before the manifest plugin (but yeah, that's still just an "add this one before anything else" situation).

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

Successfully merging this pull request may close these issues.

Control over plugin order
3 participants