Skip to content

Commit

Permalink
fix(polyfill): work around issue in @swc/core
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed May 18, 2021
1 parent 706ac97 commit 9ba9dc8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const swcBug1461Match = /var regeneratorRuntime\d?\s*=\s*require\(["'`]regenerat
const swcBug1461MatchReference = /regeneratorRuntime\d\./g;
const unicodeEscape = /(\\+)u\{([0-9a-fA-F]+)\}/g;

// TODO: Input SourceMaps are broken in the current version of @swc/core
// Remove this when the problem has been resolved
const allowInputSourceMapsForSwc = false;

/**¨
* TODO: Remove this when https://github.com/swc-project/swc/issues/1227 has been resolved
*/
Expand Down Expand Up @@ -56,17 +60,14 @@ function stringifyPolyfillFeature(feature: IPolyfillFeature): string {
return `${feature.name}${metaEntriesText.length === 0 ? "" : ` (${metaEntriesText})`}`;
}

export async function build({paths, features, featuresRequested, ecmaVersion, context, sourcemap = false, minify = false}: BuildOptions): Promise<BuildResult> {
export async function build({paths, features, ecmaVersion, context, sourcemap = false, minify = false}: BuildOptions): Promise<BuildResult> {
const entryText = paths.map(path => `import "${path}";`).join("\n");

// Generate the intro text
const featuresRequestedText = featuresRequested.length < 1 ? `No features requested` : `Features requested: ${featuresRequested.map(stringifyPolyfillFeature).join(", ")}`;
const polyfillsAppliedText = features.length < 1 ? `No polyfills applied` : `Polyfills applied (in order): ${features.map(stringifyPolyfillFeature).join(", ")}`;

const banner = `\
/**
* ${featuresRequestedText}
*
* ${polyfillsAppliedText}
* @preserve
*/
Expand All @@ -91,13 +92,12 @@ export async function build({paths, features, featuresRequested, ecmaVersion, co
bundle: true,
target: "esnext",
mainFields: context === "node" ? ["module", "es2015", "main"] : ["browser", "module", "es2015", "main"],
sourcemap,
sourcemap: sourcemap ? (canUseOnlyEsbuild ? "inline" : true) : false,
entryPoints: [tempInputFileLocation],
...(canUseOnlyEsbuild
? {
minify,
target: ecmaVersion,
sourcemap: sourcemap ? "inline" : false
target: ecmaVersion
}
: {})
});
Expand All @@ -111,22 +111,16 @@ export async function build({paths, features, featuresRequested, ecmaVersion, co

let code = Buffer.from(codeOutputFile.contents).toString("utf8");
let map = mapOutputFile == null ? undefined : Buffer.from(mapOutputFile.contents).toString("utf8");

// We might need to apply transformations in a separate step using swc if the target is ES5 or below
if (!canUseOnlyEsbuild) {
// swc doesn't support es2020 as a target
if (ecmaVersion === "es2020") {
ecmaVersion = "es2019";
}

({code, map} = await transform(code, {
// TODO: Comment this back in when a regression has been resolved
...(2 + 2 === 4
? {}
: {
sourceMaps: sourcemap ? "inline" : false,
inputSourceMap: map
}),
({code} = await transform(code, {
sourceMaps: sourcemap ? "inline" : false,
inputSourceMap: allowInputSourceMapsForSwc ? map : undefined,
minify,
filename: virtualOutputFileName,
jsc: {
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 @@ -157,6 +157,25 @@ test("Will inline regenerator-runtime if required. #1", async t => {
}
});

test("Generates SourceMap 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&sourcemap`,
acceptEncoding: undefined
});

if (!("body" in result)) {
t.false("The API didn't have a body");
} else {
t.true(result.body.toString().includes(`//# sourceMappingURL=data:application/json;base64`));
}
});

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 9ba9dc8

Please sign in to comment.