Giter Site home page Giter Site logo

Comments (2)

stackblitz avatar stackblitz commented on August 28, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

from nuxt.js.

JamieCurnow avatar JamieCurnow commented on August 28, 2024

I've done a bit of research here...

So. When there are layers, nuxt._layers becomes an array of the root project, followed by the layers. This can be demonstrated with:

 hooks: {
    ready(nuxt) {
      console.log('ready: ', nuxt.options._layers);
    },

So the first layer is the root, the second layer is an actual layer.

Then when nuxt creates nitro, it uses these layers in the nitro scanDirs options:
https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/core/nitro.ts#L96

The scanDirs in this object are passed into a call to createNitro() here:
https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/core/nitro.ts#L397

At that point they look like this:

[
  '/home/projects/nuxt-framework-cecwxh/server', // root "layer"
  '/home/projects/nuxt-framework-cecwxh/layer/server' // other layers
]

Which can be seen by:

hooks: {
  'nitro:config'(config) {
    console.log(config.scanDirs);
  }
}

These scanDirs get looped over and a /utils/* appended to them here:
https://github.com/unjs/nitro/blob/v2/src/core/config/resolvers/imports.ts#L30

Then they get passed in as imports.dirs to unImport here:
https://github.com/unjs/nitro/blob/v2/src/core/config/resolvers/paths.ts#L67

unImport does priority by order of dirs with the last dir in the array taking more priority - ref:
unjs/unimport#141

I tried to reverse the order of the scan imports by doing:

hooks: {
  'nitro:config'(config) {
    console.log('scanDirs before nitro init', config.scanDirs);
    config.scanDirs?.reverse();
  },
}

However... This doesn't work because the root dir gets unshifted onto the array by nitro anyway and then deduped so it ends up at the start of the array again...

createNitro calls loadOptions() here:
https://github.com/unjs/nitro/blob/v2/src/core/nitro.ts#L24

loadOptions loops over the configResolvers can calls them which includes resolvePathOptions here:
https://github.com/unjs/nitro/blob/v2/src/core/config/loader.ts#L32

resolvePathOptions does an unshift() on scanDirs to add the root to the start of the array here:
https://github.com/unjs/nitro/blob/v2/src/core/config/resolvers/paths.ts#L67

There is no hook between this happening, and nitro calling unImport.init() so you can never end up with the root project imports being after the layer imports, and because of the priority of unImport, that means that the layer imports always override the root project imports.

I'm not sure what the fix for this should be, potentially change the unshift to a push() in nitro? And then reverse the order of _layers in nuxt? Tricky one... But I'm happy to make a PR (or two) with some guidance @danielroe @pi0 ?

from nuxt.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.