alephjs / aleph-compiler

MIT License
29 stars 1 forks source link

parseDepsWasmFn Handling utf8 characters #2

Closed xuybin closed 2 years ago

xuybin commented 2 years ago

original: // 你好 // support jsx on deno deploy /* @jsxImportSource https://esm.sh/react@18.2.0 / import Editor from "../../components/Header.tsx"; import { Suspense } from "react"; ......

result: // 浣犲ソ // support jsx on deno deploy /* @jsxImportSource https://esm.sh/react@18.2.0 / import Editor from "../"../../components/Header.tsx?ssr&v=l5rwquy9"port { Suspense } from "react"; ......

When removing the first line, the problem disappears File encoding is UTF8 Line feed mode is LF

related code at alephjs/server/transformer.ts const deps = await parseDeps(specifier, source, { importMap: JSON.stringify(importMap), lang, }); depGraph.mark(specifier, { deps, inlineCSS }); if (deps.length) { const s = new MagicString(source); deps.forEach((dep) => { const { specifier, importUrl, loc } = dep; if (!util.isLikelyHttpURL(specifier) && loc) { const sep = importUrl.includes("?") ? "&" : "?"; const version = depGraph.get(specifier)?.version ?? depGraph.globalVersion; const url = "${importUrl}${sep}ssr&v=${version.toString(36)}"; s.overwrite(loc.start - 1, loc.end - 1, url); } }); return new Response(s.toString(), { headers: [["Content-Type", contentType]], });

xuybin commented 2 years ago

import MagicString from "https://esm.sh/magic-string@0.26.2?target=esnext"; const original="你好hello" console.log(original(${original.length}):${original}) const s = new MagicString(original); s.overwrite( 2, 4, abcd, ); const result= s.toString() console.log(result(${result.length}):${result})

deno run .\test.ts original(7):你好hello result(9):你好abcdllo

Does this test prove that it's not a MagicString problem?

xuybin commented 2 years ago

cc @ije