-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: summarize tokens before embedding dictionaries (#103)
- Loading branch information
Showing
8 changed files
with
118 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
import * as rollup from 'rollup'; | ||
|
||
export const isOutputChunk = ( | ||
x: rollup.OutputAsset | rollup.OutputChunk, | ||
): x is rollup.OutputChunk => 'code' in x; |
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 |
---|---|---|
@@ -1,23 +1,30 @@ | ||
import * as esifycss from 'esifycss'; | ||
import * as rollup from 'rollup'; | ||
import {isOutputChunk} from './isOutputChunk'; | ||
|
||
export const updateBundle = ( | ||
bundle: rollup.OutputBundle, | ||
): void => { | ||
for (const [, chunk] of Object.entries(bundle)) { | ||
let {code} = chunk; | ||
if (code) { | ||
const cssRanges = esifycss.extractCSSFromScript(code); | ||
const tokens = new Map<string, number>(); | ||
for (const rule of cssRanges) { | ||
for (const token of esifycss.tokenizeString(rule.css)) { | ||
tokens.set(token, (tokens.get(token) || 0) + 1); | ||
} | ||
const tokens = new Map<string, number>(); | ||
const chunks = Object.values(bundle) | ||
.filter(isOutputChunk) | ||
.map((chunk) => { | ||
const cssRanges = esifycss.extractCSSFromScript(chunk.code); | ||
const tokens = new Map<string, number>(); | ||
for (const rule of cssRanges) { | ||
for (const token of esifycss.tokenizeString(rule.css)) { | ||
tokens.set(token, (tokens.get(token) || 0) + 1); | ||
} | ||
const identifier = esifycss.createOptimizedIdentifier(tokens); | ||
code = esifycss.minifyCSSInScript(code, cssRanges, identifier); | ||
code = esifycss.setDictionary(code, identifier.idList); | ||
chunk.code = code; | ||
} | ||
return { | ||
chunk, | ||
cssRanges, | ||
}; | ||
}); | ||
const identifier = esifycss.createOptimizedIdentifier(tokens); | ||
for (const {chunk, cssRanges} of chunks) { | ||
let code = esifycss.minifyCSSInScript(chunk.code, cssRanges, identifier); | ||
code = esifycss.setDictionary(code, identifier.idList); | ||
chunk.code = code; | ||
} | ||
}; |
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,3 @@ | ||
.bar { | ||
color: red; | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import './b.css'; | ||
import * as a from './a.css'; | ||
export const result = a; |
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