nuxt/framework

Importing composables from nuxt/app for libraries

targetlucked69 opened this issue · 1 comments

Environment

  • Operating System: Darwin
  • Node Version: v16.17.1
  • Nuxt Version: 3.0.0-rc.12
  • Nitro Version: 0.6.0
  • Package Manager: yarn@1.22.17
  • Builder: vite
  • User Config: build
  • Runtime Modules: -
  • Build Modules: -

Reproduction

yarn build
node .output/server/index.mjs

Describe the bug

For libraries that don't need to be installed as a Nuxt module, but uses composables like useState. Can nuxt be a dev dependency to that library and have it imported from nuxt/app?

Something like

import { useState } from 'nuxt/app'

export async function useAnotherComposable(options) {
  const res = await useState('whatever', () => someOtherFunc(options))

  // do something with res

  return {}
}

Tried it and published a simple library, it works. But when running the built app I get

❯ node .output/server/index.mjs
Listening http://[::]:3000
[nuxt] [request error] [unhandled] [500] Package import specifier "#imports" is not defined in package

I know the library can be a nuxt module instead so it can be auto-importable too, but this composable accepts functions as options and nuxt is not able to parse functions in configs.

Additional context

No response

Logs

No response

Your library will need to be added to build.transpile if it is depending on runtime composables. We'd recommend using #imports rather than nuxt/app, in order to make it clear that it is relying on an alias provided by nuxt.