sveltejs / svelte-preprocess

A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
MIT License
1.73k stars 147 forks source link

Svelte and TypeScript and Bun issues #555

Open dhbaird opened 1 year ago

dhbaird commented 1 year ago

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:

EDIT: added: typescript 4.8.2

dhbaird commented 1 year ago

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

dhbaird commented 1 year ago

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

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" } }