-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
184 additions
and
95 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type vite from '../../../vendor/vite'; | ||
|
||
import path from 'path'; | ||
import htmlparser2 from 'htmlparser2'; | ||
|
||
// https://vitejs.dev/guide/features.html#css-pre-processors | ||
export const STYLE_EXTENSIONS = new Set(['.css', '.scss', '.sass', '.styl', '.stylus', '.less']); | ||
export const PREPROCESSOR_EXTENSIONS = new Set(['.scss', '.sass', '.styl', '.stylus', '.less']); | ||
|
||
/** find unloaded styles */ | ||
export function getStylesForID(id: string, viteServer: vite.ViteDevServer): Set<string> { | ||
const css = new Set<string>(); | ||
const { idToModuleMap } = viteServer.moduleGraph; | ||
const moduleGraph = idToModuleMap.get(id); | ||
if (!moduleGraph) return css; | ||
|
||
// recursively crawl module graph to get all style files imported by parent id | ||
function crawlCSS(entryModule: string, scanned = new Set<string>()) { | ||
const moduleName = idToModuleMap.get(entryModule); | ||
if (!moduleName) return; | ||
for (const importedModule of moduleName.importedModules) { | ||
if (!importedModule.id || scanned.has(importedModule.id)) return; | ||
const ext = path.extname(importedModule.id.toLowerCase()); | ||
|
||
if (STYLE_EXTENSIONS.has(ext)) { | ||
css.add(importedModule.id); // if style file, add to list | ||
} else { | ||
crawlCSS(importedModule.id, scanned); // otherwise, crawl file to see if it imports any CSS | ||
} | ||
scanned.add(importedModule.id); | ||
} | ||
} | ||
crawlCSS(id); | ||
|
||
return css; | ||
} | ||
|
||
/** add CSS <link> tags to HTML */ | ||
export function addLinkTagsToHTML(html: string, styles: Set<string>): string { | ||
let output = html; | ||
|
||
try { | ||
// get position of </head> | ||
let headEndPos = -1; | ||
const parser = new htmlparser2.Parser({ | ||
onclosetag(tagname) { | ||
if (tagname === 'head') { | ||
headEndPos = parser.startIndex; | ||
} | ||
}, | ||
}); | ||
parser.write(html); | ||
parser.end(); | ||
|
||
// update html | ||
if (headEndPos !== -1) { | ||
output = html.substring(0, headEndPos) + [...styles].map((href) => `<link rel="stylesheet" type="text/css" href="${href}">`).join('') + html.substring(headEndPos); | ||
} | ||
} catch (err) { | ||
// on invalid HTML, do nothing | ||
} | ||
|
||
return output; | ||
} |
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
Oops, something went wrong.