From ac601a44b99c8f5d3145b53aca0a63e7e9f2139e Mon Sep 17 00:00:00 2001 From: wessberg Date: Tue, 24 Nov 2020 12:57:55 +0100 Subject: [PATCH] fix: generate unique cache keys grouped by ecma version --- src/build/build-options.ts | 2 ++ src/build/build.ts | 10 ++-------- src/polyfill/i-polyfill-request.ts | 2 ++ .../polyfill-builder/polyfill-builder-service.ts | 1 + src/util/polyfill/polyfill-util.ts | 13 +++---------- src/util/type/ecma-version.ts | 1 + 6 files changed, 11 insertions(+), 18 deletions(-) create mode 100644 src/util/type/ecma-version.ts diff --git a/src/build/build-options.ts b/src/build/build-options.ts index 8e959c8..06b5581 100644 --- a/src/build/build-options.ts +++ b/src/build/build-options.ts @@ -1,10 +1,12 @@ import {IPolyfillFeature} from "../polyfill/i-polyfill-feature"; import {PolyfillContext} from "../polyfill/polyfill-context"; +import {EcmaVersion} from "../util/type/ecma-version"; export interface BuildOptions { context: PolyfillContext; features: IPolyfillFeature[]; featuresRequested: IPolyfillFeature[]; + ecmaVersion: EcmaVersion; paths: string[]; sourcemap?: boolean; minify?: boolean; diff --git a/src/build/build.ts b/src/build/build.ts index 0ab2ecf..81604dd 100644 --- a/src/build/build.ts +++ b/src/build/build.ts @@ -1,6 +1,5 @@ import {BuildOptions} from "./build-options"; import {BuildResult} from "./build-result"; -import {generateBrowserslistFromUseragent, getAppropriateEcmaVersionForBrowserslist} from "@wessberg/browserslist-generator"; import {brotliEncode, gzipEncode} from "./util/encoding"; import {BROTLI_OPTIONS} from "./options/brotli-options"; import {ZLIB_OPTIONS} from "./options/zlib-options"; @@ -49,7 +48,7 @@ function stringifyPolyfillFeature(feature: IPolyfillFeature): string { return `${feature.name}${metaEntriesText.length === 0 ? "" : ` (${metaEntriesText})`}`; } -export async function build({paths, features, featuresRequested, userAgent, context, sourcemap = false, minify = false}: BuildOptions): Promise { +export async function build({paths, features, featuresRequested, ecmaVersion, context, sourcemap = false, minify = false}: BuildOptions): Promise { const entryText = paths.map(path => `import "${path}";`).join("\n"); // Generate the intro text @@ -69,13 +68,12 @@ export async function build({paths, features, featuresRequested, userAgent, cont writeFileSync(tempInputFileLocation, entryText); - let ecmaVersion = userAgent == null ? "es3" : getAppropriateEcmaVersionForBrowserslist(generateBrowserslistFromUseragent(userAgent)); - // esbuild only supports transforming down to es2015 const canUseOnlyEsbuild = ecmaVersion !== "es3" && ecmaVersion !== "es5"; try { const result = await esbuild({ + banner, write: false, format: "iife", outfile: virtualOutputFileName, @@ -127,10 +125,6 @@ export async function build({paths, features, featuresRequested, userAgent, cont })); } - if (!sourcemap) { - code = `${banner}${code}`; - } - // Apply encoding based on the given options const raw = Buffer.from(code); const brotli = await brotliEncode(raw, BROTLI_OPTIONS); diff --git a/src/polyfill/i-polyfill-request.ts b/src/polyfill/i-polyfill-request.ts index a533e42..f0c1a45 100644 --- a/src/polyfill/i-polyfill-request.ts +++ b/src/polyfill/i-polyfill-request.ts @@ -1,9 +1,11 @@ import {IPolyfillFeatureInput} from "./i-polyfill-feature"; import {ContentEncodingKind} from "../encoding/content-encoding-kind"; import {PolyfillContext} from "./polyfill-context"; +import {EcmaVersion} from "../util/type/ecma-version"; export interface IPolyfillRequest { userAgent: string; + ecmaVersion: EcmaVersion; encoding?: ContentEncodingKind; features: Set; sourcemap: boolean; diff --git a/src/service/polyfill-builder/polyfill-builder-service.ts b/src/service/polyfill-builder/polyfill-builder-service.ts index 45920bd..be53e52 100644 --- a/src/service/polyfill-builder/polyfill-builder-service.ts +++ b/src/service/polyfill-builder/polyfill-builder-service.ts @@ -215,6 +215,7 @@ export class PolyfillBuilderService implements IPolyfillBuilderService { const {brotli, zlib, raw} = await build({ context: request.context, userAgent: request.userAgent, + ecmaVersion: request.ecmaVersion, featuresRequested: [...request.features], paths: [...new Set(paths)], features: [...polyfillSet], diff --git a/src/util/polyfill/polyfill-util.ts b/src/util/polyfill/polyfill-util.ts index 16db084..85d2db9 100644 --- a/src/util/polyfill/polyfill-util.ts +++ b/src/util/polyfill/polyfill-util.ts @@ -10,7 +10,7 @@ import {polyfillRawForceName} from "../../polyfill/polyfill-raw-force-name"; import {polyfillOptionValueSeparator} from "../../polyfill/polyfill-option-value-separator"; import {createHash} from "crypto"; import {constant} from "../../constant/constant"; -import {userAgentSupportsFeatures} from "@wessberg/browserslist-generator"; +import {generateBrowserslistFromUseragent, getAppropriateEcmaVersionForBrowserslist, userAgentSupportsFeatures} from "@wessberg/browserslist-generator"; import {truncate} from "@wessberg/stringutil"; import {IPolyfillLibraryDictEntry, IPolyfillLocalDictEntry} from "../../polyfill/polyfill-dict"; // eslint-disable-next-line @typescript-eslint/ban-ts-ignore @@ -22,9 +22,6 @@ import {PolyfillCachingContext} from "../../service/registry/polyfill-registry/i /** * Traces all polyfill names that matches the given name. It may be an alias, and it may refer to additional aliases * within the given features - * - * @param name - * @returns */ export function traceAllPolyfillNamesForPolyfillName(name: PolyfillName): Set { // Get the PolyfillDict that matches the given name @@ -55,7 +52,7 @@ export function getPolyfillIdentifier( const normalizedName = name instanceof Set ? name : new Set([name]); const sortedName = [...normalizedName].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)); const namePart = sortedName.map(part => `${part.name}${JSON.stringify(part.meta)}${context}`).join(","); - shasum.update(`[${namePart}].${context.sourcemap}.${context.minify}.${context.encoding || "none"}`); + shasum.update(`[${namePart}].${context.sourcemap}.${context.minify}.${context.ecmaVersion}.${context.encoding || "none"}`); return shasum.digest("hex"); } @@ -181,11 +178,6 @@ export async function getOrderedPolyfillsWithDependencies(polyfillSet: Set