#What are the main differences between a plugin and a module and when should they be used?

10 messages · Page 1 of 1 (latest)

potent marten
#

I understand from the documentation that modules are meant to execute asynchronous code at build time, rather than at runtime.

But looking at various modules and plugins in the ecosystem I kinda struggle to understand the reasoning in the choice of implementing functionality as a plugin or a module, as often the functionality still seems runtime oriented or they just inject/define plugins anyway

scenic tangle
#

Both modules and plugins can run asynchronously. The major difference is the latter is executed at runtime only, allowing the former to inject dependencies and shape your app at build time.

#

For example, a module may pull in dependencies, configure auto imports, extending the framework essentially, adding types for DX, so on. Plugins essentially extend the “vue app” aspect, allowing you to add functionality that is executed when the app is loaded in a runtime environment

#

As a consumer, very rarely will you be required to author modules in your app. However, there are some integrations you might need to add to modify the way your project is built if there isn’t a module out there that covers your needs.

potent marten
#

@scenic tangle I am looking into it, because I want to fix some bugs in some modules out there, so I'm trying to have the most comprehensive understanding

#

allowing the former to inject dependencies and shape your app at build time
But what is the point of that? the only purposes I see are either hooking into nuxt's own lifecycle or reducing noise/boilerplate in a project (because adding a module in nuxt.config.ts might create less friction than importing plugins in plugins folder)

scenic tangle
#

Modules can impact the build, and often do. That is the significant factor. Within modules, one can analyse the setup of the app. For example, when integrating a third party library, a module can detect what configuration settings are applied in order to determine how to setup said integration. A module can extend the build context, for example, pushing dependencies for transpilation, adding aliases, extending nitro configuration, adding import sources, hooking into build-only lifecycle hooks, so on. None of which can be accomplished within a plugin.

#

Modules also can and often do add runtime code, such as composables, plugins, so on. But a module is where it can only do so. Think of it as a package in isolation.

bronze walrus
scenic tangle