Skip to content

Commit

Permalink
Merge pull request #7 from vordgi/NCM-6
Browse files Browse the repository at this point in the history
NCM-6 cache minification keys
  • Loading branch information
vordgi authored Apr 14, 2022
2 parents 1f96189 + a3e6405 commit 6b0fee6
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib/converters/ConverterMinified.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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>(.*)<\/resource><name>(.*)<\/name><class>(.*)<\/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) {
Expand Down Expand Up @@ -49,6 +76,7 @@ class ConverterMinified implements ConverterBase {

const minifiedClassName = this.getClassName();
currentCache[origName] = minifiedClassName;
this.cacheFile.write(`<resource>${resourcePath}</resource><name>${origName}</name><class>${minifiedClassName}</class>\n`);
this.lastIndex += 1;
return minifiedClassName;
}
Expand Down

0 comments on commit 6b0fee6

Please sign in to comment.