Skip to content

Commit 6cc79dd

Browse files
committed
feat(kit,nuxt,schema): allow modules to specify dependencies (#33063)
1 parent 78153ba commit 6cc79dd

File tree

10 files changed

+539
-91
lines changed

10 files changed

+539
-91
lines changed

docs/2.guide/3.going-further/3.modules.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@ export default defineNuxtModule({
177177
defaults: {},
178178
// Shorthand sugar to register Nuxt hooks
179179
hooks: {},
180+
// Configuration for other modules - this does not ensure the module runs before
181+
// your module, but it allows you to change the other module's configuration before it runs
182+
moduleDependencies: {
183+
'some-module': {
184+
// You can specify a version constraint for the module. If the user has a different
185+
// version installed, Nuxt will throw an error on startup.
186+
version: '>=2',
187+
// By default moduleDependencies will be added to the list of modules to be installed
188+
// by Nuxt unless `optional` is set.
189+
optional: true,
190+
// Any configuration that should override `nuxt.options`.
191+
overrides: {},
192+
// Any configuration that should be set. It will override module defaults but
193+
// will not override any configuration set in `nuxt.options`.
194+
defaults: {}
195+
}
196+
},
180197
// The function holding your module logic, it can be asynchronous
181198
setup(moduleOptions, nuxt) {
182199
// ...

packages/kit/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Module
22
export { defineNuxtModule } from './module/define'
3-
export { getDirectory, installModule, loadNuxtModuleInstance, normalizeModuleTranspilePath } from './module/install'
3+
export { getDirectory, installModule, installModules, loadNuxtModuleInstance, normalizeModuleTranspilePath, resolveModuleWithOptions } from './module/install'
44
export { getNuxtModuleVersion, hasNuxtModule, hasNuxtModuleCompatibility } from './module/compatibility'
55

66
// Loader

packages/kit/src/module/define.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ function _defineNuxtModule<
7878
return Promise.resolve(options)
7979
}
8080

81+
function getModuleDependencies (nuxt: Nuxt = useNuxt()) {
82+
if (typeof module.moduleDependencies === 'function') {
83+
return module.moduleDependencies(nuxt)
84+
}
85+
return module.moduleDependencies
86+
}
87+
8188
// Module format is always a simple function
8289
async function normalizedModule (this: any, inlineOptions: Partial<TOptions>, nuxt: Nuxt): Promise<ModuleSetupReturn> {
8390
nuxt ||= tryUseNuxt() || this.nuxt /* invoked by nuxt 2 */
@@ -145,6 +152,7 @@ function _defineNuxtModule<
145152
// Define getters for options and meta
146153
normalizedModule.getMeta = () => Promise.resolve(module.meta)
147154
normalizedModule.getOptions = getOptions
155+
normalizedModule.getModuleDependencies = getModuleDependencies
148156

149157
normalizedModule.onInstall = module.onInstall
150158
normalizedModule.onUpgrade = module.onUpgrade

0 commit comments

Comments
 (0)