pluginName and actions API are side effectful #2045
-
The current plugin API for adding a Here's a rough example of what I mean for creating a new plugin: const useMyPlugin = hooks => {
...
};
useMyPlugin.pluginName = "useMyPlugin" // side effect!!! Side effects like this prevent many build tools from being able to fully tree shake a given module since it becomes very difficult to statically analyze the tree of dependencies. If this code were to be marked side effect free, the setting of
There are some ways around this such as using the function as a statement pattern like so: const useMyPlugin = () => {
const plug = hooks => {
...
}
plug.pluginName = "useMyPlugin" // no longer side effectful
return plug;
}(); While this works, its a bit clunky and probably won't be what folks try first. Once possible alternative would be to change the plugin definition to be simply and object, rather than callable itself. Something like this: const myPlugin = {
pluginName: "myPlugin",
hook: hook => {
...
},
actions: {
action1: "action1",
...
}
} Would you be open to a refactor of the plugin API along these lines? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Interesting. Yes, I think making things as tree-shakable as possible would be a good move. This would clearly be a breaking change, but a good one probably. Let's keep discussing this with the intent to move forward with it when other breaking changes come in to play. Thoughts? |
Beta Was this translation helpful? Give feedback.
-
That definitely makes sense and is a good call! Especially since there is a work around right now |
Beta Was this translation helpful? Give feedback.
-
pls mark answer and then remove again |
Beta Was this translation helpful? Give feedback.
Interesting. Yes, I think making things as tree-shakable as possible would be a good move. This would clearly be a breaking change, but a good one probably. Let's keep discussing this with the intent to move forward with it when other breaking changes come in to play. Thoughts?