Skip to content
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

Allow conditionally enabling the module #59

Open
1 of 3 tasks
DamianGlowala opened this issue Apr 19, 2024 · 5 comments · May be fixed by #62
Open
1 of 3 tasks

Allow conditionally enabling the module #59

DamianGlowala opened this issue Apr 19, 2024 · 5 comments · May be fixed by #62

Comments

@DamianGlowala
Copy link
Contributor

Describe the feature

Currently, there doesn't seem to be a way to conditionally enable the module. This is what I would ideally imagine:

gtag: {
  id: import.meta.env.GOOGLE_ANALYTICS_ID,
  enabled: !!import.meta.env.GOOGLE_ANALYTICS_ID
}

Sadly, enabled is used for a different purpose, which is confusing. Could we consider renaming current enabled option to a more descriptive one, please (e.g. initializationMode: 'auto' | 'manual')? (Yes, this would be a breaking change but worth it in my opinion.)

Additional information

  • Would you be willing to help implement this feature?
  • Can you think of other implementations of this feature?

Final checks

@johannschopplich
Copy link
Owner

Great idea! I've seen enabled being used in other modules. Is this some kind of hidden convention?

@DamianGlowala
Copy link
Contributor Author

From my perspective, it depends on the module. This one sounds like a good one to have the enabled option, as some users might want to enable it conditionally (e.g. based on the presence of the environment variable for the GTAG ID, or their preference on the environment in which the app runs, i.e. dev/stage/prod).

It would be as easy as adding the following snippet at the start of the module setup function:

if (!options.enabled) {
  return
}

@johannschopplich
Copy link
Owner

What I generally dislike about this approach for a enabled option is that it breaks auto-imports... I mean, if the user want's to disable the model altogether, he might as well remove it from his Nuxt config? Tricky one. 😄

But yeah, if other modules provide this option that it might be best to add support for enabled to keep the ecosystem aligned.

@DamianGlowala
Copy link
Contributor Author

DamianGlowala commented Apr 19, 2024

Would you say more about how this approach might break auto-imports? If we return from the module's setup function early, none of the imports/plugins will be added. Let me know if there's something more to consider here I might have overlooked 😄

addImports([

addPlugin({

And, to be very clear, we are talking about disabling the module conditionally, when the condition is some form of a variable.

@DamianGlowala
Copy link
Contributor Author

DamianGlowala commented Apr 19, 2024

I know what you mean now. Yes, the code using whatever this module exports will break when enabled: false. Other alternative would be:

export default defineNuxtConfig({
  modules: [
    'other-module',
    ...(import.meta.env.GOOGLE_ANALYTICS_ID ? ['nuxt-gtag'] : [])
  ]
})

... but, this would mean we would have to add the config with the same condition (and just checked that we lose autocompletion for module config by doing it this way):

export default defineNuxtConfig({
  otherConfig: {},
  ...(import.meta.env.GOOGLE_ANALYTICS_ID ? {
    gtag: {
      id: import.meta.env.GOOGLE_ANALYTICS_ID
    }
  } : {})
})

What do you think?

@DamianGlowala DamianGlowala linked a pull request Apr 19, 2024 that will close this issue
8 tasks
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 a pull request may close this issue.

2 participants