From 9ba9dc8d848ec9a813b8ce382654ae9b978d2c72 Mon Sep 17 00:00:00 2001 From: wessberg Date: Tue, 18 May 2021 16:55:39 +0200 Subject: [PATCH] fix(polyfill): work around issue in @swc/core --- src/build/build.ts | 26 ++++++++++---------------- test/server/server.test.ts | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/build/build.ts b/src/build/build.ts index 6ef9231..0cbe12f 100644 --- a/src/build/build.ts +++ b/src/build/build.ts @@ -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 */ @@ -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 { +export async function build({paths, features, ecmaVersion, context, sourcemap = false, minify = false}: BuildOptions): Promise { 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 */ @@ -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 } : {}) }); @@ -111,7 +111,6 @@ 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 @@ -119,14 +118,9 @@ export async function build({paths, features, featuresRequested, ecmaVersion, co 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: { diff --git a/test/server/server.test.ts b/test/server/server.test.ts index 8d538e4..3c6310f 100644 --- a/test/server/server.test.ts +++ b/test/server/server.test.ts @@ -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,