Preview fails on some valid module syntax in `.js` files

Abierto
#406 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
58/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
javascript, typescript
Área
frontend, tooling

Línea de trabajo

Start in src/output/moduleCompiler.ts, where processModule handles superclass bindings and re-exports; trace how it parses and rewrites each reproduction. Check the generated modules for the class, namespace re-export, package re-export, and default-export cases in the issue. Done means all listed examples preview their expected values without syntax or runtime errors.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Reproduced on @vue/repl main (9b5bc87), in the preview with SSR off. In every case below, App.vue imports from a.js.

1. A class extending an imported class, unless the class is exported directly

base.js

export class Base {}

App.vue (cases 1a, 1c, 1d)

<script setup>
import { msg } from './a.js'
</script>

<template>
  <h1>{{ msg }}</h1>
</template>

1a. Top-level class that is not exported.

a.js

import { Base } from './base.js'

class Foo extends Base {}

export const msg = new Foo() instanceof Base

Actual: the preview shows Cannot read properties of undefined (reading 'start'), and the console logs:

TypeError: Cannot read properties of undefined (reading 'start')

This is thrown in processModule (src/output/moduleCompiler.ts), on the line that inserts the superclass binding at parentStack[1].start.

Expected: the preview shows true.

1b. export default class extends

App.vue

<script setup>
import Foo from './a.js'
import { Base } from './base.js'

const msg = new Foo() instanceof Base
</script>

<template>
  <h1>{{ msg }}</h1>
</template>

a.js

import { Base } from './base.js'

export default class extends Base {}

Actual: the preview shows Failed to execute 'appendChild' on 'Node': Unexpected token 'const'. The generated module for a.js contains:

__module__.default = const Base = __import_1__.Base;
class extends Base {}

Expected: the preview shows true.

1c. Class inside a function that is not exported.

a.js

import { Base } from './base.js'

function create() {
  class Foo extends Base {}
  return new Foo()
}

export const msg = create() instanceof Base

Actual: the preview shows Failed to execute 'appendChild' on 'Node': Unexpected token 'const'. The generated module for a.js contains:

function create() const Base = __import_1__.Base;
{
  class Foo extends Base {}
  return new Foo()
}

Expected: the preview shows true.

1d. Class inside a block.

a.js

import { Base } from './base.js'

let foo
if (true) {
  class Foo extends Base {}
  foo = new Foo()
}

export const msg = foo instanceof Base

Actual: the preview shows Failed to execute 'appendChild' on 'Node': Unexpected token 'const'. The generated module for a.js contains:

if (true) const Base = __import_1__.Base;
{
  class Foo extends Base {}
  foo = new Foo()
}

Expected: the preview shows true.

2. export * as ns from './…'

App.vue

<script setup>
import { b } from './a.js'
</script>

<template>
  <h1>{{ b.msg }}</h1>
</template>

a.js

export * as b from './b.js'

b.js

export const msg = 'ok'

Actual: the preview shows Cannot read properties of undefined (reading 'name'), and the console logs:

TypeError: Cannot read properties of undefined (reading 'name')

This is thrown in processModule (src/output/moduleCompiler.ts), in the export { … } from './…' branch, where it reads spec.local.name.

Expected: the preview shows ok.

3. Re-export from a package: export { ref } from 'vue'

App.vue

<script setup>
import { ref } from './a.js'

const msg = ref('ok')
</script>

<template>
  <h1>{{ msg }}</h1>
</template>

a.js

export { ref } from 'vue'

Actual: the preview shows ref is not defined, and the console logs:

ReferenceError: ref is not defined

The generated module for a.js is:

const __module__ = __modules__["src/a.js"] = { [Symbol.toStringTag]: "Module" }



__export__(__module__, "ref", () => ref)

Expected: the preview shows ok.

4. export default not written exactly as export default

App.vue (cases 4b, 4c)

<script setup>
import msg from './a.js'
</script>

<template>
  <h1>{{ msg }}</h1>
</template>

4a. Parenthesized named function.

App.vue

<script setup>
import getMsg from './a.js'

const msg = getMsg()
</script>

<template>
  <h1>{{ msg }}</h1>
</template>

a.js

export default (function getMsg() {
  return 'ok'
})

Actual: the preview shows {(intermediate value)} is not a function. The generated module for a.js is:

const __module__ = __modules__["src/a.js"] = { [Symbol.toStringTag]: "Module" }

(function getMsg() {
  return 'ok'
})

__export__(__module__, "default", () => getMsg)

Expected: the preview shows ok.

4b. Two spaces.

a.js

export  default 'ok'

Actual: the preview shows Failed to execute 'appendChild' on 'Node': Unexpected string. The generated module for a.js contains:

__module__.default =t 'ok'

Expected: the preview shows ok.

4c. A comment between export and default.

a.js

export /* x */ default 'ok'

Actual: the preview shows Failed to execute 'appendChild' on 'Node': Unexpected token 'default'. The generated module for a.js contains:

__module__.default = default 'ok'

Expected: the preview shows ok.

Lenguaje dominante
TypeScript
Estrellas
1.1k
Forks
222
Merge medio
2 h 10 min
PR fusionados (30 d)
1

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de vuejs/repl

Todos los issues de vuejs/repl

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.