-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#250 Creates a new `minifyHTMLLiteralsPlugin` for rollup to handle dynamic tags effectively, reducing warnings and increasing compression efficiency during bundling. Currently this only affects the button, but might affect other elements in future, too.
- Loading branch information
1 parent
d449c7b
commit 1730fe8
Showing
6 changed files
with
63 additions
and
80 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
packages/components/scripts/rollup-plugin-minify-html-literals.ts
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,50 @@ | ||
/** | ||
* The `minifyHTMLLiteralsPlugin` function creates a Rollup plugin that minifies | ||
* HTML literals in your source code during the Rollup bundling process. | ||
* | ||
* This plugin: | ||
* 1. Iterates through each module in the input bundle. | ||
* 2. Prepares the module code for minification by replacing certain dynamic tags | ||
* that would otherwise confuse the minification process. | ||
* 3. Minifies the HTML literals in the prepared code using the minify-html-literals library. | ||
* 4. If minification was successful, it reverses the replacements made during preparation | ||
* to restore the original dynamic tags, and the minified code replaces the original code. | ||
* 5. If minification failed, it leaves the original code untouched. | ||
*/ | ||
|
||
import { minifyHTMLLiterals } from 'minify-html-literals'; | ||
|
||
export default function minifyHTMLLiteralsPlugin() { | ||
return { | ||
name: 'minify-html-literals', | ||
transform(code: string, id: string) { | ||
// This function prepares code by replacing certain dynamic tags. | ||
// If reverse is true, the function will reverse the replacements. | ||
const prepareCode = (codeToModify: string, reverse = false) => { | ||
// eslint-disable-next-line no-template-curly-in-string | ||
const dynamicTags = [{ from: '${tag}', to: 'tag-to-be-replaced' }]; | ||
|
||
// Replace all occurrences of each dynamic tag and return the modified code. | ||
dynamicTags.forEach(dynamicTag => { | ||
const from = reverse ? dynamicTag.to : dynamicTag.from; | ||
const to = reverse ? dynamicTag.from : dynamicTag.to; | ||
|
||
codeToModify = codeToModify.replaceAll(`<${from}`, `<${to}`); | ||
codeToModify = codeToModify.replaceAll(`</${from}`, `</${to}`); | ||
}); | ||
|
||
return codeToModify; // return the modified code | ||
}; | ||
|
||
// Prepare the code for minification by replacing certain dynamic tags. | ||
const preparedCode = prepareCode(code, false); | ||
|
||
// Minify the HTML literals in the prepared code. | ||
const minified = minifyHTMLLiterals(preparedCode, { fileName: id }); | ||
|
||
// If minification was successful, prepare the minified code by reversing the replacements | ||
// made earlier. If minification failed (i.e., minified is null), return the original code. | ||
return minified ? prepareCode(minified.code, true) : code; // Check if minified is null or not | ||
} | ||
}; | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.