sveltejs/svelte-preprocess

Svelte and TypeScript and Bun issues

dhbaird opened this issue · 2 comments

The svelte-preprocessor seems to have two issues when used with Bun. This might also be a Bun or other issue; I'm not sure yet :)

  1. The module doesn't import the same way in node vs bun:
// node
const sveltePreprocess = await import('svelte-preprocess');
sveltePreprocess.default({});

// bun
const sveltePreprocess = await import('svelte-preprocess');
sveltePreprocess.default()({});
  1. When used to preprocess some text, the bun version ends up escaping the escapes (UPDATE: this was a Bun issue, fixed in Bun upstream, see comments below in this issue):
// a.js
const { preprocess } = await import("svelte/compiler");
const sveltePreprocess = await import('svelte-preprocess');
(async ({ source }) => {
    const x = await preprocess(
        source,
        [sveltePreprocess.default({})],
        { filename: "<inline>", });
    console.log("x.code =", JSON.stringify(x.code));
})({source: `
<script lang="ts">
let x : int = 1;
</script>
`});

// b.js
const { preprocess } = await import("svelte/compiler");
const sveltePreprocess = await import('svelte-preprocess');
(async ({ source }) => {
    const x = await preprocess(
        source,
        [sveltePreprocess.default()({})],
        { filename: "<inline>", });
    console.log("x.code =", JSON.stringify(x.code));
})({source: `
<script lang="ts">
let x : int = 1;
</script>
`});
node a.js ; bun b.js
// >>>
// x.code = "\n<script lang=\"ts\">let x = 1;\n</script>\n"  <-- expected this
// x.code = "\n<script lang=\"ts\">let x = 1;\\n</script>\n"  <-- but got extra "\"

Versions:

  • node 16.17.0
  • bun 0.2.2
  • svelte-preprocess 4.10.7
  • svelte 3.52.0
  • typescript 4.8.2

EDIT: added: typescript 4.8.2

The extra slash gets inserted in the call to typescript transpileModule().

os.EOL yields different values in node vs bun and seems to be the source of the newline problem:

  • "\n" in node
  • "\\n" in bun

Quick reproducer:

echo 'console.log(JSON.stringify(require("os").EOL));' > oseol.cjs ; node oseol.cjs ; bun oseol.cjs
# >>>
# "\n"
# "\\n"

Quick workaround, set this:
tsconfig.json --> { "compilerOptions": { "newLine": "lf" } }