Skip to content

Commit

Permalink
fix(swc): make sure regenerator-runtime is always inlined
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed May 18, 2021
1 parent 9b23786 commit 8f0f4d9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 63 deletions.
132 changes: 77 additions & 55 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
],
"scripts": {
"start": "node ./dist/index.js",
"generate:scaffold": "scaffold all --yes",
"generate:sandhog": "sandhog all --yes",
"generate:changelog": "standard-changelog --first-release",
"generate:all": "npm run generate:scaffold && npm run generate:changelog",
"generate:all": "npm run generate:sandhog && npm run generate:changelog",
"clean": "rimraf dist",
"lint": "tsc --noEmit && eslint \"src/**/*.ts\" --color",
"prettier": "prettier --write '{src,test,documentation}/**/*.{js,ts,json,html,xml,css,md}'",
Expand Down Expand Up @@ -43,8 +43,7 @@
"@typescript-eslint/parser": "4.24.0",
"@wessberg/di-compiler": "2.1.1",
"@wessberg/rollup-plugin-ts": "1.3.14",
"@wessberg/scaffold": "1.0.36",
"@wessberg/ts-config": "1.0.22",
"@wessberg/ts-config": "1.1.0",
"ava": "3.15.0",
"eslint": "7.26.0",
"eslint-config-prettier": "8.3.0",
Expand All @@ -57,6 +56,7 @@
"pretty-quick": "3.1.0",
"rimraf": "3.0.2",
"rollup": "2.48.0",
"sandhog": "1.0.38",
"standard-changelog": "2.0.27",
"ts-node": "9.1.1",
"typescript": "4.2.4",
Expand Down
2 changes: 1 addition & 1 deletion scaffold.config.js → sandhog.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
...require("@wessberg/ts-config/scaffold.config"),
...require("@wessberg/ts-config/sandhog.config"),
logo: {
url: "https://raw.githubusercontent.com/wessberg/Polyfiller/master/documentation/asset/logo-color-text.png",
height: 80
Expand Down
5 changes: 3 additions & 2 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {unlinkSync, writeFileSync} from "fs";
import {transform} from "@swc/core";
import {REGENERATOR_SOURCE, REGENERATOR_SOURCE_MINIFIED} from "../constant/regenerator-source";

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

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ 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);
return str.replace(swcBug1461Match, minify ? REGENERATOR_SOURCE_MINIFIED : REGENERATOR_SOURCE).replace(swcBug1461MatchReference, "regeneratorRuntime.");
}

function stringifyPolyfillFeature(feature: IPolyfillFeature): string {
Expand Down
21 changes: 20 additions & 1 deletion test/server/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test("Is able to generate a bundle of every available polyfill", async t => {
.filter(([, value]) => !("polyfills" in value))
.map(([key, value]) => {
if (key === "zone") {
return `${key}|${Object.keys(((value as unknown) as PolyfillDictNormalizedEntry).meta!).join("|")}|force`;
return `${key}|${Object.keys((value as unknown as PolyfillDictNormalizedEntry).meta!).join("|")}|force`;
} else if (key.startsWith("intl.")) {
return `${key}|locale=en~da|force`;
} else {
Expand Down Expand Up @@ -138,6 +138,25 @@ test("Will correctly parse meta information for Zone. #1", async t => {
t.true([...polyfillRequest.features].some(({meta, name}) => name === "zone" && meta != null && meta.error === true));
});

test("Will inline regenerator-runtime if required. #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=form-data`,
acceptEncoding: undefined
});

if (!("body" in result)) {
t.false("The API didn't have a body");
} else {
t.false(result.body.toString().includes(`require("regenerator-runtime")`));
}
});

test("Will set a 'x-applied-polyfills' header on HTTP2 responses with a HTTP-friendly list of all applied polyfills. #1", async t => {
const result = await sendRequest({
http2: config.http2,
Expand Down

0 comments on commit 8f0f4d9

Please sign in to comment.