Skip to content

Commit

Permalink
fix: correctly work around swc bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Sep 23, 2021
1 parent d749b96 commit 4e695bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {transform} from "@swc/core";
import {REGENERATOR_SOURCE, REGENERATOR_SOURCE_MINIFIED} from "../constant/regenerator-source";
import {generateRandomHash} from "../api/util";

const swcBug1461Match = /var regeneratorRuntime\d?\s*=\s*require\(["'`]regenerator-runtime["'`]\);/;
const swcBug1461MatchA = /var regeneratorRuntime\d?\s*=\s*require\(["'`]regenerator-runtime["'`]\);/;
const swcBug1461MatchB = /import\s+regeneratorRuntime\d?\s+from\s*["'`]regenerator-runtime["'`];/;
const swcBug1461MatchReference = /regeneratorRuntime\d\./g;
const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g;

Expand Down Expand Up @@ -48,7 +49,11 @@ function workAroundSwcBug1227(str: string): string {
* TODO: Remove this when https://github.com/swc-project/swc/issues/1461 has been resolved
*/
function workAroundSwcBug1461(str: string, minify = false): string {
return str.replace(swcBug1461Match, minify ? REGENERATOR_SOURCE_MINIFIED : REGENERATOR_SOURCE).replace(swcBug1461MatchReference, "regeneratorRuntime.");
return str
.replace(swcBug1461MatchA, minify ? REGENERATOR_SOURCE_MINIFIED : REGENERATOR_SOURCE)
.replace(swcBug1461MatchReference, "regeneratorRuntime.")
.replace(swcBug1461MatchB, minify ? REGENERATOR_SOURCE_MINIFIED : REGENERATOR_SOURCE)
.replace(swcBug1461MatchReference, "regeneratorRuntime.");
}

function stringifyPolyfillFeature(feature: PolyfillFeature): string {
Expand Down Expand Up @@ -134,7 +139,7 @@ export async function build({paths, features, ecmaVersion, context, sourcemap =
}

// TODO: Remove this when https://github.com/swc-project/swc/issues/1461 has been resolved
if (swcBug1461Match.test(code)) {
if (swcBug1461MatchA.test(code) || swcBug1461MatchB.test(code)) {
code = workAroundSwcBug1461(code, minify);
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ test("Will inline regenerator-runtime if required. #1", async t => {
}
});

test("Will inline regenerator-runtime if required. #2", async t => {
const result = await sendRequest({
path: `${constant.endpoint.polyfill}?features=form-data`,
headers: {
"User-Agent": ie("11")
}
});

const body = await result.buffer();

if (!result.ok) {
t.fail(body.toString());
} else {
t.false(body.toString().includes(`import regeneratorRuntime from"regenerator-runtime";`));
t.false(body.toString().includes(`import regeneratorRuntime from "regenerator-runtime";`));
}
});

test("Generates SourceMap if required. #1", async t => {
const result = await sendRequest({
path: `${constant.endpoint.polyfill}?features=form-data&sourcemap`,
Expand Down

0 comments on commit 4e695bc

Please sign in to comment.