From a3e64053aa34dcdea86ddfb8407cdef35094d8a8 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Thu, 14 Apr 2022 20:15:27 +0400 Subject: [PATCH] NCM-6 cache minification keys --- src/lib/converters/ConverterMinified.ts | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lib/converters/ConverterMinified.ts b/src/lib/converters/ConverterMinified.ts index 50d3f17..f5ae351 100644 --- a/src/lib/converters/ConverterMinified.ts +++ b/src/lib/converters/ConverterMinified.ts @@ -1,9 +1,16 @@ import type { LoaderContext } from 'webpack/types'; import type ConverterBase from './ConverterBase'; +import fs from 'fs'; +import path from 'path'; + +const cacheFolderPath = path.join(process.cwd(), '.next/cache/next-classnames-minifier'); +const cacheFilePath = path.join(cacheFolderPath, 'minified.xml'); class ConverterMinified implements ConverterBase { cache: {[resource: string]: {[className: string]: string}} = {}; + cacheFile; + symbols: string[] = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', @@ -18,6 +25,26 @@ class ConverterMinified implements ConverterBase { nameMap = [0]; + constructor() { + if (!fs.existsSync(cacheFolderPath)) fs.mkdirSync(cacheFolderPath, {recursive: true}); + + if (fs.existsSync(cacheFilePath)) { + this.cacheFile = fs.createWriteStream(cacheFilePath, {flags: 'a'}) + const cacheRow = fs.readFileSync(cacheFilePath, {encoding: 'utf-8'}); + cacheRow.split('\n').forEach(row => { + if (!row) return; + const matched = row.match(/(.*)<\/resource>(.*)<\/name>(.*)<\/class>/); + if (!matched) return; + const resource = matched[1]; + + if (!this.cache[resource]) this.cache[resource] = {}; + this.cache[resource][matched[2]] = matched[3]; + }); + } else { + this.cacheFile = fs.createWriteStream(cacheFilePath); + } + } + getClassName() { const symbolsCount = 62; if (this.lastIndex >= this.nextLoopEndsWith) { @@ -49,6 +76,7 @@ class ConverterMinified implements ConverterBase { const minifiedClassName = this.getClassName(); currentCache[origName] = minifiedClassName; + this.cacheFile.write(`${resourcePath}${origName}${minifiedClassName}\n`); this.lastIndex += 1; return minifiedClassName; }