-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[build] [minify] [iife] Create custom plugin to do basic minification…
… of inline styles + Export something from adapter and global to make sure vite is wrapping inside an iife.
- Loading branch information
Showing
10 changed files
with
50 additions
and
4 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { PluginOption } from 'vite'; | ||
|
||
export const minifyInlineCSS: PluginOption = { | ||
name: 'fs-minify-inline-css', | ||
transform(code, id) { | ||
// We are only minifying TypeScript and JavaScript files. | ||
if (!/\.(ts|js)$/.test(id)) { | ||
return null; | ||
} | ||
|
||
// Regex to match CSS inside template literals starting with `/*@fs-css-minify*/`. | ||
const cssRegex = /\/\*@fs-css-minify\*\/\s*`([^`]+)`/g; | ||
|
||
// Check if the code contains any CSS to minify. | ||
if (cssRegex.test(code)) { | ||
// Replace all the CSS inside template literals with minified CSS. | ||
code = code.replace(cssRegex, (_, css) => { | ||
// Just remove the new lines and spaces. | ||
css = css.replace(/\s+/g, ' ').trim(); | ||
|
||
// Return the minified CSS inside a template literal. | ||
return `\`${css}\``; | ||
}); | ||
} | ||
|
||
return { | ||
code, | ||
map: null, | ||
}; | ||
}, | ||
}; |
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