sveltejs / prettier-plugin-svelte

Format your svelte components using prettier.
MIT License
714 stars 95 forks source link

Remove dependency on Buffer #418

Closed curran closed 4 months ago

curran commented 5 months ago

As an enhancement to the browser build introduced in https://github.com/sveltejs/prettier-plugin-svelte/pull/417, this issue is about removing the dependency on the Buffer global, which is present in Node environments but not in browser environments.

Currently, this is how the browser build must be consumed in order to work:

import * as prettierPluginSvelte from 'prettier-plugin-svelte/browser';
import { Buffer } from 'buffer';
globalThis.Buffer = Buffer;

The goal with removing the Buffer dependency is to make it simpler, like this:

import * as prettierPluginSvelte from 'prettier-plugin-svelte/browser';
curran commented 5 months ago

A solution to this problem was already proposed by @dummdidumm in here:

https://github.com/sveltejs/prettier-plugin-svelte/pull/239/files#r696572168

This looks promising!

// base64-string.ts
export const stringToBase64 = (str: string) => typeof btoa !== 'undefined' ? bota : Buffer.from(str).toString('base64');
export const base64ToString = (str: string) => typeof atob !== 'undefined' ? bota : Buffer.from(str, 'base64').toString();
curran commented 5 months ago

There appears to be only two occurrences of Buffer that need to be changed:

https://github.com/search?q=repo%3Asveltejs%2Fprettier-plugin-svelte%20Buffer&type=code

https://github.com/sveltejs/prettier-plugin-svelte/blob/1c4bcfe186d9a23ceeebb791ac7065b57523a51a/src/lib/snipTagContent.ts#L100

https://github.com/sveltejs/prettier-plugin-svelte/blob/1c4bcfe186d9a23ceeebb791ac7065b57523a51a/src/embed.ts#L255