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 54e2ca0 commit 53aff2b
Show file tree
Hide file tree
Showing 5 changed files with 822 additions and 56 deletions.
105 changes: 80 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@wessberg/pointer-events": "1.0.9",
"@wessberg/stringutil": "1.0.19",
"esbuild": "0.8.12",
"@swc/core": "1.2.39",
"@swc/core": "1.2.50",
"Base64": "1.1.0",
"astring": "1.4.3",
"blob-polyfill": "4.0.20200601",
Expand Down
40 changes: 11 additions & 29 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,15 @@ import {join} from "path";
import {generateRandomHash} from "../util/hash-util/hash-util";
import {unlinkSync, writeFileSync} from "fs";
import {transform} from "@swc/core";
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
* TODO: Remove this when https://github.com/swc-project/swc/issues/1461 has been resolved
*/
function workAroundSwcBug(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);
function workAroundSwcBug1461(str: string): string {
return str.replace(swcBug1461String, REGENERATOR_SOURCE);
}

function stringifyPolyfillFeature(feature: IPolyfillFeature): string {
Expand Down Expand Up @@ -109,11 +91,6 @@ 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 = workAroundSwcBug(code);
}

({code, map} = await transform(code, {
sourceMaps: sourcemap ? "inline" : false,
inputSourceMap: map,
Expand All @@ -123,6 +100,11 @@ export async function build({paths, features, featuresRequested, ecmaVersion, co
target: ecmaVersion
}
}));

// TODO: Remove this when https://github.com/swc-project/swc/issues/1461 has been resolved
if (code.includes(swcBug1461String)) {
code = workAroundSwcBug1461(code);
}
}

// Apply encoding based on the given options
Expand Down
Loading

0 comments on commit 53aff2b

Please sign in to comment.