npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nuxt-plugin-order

v1.0.0

Published

Enforce a specific order of Nuxt plugins at build time.

Downloads

389

Readme

nuxt-plugin-order

A small build module for Nuxt 3 to enforce a specific order of plugins at buld time.

Setup

Install module

npm install --save nuxt-plugin-order

Configuration

export default defineNuxtConfig({
  modules: ['nuxt-plugin-order'],

  pluginOrder: {
    order: [
      // Match plugins by their file path.
      // Note that only a single plugin may match.
      // The match is performed as "plugin.src.includes(v.pathMatch)".
      { pathMatch: 'pinia.mjs' },

      // Local plugin provided by your app.
      { pathMatch: 'initData.ts' },
    ],
    logSortedPlugins: true,
  },
})

The problem

export default defineNuxtPlugin(() => {
  const user = useUserStore()
  // "getActivePinia()" was called but there was no active Pinia.
})

Because you tried to use Pinia while it was not yet injected.

export default defineNuxtPlugin(() => {
  const { $api } = useNuxtApp()
  await $api.loadSiteData()
  // Cannot read properties of undefined (reading 'loadSiteData')
})

Because you tried to use something provided by a plugin from an installed dependency or one of your own plugins.

A classic example is something like this:

  • Plugin A: Coming from a module that injects a global $api object
  • Plugin B: A local plugin that adds a $api.beforeRequest((request) => { request.headers.token = getUserToken() }) callback
  • Plugin C: A local plugin that calls $api.loadData()

In this case, if plugin C is initialized after plugin B, then the request made by plugin C is missing the beforeRequest callback and its request will not contain the authentication token header. We need an explicit order here.

Currently it's not possible to always enforce a specifc order of plugins. While it's possible to define dependencies and a specific numeric order for custom plugins (using the dependsOn, order or enforce properties in defineNuxtPlugin), this may not always work when using plugins provided from third party modules or layers.

In addition, a small change in a dependency could have a breaking impact on how your app works: If such a module provided plugin changes its order after updating dependencies, it could result in unexpected runtime behaviour.

This module is a small helper to enforce an explicit order of plugins at build time, that is deterministic.

How it works

The module requires that all plugins in your app have exactly one entry in the order configuration, except for:

  • plugins that contain "node_modules/nuxt" in their path
  • plugins that contain "node_modules/@nuxt" in their path
  • the plugin that contains "components.plugin.mjs" in its path

You can still add an order entry for any of these, but in general the order of plugins that are provided by Nuxt itself is correct and very likely not the cause of your problems.

If the module finds a plugin for which it can't determine an exact weight, it will throw an error.

The final weight is determined as such:

  • The initial weight is calculated based on the initial order as determined by Nuxt itself
  • For plugins that are defined in order, their weight is calculated as 1000 + index_inside_order
  • The final weights are then sorted from lowest to highest.

By default, the module will then log the final order of all plugins to the console. This can be disabled by setting logSortedPlugins: false.

Should I use this?

Short answer: Probably not.

Long answer: It depends. If you have complex dependencies of several plugins coming from different packages, layers, etc., then enforcing a strict order at this level might be very helpful and prevent potential bugs. But if you only have local plugins, then you can already enforce a specific order by giving all your plugins a name and then use dependsOn to let Nuxt determine the correct order.