Skip to content

Commit

Permalink
fix(swc): fix a bug where regenerator-runtime would be marked as exte…
Browse files Browse the repository at this point in the history
…rnal.
  • Loading branch information
wessberg committed Mar 9, 2021
1 parent da13b33 commit 53bda66
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ import {REGENERATOR_SOURCE} from "../constant/regenerator-source";

const swcBug1461String = `var regeneratorRuntime = require("regenerator-runtime");`;

/**
* TODO: Remove this when https://github.com/swc-project/swc/issues/1227 has been resolved
*/
function workAroundSwcBug1227(str: string): string {
const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g;

function escape(code: any) {
let str = code.toString(16);
// Sigh, node 6 doesn't have padStart
// TODO: Remove in Babel 8, when we drop node 6.
while (str.length < 4) str = "0" + str;
return "\\u" + str;
}

function replacer(match: any, backslashes: any, code: any) {
if (backslashes.length % 2 === 0) {
return match;
}

const char = String.fromCodePoint(parseInt(code, 16));
const escaped = backslashes.slice(0, -1) + escape(char.charCodeAt(0));

return char.length === 1 ? escaped : escaped + escape(char.charCodeAt(1));
}

return str.replace(unicodeEscape, replacer);
}

/**
* TODO: Remove this when https://github.com/swc-project/swc/issues/1461 has been resolved
*/
Expand Down Expand Up @@ -91,6 +119,11 @@ export async function build({paths, features, featuresRequested, ecmaVersion, co
ecmaVersion = "es2019";
}

// TODO: Remove this when https://github.com/swc-project/swc/issues/1227 has been resolved
if (code.includes("\\u{")) {
code = workAroundSwcBug1227(code);
}

({code, map} = await transform(code, {
sourceMaps: sourcemap ? "inline" : false,
inputSourceMap: map,
Expand Down
19 changes: 19 additions & 0 deletions test/server/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ test("Will inline regenerator-runtime if required. #1", async t => {
}
});

test("Will correctly escape unicode escape sequences. #1", async t => {
const result = await sendRequest({
http2: config.http2,
tls: false,
userAgent: ie("11"),
method: "GET",
host: config.host,
port: config.port,
path: `${constant.endpoint.polyfill}?features=intl.number-format`,
acceptEncoding: undefined
});

if (!("body" in result)) {
t.false("The API didn't have a body");
} else {
t.false(result.body.toString().includes(`u{1e950}`));
}
});

test("Will correctly parse meta information for SystemJS. #1", async t => {
const polyfillRequest = getPolyfillRequestFromUrl(new URL("?features=systemjs|variant=s", `https://my-polyfill-service.app${constant.endpoint.polyfill}`), chrome(70));
t.true([...polyfillRequest.features].some(({meta, name}) => name === "systemjs" && meta != null && meta.variant === "s"));
Expand Down

0 comments on commit 53bda66

Please sign in to comment.