Subscribe on changes!

compiler-sfc parse error with "&&" in vscode extension

avatar
Jul 5th 2021

Version

3.1.4

Reproduction link

http://no.com

Steps to reproduce

I am using @vue/compiler-sfc to parse SFC in VS Code extension, but i found that if i have && in my directives, the parse will fail:

<template>
  <div v-if="list && list.length"></div>
</template>
ReferenceError: document is not defined
at Object.decodeHtmlBrowser [as decodeEntities] (/Users/guang/Documents/my-repo/kiwi/node_modules/@vue/compiler-dom/dist/compiler-dom.esm-bundler.js:32:1)

I hava found that in compiler-dom, it use decodeHtmlBrowser but document is not defined.

Sorry that there is not reproduction because it run on vscode extension

What is expected?

As VS Code extension run in nodejs context, compiler-dom should not use decodeHtmlBrowser

What is actually happening?

compiler-dom use decodeHtmlBrowser to decode html.


How can i make it work in VS Code extension ?

avatar
Jul 5th 2021

Sorry to bother, I have found that I can use alias to let @vue/compiler-doc redirect to @vue/compiler-doc/dist/compiler-dom.cjs

avatar
Mar 30th 2023

I also encountered this problem after a rollup build using @vue/compiler-dom, and I solved it by redefining the decodeEntities option with the default implementation provided by https://github.com/vuejs/core/blob/main/packages/compiler-core/src/parse.ts#L77

    const decodeRE = /&(gt|lt|amp|apos|quot);/g
    const decodeMap = {
        gt: '>',
        lt: '<',
        amp: '&',
        apos: "'",
        quot: '"',
    }

    const componentAst = parse(fileContent, {
        decodeEntities: (rawText) =>
            rawText.replace(
                decodeRE,
                (_, p1: keyof typeof decodeMap) => decodeMap[p1]
            ),
    })

Use directly the baseParse function from @vue/compiler-core could also be a solution.