Skip to content

Commit

Permalink
Feat: optimizedMapping always have values
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Aug 9, 2020
1 parent ee722b6 commit 25a36dc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ rcs.fillLibraries(fs.readFileSync('./src/styles.css', 'utf8'));
rcs.selectorsLibrary.setExclude('selector-to-ignore');
```

2. Rewrite all files
2. Optimize the selectors compression (optional)

```js
rcs.optimize();
```

3. Rewrite all files

> **Note:** Do not forget to replace your CSS file
Expand Down
26 changes: 25 additions & 1 deletion __tests__/optimize/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,36 @@ beforeEach(() => {
rcs.keyframesLibrary.reset();
rcs.selectorsLibrary.setAlphabet('#abcdefghijklmnopqrstuvwxyz');
rcs.selectorsLibrary.reset();
rcs.statistics.load(undefined);
});

test('should ignore everything without selectors', () => {
test('should ignore everything without selectors but with statistics', () => {
const input = { };

rcs.mapping.load(input);
rcs.statistics.load({
ids: {
unused: [],
usageCount: {
test: 2,
},
},
classes: {
unused: [],
usageCount: {
'my-selector': 2,
},
},
keyframes: {
unused: [],
usageCount: {},
},
cssVariables: {
unused: [],
usageCount: {},
},
});

rcs.optimize();

expect(rcs.mapping.generate()).toEqual(input);
Expand Down
11 changes: 6 additions & 5 deletions lib/optimize/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const optimize = (): void => {
// sorting
// renaming into new mapping
const separateMapping = separateMappingSelectors(mapping.selectors);
const optimizedMapping: { [key in keyof ReturnType<typeof generateStatistics>]?: string[] } = {};
const optimizedMapping: { [key in keyof ReturnType<typeof generateStatistics>]: string[] } = {
ids: [],
classes: [],
keyframes: [],
cssVariables: [],
};

// optimize each library
Object.entries(separateMapping).forEach(([key, selectors]) => {
Expand All @@ -37,10 +42,6 @@ const optimize = (): void => {

// fill libraries with optimized mapping
Object.entries(optimizedMapping).forEach(([key, selectors]) => {
if (!selectors) {
return;
}

switch (key) {
case 'ids':
selectors.forEach((selector) => selectorsLibrary.set(`#${selector}`));
Expand Down

0 comments on commit 25a36dc

Please sign in to comment.