Skip to content

Commit

Permalink
fix: optimize css loader (antvis#3156)
Browse files Browse the repository at this point in the history
  • Loading branch information
NewByVector authored Jan 18, 2023
1 parent 1a5f165 commit 9c48ad8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/x6-common/src/css-loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Platform } from '../platform'

interface CssModule {
name: string
loadTimes: number
styleElement: HTMLStyleElement | null
}

Expand All @@ -10,7 +11,10 @@ const cssModules: CssModule[] = []
export function ensure(name: string, content: string) {
const cssModule = cssModules.find((m) => m.name === name)
if (cssModule) {
return
cssModule.loadTimes += 1
if (cssModule.loadTimes > 1) {
return
}
}

if (!Platform.isApplyingHMR()) {
Expand All @@ -25,6 +29,7 @@ export function ensure(name: string, content: string) {

cssModules.push({
name,
loadTimes: 1,
styleElement,
})
}
Expand All @@ -34,7 +39,13 @@ export function clean(name: string) {
const index = cssModules.findIndex((m) => m.name === name)

if (index > -1) {
let styleElement = cssModules[index].styleElement
const cssModule = cssModules[index]
cssModule.loadTimes -= 1
if (cssModule.loadTimes > 0) {
return
}

let styleElement = cssModule.styleElement
if (styleElement && styleElement.parentNode) {
styleElement.parentNode.removeChild(styleElement)
}
Expand Down

0 comments on commit 9c48ad8

Please sign in to comment.