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

オープン
#406 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
58/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, typescript
領域
frontend, tooling

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
TypeScript
スター
1.1k
フォーク
222
平均マージ
2時間 10分
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

vuejs/repl のほかの issue

vuejs/repl の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。