Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release: stark-module-1.5.1 #507

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: 🐛 fast re-render will cause css flashing (#506)
  • Loading branch information
maoxiaoke authored Jan 5, 2022
commit 75745b9277802675ac4b73b04dbfa028cc420172
51 changes: 26 additions & 25 deletions packages/icestark-module/src/modules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ export interface StarkModule {

export type ISandbox = boolean | SandboxProps | SandboxConstructor;

interface CssStorage {
[key: string]: {
count: number;
task: Promise<boolean>;
};
}

let globalModules = [];
let importModules = {};
// store css link
const cssStorage = {};
const cssStorage: CssStorage = {};

const IS_CSS_REGEX = /\.css(\?((?!\.js$).)+)?$/;
export const moduleLoader = new ModuleLoader();
Expand Down Expand Up @@ -73,23 +80,10 @@ export const registerModules = (modules: StarkModule[]) => {
modules.forEach((m) => registerModule(m));
};

// if css link already loaded, record load count
const filterAppendCSS = (cssList: string[]) => {
return (cssList || []).filter((cssLink) => {
if (cssStorage[cssLink]) {
cssStorage[cssLink] += 1;
return false;
} else {
cssStorage[cssLink] = 1;
return true;
}
});
};

const filterRemoveCSS = (cssList: string[]) => {
return (cssList || []).filter((cssLink) => {
if (cssStorage[cssLink] > 1) {
cssStorage[cssLink] -= 1;
if (cssStorage[cssLink].count > 1) {
cssStorage[cssLink].count -= 1;
return false;
} else {
delete cssStorage[cssLink];
Expand Down Expand Up @@ -139,8 +133,8 @@ export function appendCSS(
name: string,
url: string,
root: HTMLElement | ShadowRoot = document.getElementsByTagName('head')[0],
): Promise<void> {
return new Promise<void>((resolve, reject) => {
): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
if (!root) reject(new Error(`no root element for css assert: ${url}`));

const element: HTMLLinkElement = document.createElement('link');
Expand All @@ -152,11 +146,11 @@ export function appendCSS(
'error',
() => {
console.error(`css asset loaded error: ${url}`);
return resolve();
return resolve(true);
},
false,
);
element.addEventListener('load', () => resolve(), false);
element.addEventListener('load', () => resolve(false), false);

root.appendChild(element);
});
Expand Down Expand Up @@ -245,11 +239,18 @@ export const loadModule = async (targetModule: StarkModule, sandbox?: ISandbox)
console.error('[icestark module] Please export mount/unmount function');
}

// append css before mount module
const cssList = filterAppendCSS(moduleCSS);
if (cssList.length) {
await Promise.all(cssList.map((css: string) => appendCSS(name, css)));
}
await Promise.all(
moduleCSS.map((css) => {
if (!cssStorage[css]) {
cssStorage[css] = {
count: 1,
task: appendCSS(name, css),
};
}
cssStorage[css].count += 1;
return cssStorage[css].task;
}),
);

if (typeof moduleInfo.component !== 'undefined') {
console.warn('[icestark module] The export function name called component is conflict, please change it or it will be ignored.');
Expand Down