What are the main differences between a plugin and a module and when should they be used?
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
6 Replies
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.
@cuebit 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 timeBut 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)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.
But, is there a difference with a layer packaged as a simple zip?
The difference in this context is that modules are a low-level construct in the overarching architecture of a project. Layers, by design, are a high-level construct particularly aimed at user-land. Distribution, such as a simple zip, is another topic altogether.
But an interesting one 🙂 lacking of knowledges, we simply pack our layer as a reusable package (internally available only). Retrospectively, was this the way to go or should we have instead choose this other path?