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

Uncaught SyntaxError when using Composition API with Pug: Missing type in import statement #12542

Open
Nathan7139 opened this issue Dec 13, 2024 · 1 comment
Labels
has workaround A workaround has been found to avoid the problem 🔩 p2-edge-case

Comments

@Nathan7139
Copy link

Nathan7139 commented Dec 13, 2024

Vue version

3.4.27

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-pu2hgbvs?file=src%2Fcomponents%2FHelloWorldSetupPug.vue&terminal=dev
or
https://codesandbox.io/p/devbox/fast-cookies-75hgkn?file=%2Fsrc%2Fcomponents%2FHelloWorldSetupPug.vue%3A3%2C8&workspaceId=ws_QsAMR8rV8TbYxEA1u1WzFz

Steps to reproduce

  1. The reproduction includes four files: HelloWorld, HelloWorldSetup, HelloWorldPug, and HelloWorldSetupPug.
    • Setup indicates the use of the Composition API.
    • Pug indicates the use of Pug.
  2. Run the reproduction in a browser with pnpm install && pnpm dev.
  3. Only HelloWorldSetupPug triggers the error, while the other files (HelloWorld, HelloWorldSetup, and HelloWorldPug) work fine.
  4. The error appears when using import { MyType } from "../utils/myUtils" in HelloWorldSetupPug.
  5. Adding type to the import statement (import type { MyType } from "../utils/myUtils") resolves the issue.

What is expected?

All files, including HelloWorldSetupPug, should work without requiring type in the import statement if it is consistent with the behavior of the other files.

What is actually happening?

Only HelloWorldSetupPug throws the following error in the browser:

Uncaught SyntaxError: The requested module '/src/utils/myUtils.ts?t=1734061016288' does not provide an export named 'MyType'

This issue is resolved by adding type to the import statement.

System Info

System:
    OS: macOS 14.6.1
    CPU: (8) arm64 Apple M1
    Memory: 65.95 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.2 - ~/.nvm/versions/node/v18.20.2/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v18.20.2/bin/npm
    pnpm: 8.7.6 - /usr/local/bin/pnpm
  Browsers:
    Chrome: 131.0.6778.140
    Safari: 17.6
    Safari Technology Preview: 18.0
  npmPackages:
    vue: ~3.4.27 => 3.4.38

Any additional comments?

  • This issue seems specific to the combination of Pug and the Composition API.
  • The other files (HelloWorld, HelloWorldSetup, and HelloWorldPug) work fine without adding type in the import statement.
  • Is this behavior a bug in the setup or expected based on current configurations?
@edison1105 edison1105 added has workaround A workaround has been found to avoid the problem 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Dec 16, 2024
@edison1105
Copy link
Member

edison1105 commented Dec 24, 2024

let isUsedInTemplate = needTemplateUsageCheck
if (
needTemplateUsageCheck &&
ctx.isTS &&
sfc.template &&
!sfc.template.src &&
!sfc.template.lang
) {
isUsedInTemplate = isImportUsed(local, sfc)
}

if a variable is used in the template, we will return it in non-inline mode. However, if the template's lang is pug, isUsedInTemplate will be true without checking if it is really used in template. This result in MyType being returned as a variable to the template with

const __returned__ = { const, myMethod, ref, getMyType(){return MyType}}

so

import { MyType } from '../utils/myUtils'

is retained.
Currently, this issue can only be resolved by using

import type { MyType } from '../utils/myUtils'

@edison1105 edison1105 added 🔩 p2-edge-case and removed 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround A workaround has been found to avoid the problem 🔩 p2-edge-case
Projects
None yet
Development

No branches or pull requests

2 participants