-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(theme:preloader): fix loading order issues (#1691)
- Loading branch information
Showing
11 changed files
with
204 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,5 @@ node_modules/ | |
|
||
# yarn v2 | ||
.yarn | ||
|
||
**/src/index.html |
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 |
---|---|---|
@@ -1,25 +1,25 @@ | ||
import type { NzSafeAny } from 'ng-zorro-antd/core/types'; | ||
|
||
export function preloaderFinished(): void { | ||
const body = document.querySelector('body')!; | ||
const preloader = document.querySelector('.preloader')!; | ||
import { DOCUMENT } from '@angular/common'; | ||
import { inject } from '@angular/core'; | ||
|
||
export function stepPreloader(): () => void { | ||
const doc: Document = inject(DOCUMENT); | ||
const body = doc.querySelector('body')!; | ||
body.style.overflow = 'hidden'; | ||
let done = false; | ||
|
||
function remove(): void { | ||
// preloader value null when running --hmr | ||
if (!preloader) return; | ||
preloader.addEventListener('transitionend', () => { | ||
preloader.className = 'preloader-hidden'; | ||
}); | ||
return () => { | ||
if (done) return; | ||
|
||
preloader.className += ' preloader-hidden-add preloader-hidden-add-active'; | ||
} | ||
done = true; | ||
const preloader = doc.querySelector<HTMLElement>('.preloader'); | ||
if (preloader == null) return; | ||
|
||
(window as NzSafeAny).appBootstrap = () => { | ||
setTimeout(() => { | ||
remove(); | ||
body.style.overflow = ''; | ||
}, 100); | ||
const CLS = 'preloader-hidden'; | ||
preloader.addEventListener('transitionend', () => { | ||
preloader.className = CLS; | ||
}); | ||
preloader.className += ` ${CLS}-add ${CLS}-add-active`; | ||
const body = doc.querySelector<HTMLBodyElement>('body')!; | ||
body.style.overflow = ''; | ||
}; | ||
} |
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,73 @@ | ||
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; | ||
|
||
import { DEFAULT_WORKSPACE_PATH, logWarn, readJSON } from '../../../utils'; | ||
|
||
export function updatePreloader(): Rule { | ||
return (tree: Tree, context: SchematicContext) => { | ||
addESLintIgnore(tree); | ||
|
||
const angularJson = readJSON(tree, DEFAULT_WORKSPACE_PATH); | ||
const projectNames = Object.keys(angularJson.projects); | ||
for (const name of projectNames) { | ||
const sourceRoot = angularJson.projects[name].sourceRoot; | ||
fixIndexHtml(tree, name, sourceRoot, context); | ||
run(tree, name, sourceRoot, context); | ||
} | ||
}; | ||
} | ||
|
||
function addESLintIgnore(tree: Tree): void { | ||
const filePath = '/.eslintignore'; | ||
if (!tree.exists(filePath)) return; | ||
const content = tree.readText(filePath); | ||
if (!content.includes('**/src/index.html')) { | ||
tree.overwrite(filePath, `${content}\n**/src/index.html`); | ||
} | ||
} | ||
|
||
function fixIndexHtml(tree: Tree, _: string, sourceRoot: string, __: SchematicContext): void { | ||
const indexPath = `${sourceRoot}/index.html`; | ||
if (!tree.exists(indexPath)) return; | ||
|
||
let indexContent = tree.readText(indexPath); | ||
|
||
const selfClose = '<app-root />'; | ||
if (!indexContent.includes(selfClose)) return; | ||
|
||
tree.overwrite(indexPath, indexContent.replace(selfClose, '<app-root></app-root>')); | ||
} | ||
|
||
function run(tree: Tree, name: string, sourceRoot: string, context: SchematicContext): void { | ||
// main.ts | ||
const mainPath = `${sourceRoot}/main.ts`; | ||
if (!tree.exists(mainPath)) return; | ||
|
||
let mainContent = tree.readText(mainPath); | ||
['preloaderFinished();'].forEach(item => { | ||
if (mainContent.includes(item)) mainContent = mainContent.replace(item, ''); | ||
}); | ||
tree.overwrite(mainPath, mainContent); | ||
|
||
// app | ||
const appPath = `${sourceRoot}/app/app.component.ts`; | ||
if (!tree.exists(appPath)) return; | ||
const appContent = tree.readText(appPath); | ||
if (appContent.includes(', stepPreloader')) return; | ||
|
||
const appContentLines = appContent.split('\n'); | ||
const importIndex = appContentLines.findIndex(line => line.includes(', VERSION as VERSION_ALAIN')); | ||
const addIndex = appContentLines.findIndex(line => line.includes('export class AppComponent')); | ||
const callDoneIndex = appContentLines.findIndex(line => line.includes('if (ev instanceof NavigationEnd) {')); | ||
if (importIndex === -1 || addIndex === -1 || callDoneIndex === -1) return; | ||
|
||
appContentLines[importIndex] = appContentLines[importIndex].replace( | ||
', VERSION as VERSION_ALAIN', | ||
', VERSION as VERSION_ALAIN, stepPreloader' | ||
); | ||
appContentLines.splice(addIndex + 1, 0, 'private donePreloader = stepPreloader();'); | ||
appContentLines.splice(callDoneIndex + 2, 0, 'this.donePreloader();'); | ||
|
||
tree.overwrite(appPath, appContentLines.join('\n')); | ||
|
||
logWarn(context, `Upgrade preloader in ${name} project`); | ||
} |
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.