-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into enhance-css-formal-…
…syntax
- Loading branch information
Showing
20 changed files
with
253 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import fs from "node:fs"; | ||
import { | ||
BUILD_OUT_ROOT, | ||
GOOGLE_ANALYTICS_MEASUREMENT_ID, | ||
} from "../libs/env/index.js"; | ||
import path from "node:path"; | ||
|
||
export async function generateGA() { | ||
const outFile = path.join(BUILD_OUT_ROOT, "static", "js", "gtag.js"); | ||
const measurementIds = | ||
GOOGLE_ANALYTICS_MEASUREMENT_ID.split(",").filter(Boolean); | ||
if (measurementIds.length) { | ||
const dntHelperCode = fs | ||
.readFileSync( | ||
new URL("mozilla.dnthelper.min.js", import.meta.url), | ||
"utf-8" | ||
) | ||
.trim(); | ||
|
||
const firstMeasurementId = measurementIds[0]; | ||
const gaScriptURL = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(firstMeasurementId)}`; | ||
|
||
const code = ` | ||
// Mozilla DNT Helper | ||
${dntHelperCode} | ||
// Load GA unless DNT is enabled. | ||
if (Mozilla && !Mozilla.dntEnabled()) { | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
${measurementIds | ||
.map((id) => `gtag('config', '${id}', { 'anonymize_ip': true });`) | ||
.join("\n ")} | ||
var gaScript = document.createElement('script'); | ||
gaScript.async = true; | ||
gaScript.src = '${gaScriptURL}'; | ||
document.head.appendChild(gaScript); | ||
}`.trim(); | ||
fs.writeFileSync(outFile, `${code}\n`, "utf-8"); | ||
console.log( | ||
`Generated ${outFile} for SSR rendering using ${GOOGLE_ANALYTICS_MEASUREMENT_ID}.` | ||
); | ||
} else { | ||
console.log("No Google Analytics code file generated"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const WEBFONT_TAGS: string; | ||
export const GTAG_PATH: null | string; | ||
export const BASE_URL: string; | ||
export const ALWAYS_ALLOW_ROBOTS: boolean; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { fileURLToPath } from "node:url"; | ||
import { | ||
ALWAYS_ALLOW_ROBOTS, | ||
BUILD_OUT_ROOT, | ||
BASE_URL, | ||
} from "../libs/env/index.js"; | ||
import { generateGA } from "./ga.js"; | ||
|
||
const dirname = path.dirname(fileURLToPath(new URL(".", import.meta.url))); | ||
const clientBuildRoot = path.resolve(dirname, "client/build"); | ||
|
||
function extractWebFontURLs() { | ||
const urls: string[] = []; | ||
const manifest = JSON.parse( | ||
fs.readFileSync(path.join(clientBuildRoot, "asset-manifest.json"), "utf-8") | ||
); | ||
for (const entrypoint of manifest.entrypoints) { | ||
if (!entrypoint.endsWith(".css")) continue; | ||
const css = fs.readFileSync( | ||
path.join(clientBuildRoot, entrypoint), | ||
"utf-8" | ||
); | ||
const generator = extractCSSURLs(css, (url) => url.endsWith(".woff2")); | ||
urls.push(...generator); | ||
} | ||
return [...new Set(urls)]; | ||
} | ||
|
||
function* extractCSSURLs(css, filterFunction) { | ||
for (const match of css.matchAll(/url\((.*?)\)/g)) { | ||
const url = match[1]; | ||
if (filterFunction(url)) { | ||
yield url; | ||
} | ||
} | ||
} | ||
|
||
function webfontTags(webfontURLs): string { | ||
return webfontURLs | ||
.map( | ||
(url) => | ||
`<link rel="preload" as="font" type="font/woff2" href="${url}" crossorigin>` | ||
) | ||
.join(""); | ||
} | ||
|
||
function gtagScriptPath(relPath = "/static/js/gtag.js") { | ||
const filePath = relPath.split("/").slice(1).join(path.sep); | ||
if (fs.existsSync(path.join(BUILD_OUT_ROOT, filePath))) { | ||
return relPath; | ||
} | ||
return null; | ||
} | ||
|
||
function prepare() { | ||
const webfontURLs = extractWebFontURLs(); | ||
const tags = webfontTags(webfontURLs); | ||
const gtagPath = gtagScriptPath(); | ||
|
||
fs.writeFileSync( | ||
path.join(dirname, "ssr", "include.ts"), | ||
` | ||
export const WEBFONT_TAGS = ${JSON.stringify(tags)}; | ||
export const GTAG_PATH = ${JSON.stringify(gtagPath)}; | ||
export const BASE_URL = ${JSON.stringify(BASE_URL)}; | ||
export const ALWAYS_ALLOW_ROBOTS = ${JSON.stringify(ALWAYS_ALLOW_ROBOTS)}; | ||
` | ||
); | ||
} | ||
|
||
generateGA().then(() => prepare()); |
Oops, something went wrong.