Skip to content

Commit

Permalink
fix: generate unique cache keys grouped by ecma version
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Nov 24, 2020
1 parent 7c21f5e commit ac601a4
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/build/build-options.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 2 additions & 8 deletions src/build/build.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<BuildResult> {
export async function build({paths, features, featuresRequested, ecmaVersion, context, sourcemap = false, minify = false}: BuildOptions): Promise<BuildResult> {
const entryText = paths.map(path => `import "${path}";`).join("\n");

// Generate the intro text
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/polyfill/i-polyfill-request.ts
Original file line number Diff line number Diff line change
@@ -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<IPolyfillFeatureInput>;
sourcemap: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/service/polyfill-builder/polyfill-builder-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
13 changes: 3 additions & 10 deletions src/util/polyfill/polyfill-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<PolyfillDealiasedName> {
// Get the PolyfillDict that matches the given name
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -181,11 +178,6 @@ export async function getOrderedPolyfillsWithDependencies(polyfillSet: Set<IPoly

/**
* Generates an IPolyfillRequest from the given URL
*
* @param url
* @param userAgent
* @param [encoding]
* @returns
*/
export function getPolyfillRequestFromUrl(url: URL, userAgent: string, encoding?: ContentEncodingKind): IPolyfillRequest {
const featuresRaw = url.searchParams.get("features");
Expand Down Expand Up @@ -243,6 +235,7 @@ export function getPolyfillRequestFromUrl(url: URL, userAgent: string, encoding?
// Return the IPolyfillRequest
return {
userAgent,
ecmaVersion: getAppropriateEcmaVersionForBrowserslist(generateBrowserslistFromUseragent(userAgent)),
encoding,
features: featureSet,
context,
Expand Down
1 change: 1 addition & 0 deletions src/util/type/ecma-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type EcmaVersion = "es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020";

0 comments on commit ac601a4

Please sign in to comment.