nuxt / bridge

🌉 Experience Nuxt 3 features on existing Nuxt 2 projects
MIT License
270 stars 29 forks source link

useNuxtApp is not defined after build #1268

Open codeofsumit opened 1 month ago

codeofsumit commented 1 month ago

Environment

Reproduction

https://github.com/tresorone/tresor-repro

Describe the bug

As part of our nuxt 2 to nuxt 3 migration we ran across an error where useNuxtApp is not defined in our build, while it works fine in dev.

To reproduce:

  1. Clone the reproduction repo
  2. npm i
  3. npm run build
  4. npm start
  5. open localhost:3000 in your browser and open the javascript console

You should see the error useNuxtApp is not defined.

Additional context

No response

Logs

No response

wattanx commented 1 month ago

It can be worked around by explicitly importing.

import { useNuxtApp } from ‘#imports'

https://stackblitz.com/edit/github-hqqmha

codeofsumit commented 1 month ago

@wattanx thanks!

import { useNuxtApp } from ‘#imports'

That seems to fix it so the web app starts. However, tests have a problem with this:

image

Anything special I need to do in tests that test the components that use this import workaround?

wattanx commented 1 month ago

#imports is alias. You can check what alias it is by looking at .nuxt/tsconfig.json.

If you are using vitest, you can set this in resolve.alias. https://vitest.dev/config/#alias

If alias is not used, the following writing style will also work.

import { useNuxtApp } from '@nuxt/bridge/dist/runtime'
codeofsumit commented 1 month ago

@wattanx we are using jest and @vue/test-utils. There is no tsconfig.json in .nuxtunfortunately.

image

I replaced all imports of import { useNuxtApp } from '#imports'; with import { useNuxtApp } from '@nuxt/bridge/dist/runtime' but unfortunately it did not help.

Cannot find module '@nuxt/bridge/dist/runtime' from 'composables/usePortfolioReloadHandler.js

This is what thenuxt bridge folder looks like:

image
codeofsumit commented 1 month ago

@wattanx I also tried it with

import { useNuxtApp } from '@nuxt/bridge/dist/runtime/index.mjs';

Now the error is this:

FAIL helper/index.test.js ● Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Can this be fixed by doing some magic in jest.config.js? I tried transformIgnorePatterns: ['/node_modules/(?!@nuxt/bridge)'], but it didn't help.

wattanx commented 1 month ago

I replaced all imports of import { useNuxtApp } from '#imports'; with import { useNuxtApp } from '@nuxt/bridge/dist/runtime' but unfortunately it did not help.

I can't reproduce it. If you can provide me with a reproduction, I can investigate more. https://stackblitz.com/edit/github-hqqmha-zf5tch

we are using jest and @vue/test-utils.

I also think jest can resolve alias by using moduleNameMapper. https://jestjs.io/docs/configuration#modulenamemapper-objectstring-string--arraystring

codeofsumit commented 1 month ago

@wattanx adding the alias via moduleNameMapper helped!

Unfortunately jest can't deal with ESM - I'm trying to figure this out using babel but no luck yet.

This is the jest config

module.exports = {
  setupFiles: ['./jest.setup.js'],
  testEnvironment: 'jsdom',
  moduleNameMapper: {
    '/assets/json/generated/(.*)': '<rootDir>/tests/__mocks__/$1',
    '^@/(.*)$': '<rootDir>/$1',
    '^~/(.*)$': '<rootDir>/$1',
    '^vue$': 'vue/dist/vue.common.js',
    '^#imports$': '@nuxt/bridge/dist/runtime/index.mjs',
  },
  moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node', 'vue'],
  transform: {
    '\\.m?jsx?$': 'jest-esm-transformer',
    '^.+\\.(js)$': 'babel-jest',
    '.*\\.(vue)$': '@vue/vue2-jest',
    '^.+\\.(css)$': '<rootDir>/cssTransform.js',
  },
  transformIgnorePatterns: ['/node_modules/(?!(swiper|@nuxt/bridge)/)'],
};

And this is the babel config

module.exports = {
  presets: ['@babel/preset-env'],
  plugins: ['@babel/plugin-transform-export-namespace-from'],
};