Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support characters outside of the Latin1 range in Browser #154

Closed
curran opened this issue Jan 30, 2019 · 2 comments
Closed

Support characters outside of the Latin1 range in Browser #154

curran opened this issue Jan 30, 2019 · 2 comments

Comments

@curran
Copy link
Contributor

curran commented Jan 30, 2019

I came across this issue where if the source code passed into magic-string contains characters outside of the Latin1 range, e.g. Cyrillic, and the execution context is a browser (works fine in Node), then an error is thrown:

InvalidCharacterError: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

I did some research and found a solution (in the context of a Rollup use case):

const rollupBundle = await rollup(inputOptions);
const { code, map } = await rollupBundle.generate(outputOptions);

// Monkey patch magic-string internals
// to support characters outside of the Latin1 range, e.g. Cyrillic.
const toString = map.toString.bind(map);
map.toString = () => unescape(encodeURIComponent(toString()));

// Inspired by https://github.com/rollup/rollup/issues/121
const codeWithSourceMap = code + '\n//# sourceMappingURL=' + map.toUrl();

Related reading:

I'd be happy to submit a PR incorporating this into magic-string, if there's interest.

@Rich-Harris
Copy link
Owner

thanks! this was actually a long-standing annoyance in the Svelte REPL; had no idea it would be such an easy fix. I had to go into that file today anyway, to remove a deprecated new Buffer(...) call, so I changed this while I was at it: 2177946

@curran
Copy link
Contributor Author

curran commented Feb 3, 2019

Hooray! Thank you for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants