Actually remove non used resources
This commit is contained in:
parent
3c2820dc31
commit
983af57842
@ -24,9 +24,9 @@ async function main() {
|
|||||||
fs.rmSync(tmpDirPath, { "recursive": true, "force": true });
|
fs.rmSync(tmpDirPath, { "recursive": true, "force": true });
|
||||||
|
|
||||||
await downloadBuiltinKeycloakTheme({
|
await downloadBuiltinKeycloakTheme({
|
||||||
|
"projectDirPath": getProjectRoot(),
|
||||||
keycloakVersion,
|
keycloakVersion,
|
||||||
"destDirPath": tmpDirPath,
|
"destDirPath": tmpDirPath
|
||||||
isSilent
|
|
||||||
});
|
});
|
||||||
|
|
||||||
type Dictionary = { [idiomId: string]: string };
|
type Dictionary = { [idiomId: string]: string };
|
||||||
|
@ -27,7 +27,8 @@ import * as fs from "fs";
|
|||||||
projectDirPath,
|
projectDirPath,
|
||||||
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
|
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
|
||||||
"themeType": themeType,
|
"themeType": themeType,
|
||||||
"themeDirPath": keycloakDirInPublicDir
|
"themeDirPath": keycloakDirInPublicDir,
|
||||||
|
"usedResources": undefined
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,8 +12,6 @@ export async function downloadBuiltinKeycloakTheme(params: { projectDirPath: str
|
|||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
||||||
console.log("Downloading Keycloak theme...", { keycloakVersion });
|
|
||||||
|
|
||||||
await downloadAndUnzip({
|
await downloadAndUnzip({
|
||||||
"doUseCache": true,
|
"doUseCache": true,
|
||||||
projectDirPath,
|
projectDirPath,
|
||||||
@ -21,7 +19,7 @@ export async function downloadBuiltinKeycloakTheme(params: { projectDirPath: str
|
|||||||
"url": `https://github.com/keycloak/keycloak/archive/refs/tags/${keycloakVersion}.zip`,
|
"url": `https://github.com/keycloak/keycloak/archive/refs/tags/${keycloakVersion}.zip`,
|
||||||
"specificDirsToExtract": ["", "-community"].map(ext => `keycloak-${keycloakVersion}/themes/src/main/resources${ext}/theme`),
|
"specificDirsToExtract": ["", "-community"].map(ext => `keycloak-${keycloakVersion}/themes/src/main/resources${ext}/theme`),
|
||||||
"preCacheTransform": {
|
"preCacheTransform": {
|
||||||
"actionCacheId": "Build Keycloak resources",
|
"actionCacheId": "npm install and build",
|
||||||
"action": async ({ destDirPath }) => {
|
"action": async ({ destDirPath }) => {
|
||||||
install_common_node_modules: {
|
install_common_node_modules: {
|
||||||
const commonResourcesDirPath = pathJoin(destDirPath, "keycloak", "common", "resources");
|
const commonResourcesDirPath = pathJoin(destDirPath, "keycloak", "common", "resources");
|
||||||
|
@ -17,9 +17,19 @@ export async function downloadKeycloakStaticResources(
|
|||||||
themeType: ThemeType;
|
themeType: ThemeType;
|
||||||
themeDirPath: string;
|
themeDirPath: string;
|
||||||
keycloakVersion: string;
|
keycloakVersion: string;
|
||||||
|
usedResources: {
|
||||||
|
resourcesCommonFilePaths: string[];
|
||||||
|
resourcesFilePaths: string[];
|
||||||
|
} | undefined
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
const { projectDirPath, themeType, themeDirPath, keycloakVersion } = params;
|
const { projectDirPath, themeType, themeDirPath, keycloakVersion, usedResources } = params;
|
||||||
|
|
||||||
|
console.log({
|
||||||
|
themeDirPath,
|
||||||
|
keycloakVersion,
|
||||||
|
usedResources
|
||||||
|
});
|
||||||
|
|
||||||
const tmpDirPath = pathJoin(
|
const tmpDirPath = pathJoin(
|
||||||
themeDirPath,
|
themeDirPath,
|
||||||
@ -35,12 +45,32 @@ export async function downloadKeycloakStaticResources(
|
|||||||
|
|
||||||
transformCodebase({
|
transformCodebase({
|
||||||
"srcDirPath": pathJoin(tmpDirPath, "keycloak", themeType, "resources"),
|
"srcDirPath": pathJoin(tmpDirPath, "keycloak", themeType, "resources"),
|
||||||
"destDirPath": pathJoin(themeDirPath, pathRelative(basenameOfKeycloakDirInPublicDir, resourcesDirPathRelativeToPublicDir))
|
"destDirPath": pathJoin(themeDirPath, pathRelative(basenameOfKeycloakDirInPublicDir, resourcesDirPathRelativeToPublicDir)),
|
||||||
|
"transformSourceCode":
|
||||||
|
usedResources === undefined
|
||||||
|
? undefined
|
||||||
|
: ({ fileRelativePath, sourceCode }) => {
|
||||||
|
if (!usedResources.resourcesFilePaths.includes(fileRelativePath)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { "modifiedSourceCode": sourceCode };
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
transformCodebase({
|
transformCodebase({
|
||||||
"srcDirPath": pathJoin(tmpDirPath, "keycloak", "common", "resources"),
|
"srcDirPath": pathJoin(tmpDirPath, "keycloak", "common", "resources"),
|
||||||
"destDirPath": pathJoin(themeDirPath, pathRelative(basenameOfKeycloakDirInPublicDir, resourcesCommonDirPathRelativeToPublicDir))
|
"destDirPath": pathJoin(themeDirPath, pathRelative(basenameOfKeycloakDirInPublicDir, resourcesCommonDirPathRelativeToPublicDir)),
|
||||||
|
"transformSourceCode":
|
||||||
|
usedResources === undefined
|
||||||
|
? undefined
|
||||||
|
: ({ fileRelativePath, sourceCode }) => {
|
||||||
|
if (!usedResources.resourcesCommonFilePaths.includes(fileRelativePath)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { "modifiedSourceCode": sourceCode };
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.rmSync(tmpDirPath, { "recursive": true, "force": true });
|
fs.rmSync(tmpDirPath, { "recursive": true, "force": true });
|
||||||
|
@ -12,6 +12,7 @@ import { downloadKeycloakStaticResources } from "./downloadKeycloakStaticResourc
|
|||||||
import { readFieldNameUsage } from "./readFieldNameUsage";
|
import { readFieldNameUsage } from "./readFieldNameUsage";
|
||||||
import { readExtraPagesNames } from "./readExtraPageNames";
|
import { readExtraPagesNames } from "./readExtraPageNames";
|
||||||
import { generateMessageProperties } from "./generateMessageProperties";
|
import { generateMessageProperties } from "./generateMessageProperties";
|
||||||
|
import { readStaticResourcesUsage } from "./readStaticResourcesUsage";
|
||||||
|
|
||||||
export type BuildOptionsLike = {
|
export type BuildOptionsLike = {
|
||||||
themeName: string;
|
themeName: string;
|
||||||
@ -172,7 +173,8 @@ export async function generateTheme(params: {
|
|||||||
projectDirPath,
|
projectDirPath,
|
||||||
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
|
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
|
||||||
"themeDirPath": keycloakDirInPublicDir,
|
"themeDirPath": keycloakDirInPublicDir,
|
||||||
themeType
|
themeType,
|
||||||
|
"usedResources": undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
if (themeType !== themeTypes[0]) {
|
if (themeType !== themeTypes[0]) {
|
||||||
@ -197,7 +199,12 @@ export async function generateTheme(params: {
|
|||||||
projectDirPath,
|
projectDirPath,
|
||||||
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
|
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
|
||||||
themeDirPath,
|
themeDirPath,
|
||||||
|
themeType,
|
||||||
|
"usedResources": readStaticResourcesUsage({
|
||||||
|
keycloakifySrcDirPath,
|
||||||
|
themeSrcDirPath,
|
||||||
themeType
|
themeType
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
|
@ -19,13 +19,11 @@ export function readStaticResourcesUsage(params: { keycloakifySrcDirPath: string
|
|||||||
for (const filePath of filePaths) {
|
for (const filePath of filePaths) {
|
||||||
const rawSourceFile = fs.readFileSync(filePath).toString("utf8");
|
const rawSourceFile = fs.readFileSync(filePath).toString("utf8");
|
||||||
|
|
||||||
if (!rawSourceFile.includes("resourcesCommonPath")) {
|
if (!rawSourceFile.includes("resourcesCommonPath") && !rawSourceFile.includes("resourcesPath")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!rawSourceFile.includes("resourcesPath")) {
|
console.log("=========>", filePath);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const wrap = readPaths({ rawSourceFile });
|
const wrap = readPaths({ rawSourceFile });
|
||||||
|
|
||||||
|
@ -10,8 +10,46 @@ import { unzip, zip } from "./unzip";
|
|||||||
|
|
||||||
const exec = promisify(execCallback);
|
const exec = promisify(execCallback);
|
||||||
|
|
||||||
function sha256(s: string) {
|
function generateFileNameFromURL(params: {
|
||||||
return createHash("sha256").update(s).digest("hex");
|
url: string;
|
||||||
|
preCacheTransform:
|
||||||
|
| {
|
||||||
|
actionCacheId: string;
|
||||||
|
actionFootprint: string;
|
||||||
|
}
|
||||||
|
| undefined;
|
||||||
|
}): string {
|
||||||
|
const { preCacheTransform } = params;
|
||||||
|
|
||||||
|
// Parse the URL
|
||||||
|
const url = new URL(params.url);
|
||||||
|
|
||||||
|
// Extract pathname and remove leading slashes
|
||||||
|
let fileName = url.pathname.replace(/^\//, "").replace(/\//g, "_");
|
||||||
|
|
||||||
|
// Optionally, add query parameters replacing special characters
|
||||||
|
if (url.search) {
|
||||||
|
fileName += url.search.replace(/[&=?]/g, "-");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace any characters that are not valid in filenames
|
||||||
|
fileName = fileName.replace(/[^a-zA-Z0-9-_]/g, "");
|
||||||
|
|
||||||
|
// Trim or pad the fileName to a specific length
|
||||||
|
fileName = fileName.substring(0, 50);
|
||||||
|
|
||||||
|
add_pre_cache_transform: {
|
||||||
|
if (preCacheTransform === undefined) {
|
||||||
|
break add_pre_cache_transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sanitize actionCacheId the same way as other components
|
||||||
|
const sanitizedActionCacheId = preCacheTransform.actionCacheId.replace(/[^a-zA-Z0-9-_]/g, "_");
|
||||||
|
|
||||||
|
fileName += `_${sanitizedActionCacheId}_${createHash("sha256").update(preCacheTransform.actionFootprint).digest("hex").substring(0, 5)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function exists(path: string) {
|
async function exists(path: string) {
|
||||||
@ -133,14 +171,22 @@ export async function downloadAndUnzip(
|
|||||||
) {
|
) {
|
||||||
const { url, destDirPath, specificDirsToExtract, preCacheTransform, ...rest } = params;
|
const { url, destDirPath, specificDirsToExtract, preCacheTransform, ...rest } = params;
|
||||||
|
|
||||||
const hash = sha256(
|
const zipFileBasename = generateFileNameFromURL({
|
||||||
JSON.stringify({ url }) + (preCacheTransform === undefined ? "" : `${preCacheTransform.actionCacheId}${preCacheTransform.action.toString()}`)
|
url,
|
||||||
).substring(0, 15);
|
"preCacheTransform":
|
||||||
|
preCacheTransform === undefined
|
||||||
|
? undefined
|
||||||
|
: {
|
||||||
|
"actionCacheId": preCacheTransform.actionCacheId,
|
||||||
|
"actionFootprint": preCacheTransform.action.toString()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const cacheRoot = !rest.doUseCache
|
const cacheRoot = !rest.doUseCache
|
||||||
? `tmp_${Math.random().toString().slice(2, 12)}`
|
? `tmp_${Math.random().toString().slice(2, 12)}`
|
||||||
: pathJoin(process.env.XDG_CACHE_HOME ?? pathJoin(rest.projectDirPath, "node_modules", ".cache"), "keycloakify");
|
: pathJoin(process.env.XDG_CACHE_HOME ?? pathJoin(rest.projectDirPath, "node_modules", ".cache"), "keycloakify");
|
||||||
const zipFilePath = pathJoin(cacheRoot, `_${hash}.zip`);
|
const zipFilePath = pathJoin(cacheRoot, `${zipFileBasename}.zip`);
|
||||||
const extractDirPath = pathJoin(cacheRoot, `tmp_unzip_${hash}`);
|
const extractDirPath = pathJoin(cacheRoot, `tmp_unzip_${zipFileBasename}`);
|
||||||
|
|
||||||
if (!(await exists(zipFilePath))) {
|
if (!(await exists(zipFilePath))) {
|
||||||
const opts = await getFetchOptions();
|
const opts = await getFetchOptions();
|
||||||
@ -181,5 +227,7 @@ export async function downloadAndUnzip(
|
|||||||
|
|
||||||
if (!rest.doUseCache) {
|
if (!rest.doUseCache) {
|
||||||
await rm(cacheRoot, { "recursive": true });
|
await rm(cacheRoot, { "recursive": true });
|
||||||
|
} else {
|
||||||
|
await rm(extractDirPath, { "recursive": true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ import * as path from "path";
|
|||||||
import { crawl } from "./crawl";
|
import { crawl } from "./crawl";
|
||||||
import { id } from "tsafe/id";
|
import { id } from "tsafe/id";
|
||||||
|
|
||||||
type TransformSourceCode = (params: { sourceCode: Buffer; filePath: string }) =>
|
type TransformSourceCode = (params: { sourceCode: Buffer; filePath: string; fileRelativePath: string }) =>
|
||||||
| {
|
| {
|
||||||
modifiedSourceCode: Buffer;
|
modifiedSourceCode: Buffer;
|
||||||
newFileName?: string;
|
newFileName?: string;
|
||||||
@ -20,26 +20,27 @@ export function transformCodebase(params: { srcDirPath: string; destDirPath: str
|
|||||||
}))
|
}))
|
||||||
} = params;
|
} = params;
|
||||||
|
|
||||||
for (const file_relative_path of crawl({ "dirPath": srcDirPath, "returnedPathsType": "relative to dirPath" })) {
|
for (const fileRelativePath of crawl({ "dirPath": srcDirPath, "returnedPathsType": "relative to dirPath" })) {
|
||||||
const filePath = path.join(srcDirPath, file_relative_path);
|
const filePath = path.join(srcDirPath, fileRelativePath);
|
||||||
|
|
||||||
const transformSourceCodeResult = transformSourceCode({
|
const transformSourceCodeResult = transformSourceCode({
|
||||||
"sourceCode": fs.readFileSync(filePath),
|
"sourceCode": fs.readFileSync(filePath),
|
||||||
filePath
|
filePath,
|
||||||
|
fileRelativePath
|
||||||
});
|
});
|
||||||
|
|
||||||
if (transformSourceCodeResult === undefined) {
|
if (transformSourceCodeResult === undefined) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.mkdirSync(path.dirname(path.join(destDirPath, file_relative_path)), {
|
fs.mkdirSync(path.dirname(path.join(destDirPath, fileRelativePath)), {
|
||||||
"recursive": true
|
"recursive": true
|
||||||
});
|
});
|
||||||
|
|
||||||
const { newFileName, modifiedSourceCode } = transformSourceCodeResult;
|
const { newFileName, modifiedSourceCode } = transformSourceCodeResult;
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
path.join(path.dirname(path.join(destDirPath, file_relative_path)), newFileName ?? path.basename(file_relative_path)),
|
path.join(path.dirname(path.join(destDirPath, fileRelativePath)), newFileName ?? path.basename(fileRelativePath)),
|
||||||
modifiedSourceCode
|
modifiedSourceCode
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,8 @@ describe("Ensure it's able to extract used Keycloak resources", () => {
|
|||||||
"resourcesCommonFilePaths": [
|
"resourcesCommonFilePaths": [
|
||||||
"node_modules/patternfly/dist/css/patternfly.min.css",
|
"node_modules/patternfly/dist/css/patternfly.min.css",
|
||||||
"node_modules/patternfly/dist/css/patternfly-additions.min.css",
|
"node_modules/patternfly/dist/css/patternfly-additions.min.css",
|
||||||
"lib/zocial/zocial.css"
|
"lib/zocial/zocial.css",
|
||||||
|
"node_modules/jquery/dist/jquery.min.js"
|
||||||
],
|
],
|
||||||
"resourcesFilePaths": ["css/login.css"]
|
"resourcesFilePaths": ["css/login.css"]
|
||||||
};
|
};
|
||||||
@ -28,6 +29,12 @@ describe("Ensure it's able to extract used Keycloak resources", () => {
|
|||||||
"htmlClassName": getClassName("kcHtmlClass"),
|
"htmlClassName": getClassName("kcHtmlClass"),
|
||||||
"bodyClassName": undefined
|
"bodyClassName": undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { prLoaded, remove } = headInsert({
|
||||||
|
"type": "javascript",
|
||||||
|
"src": \`\${kcContext.url.resourcesCommonPath}/node_modules/jquery/dist/jquery.min.js\`
|
||||||
|
});
|
||||||
|
|
||||||
`
|
`
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -52,6 +59,11 @@ describe("Ensure it's able to extract used Keycloak resources", () => {
|
|||||||
"bodyClassName": undefined
|
"bodyClassName": undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { prLoaded, remove } = headInsert({
|
||||||
|
"type": "javascript",
|
||||||
|
"src": kcContext.url.resourcesCommonPath + "/node_modules/jquery/dist/jquery.min.js\"
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
`
|
`
|
||||||
});
|
});
|
||||||
@ -82,6 +94,11 @@ describe("Ensure it's able to extract used Keycloak resources", () => {
|
|||||||
"bodyClassName": undefined
|
"bodyClassName": undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { prLoaded, remove } = headInsert({
|
||||||
|
"type": "javascript",
|
||||||
|
"src": path.join(kcContext.url.resourcesCommonPath, "/node_modules/jquery/dist/jquery.min.js")
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
`
|
`
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user