Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.

Commit 2b994a7

Browse files
committed
feat(loader): load styles and pages
1 parent 5579029 commit 2b994a7

File tree

8 files changed

+81
-13
lines changed

8 files changed

+81
-13
lines changed

examples/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ document.title = 'hello'
33

44
const handler = async() => {
55
const backLoader = new BackLoader({
6-
scripts: ['https://code.jquery.com/jquery-3.2.1.slim.min.js'],
6+
// scripts: ['https://code.jquery.com/jquery-3.2.1.slim.min.js'],
7+
pages: ['https://google.com'],
78
})
89
backLoader.start().on(event => {
910
console.log(event)

src/core/loader.ts

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,63 @@
11
import { EventHub } from './event'
22
import { LoaderEvent } from '../types'
3+
import { $fetch, isRelativeURL } from '../utils/tools'
34

45

56
export class Loader {
67

7-
readonly baseEvent: LoaderEvent = { source: '', type: 'none', success: true, insertScripts: () => {} }
88
hub: EventHub
9+
readonly baseEvent: LoaderEvent = { source: '', type: 'none', success: true, insertScripts: () => {} }
10+
11+
static resourceReg(str: string): RegExp {
12+
return {
13+
script: /\<script\s+\S?src\=\"([^"]*)\"/g,
14+
}[str]
15+
}
16+
917
constructor(hub: EventHub) {
1018
this.hub = hub
1119
}
1220

1321
scripts(urls: string[]): void {
14-
const scriptEvent = Object.assign({}, this.baseEvent, { type: 'script' })
22+
this.loadURL(urls, 'script')
23+
}
24+
25+
styles(urls: string[]): void {
26+
this.loadURL(urls, 'style')
27+
}
28+
29+
pages(urls: string[]): void {
1530
urls.forEach(url => {
16-
fetch(url)
17-
.then(() => {
18-
this.hub.dispath('back_load_completed', Object.assign({},
19-
scriptEvent, { source: url, insertScripts: this.makeInsertScripts(url) }))
31+
$fetch(url, { mode: 'cors' })
32+
.then(html => {
33+
console.log(html)
34+
this.scripts(this.filterResources(html, 'script'))
35+
})
36+
.catch((e) => {
37+
console.log(e)
2038
})
21-
.catch(() => {
22-
this.hub.dispath('back_load_completed', Object.assign({},
23-
scriptEvent, { source: url, success: false }))
39+
})
40+
}
41+
42+
private filterResources(source: string, type: string): string[] {
43+
const reg: RegExp = Loader.resourceReg(type), arr: string[] = []
44+
let result: string[], num = 10
45+
while ((result = reg.exec(source)) && num --) {
46+
if (result[1] && !isRelativeURL(result[1])) {
47+
arr.push(result[1])
48+
}
49+
}
50+
return arr
51+
}
52+
53+
private loadURL(urls: string[], type: string): void {
54+
urls.forEach(url => {
55+
const scriptEvent = Object.assign({}, this.baseEvent, {
56+
type: type, source: url,
2457
})
58+
$fetch(url)
59+
.then(() => this.emit(scriptEvent))
60+
.catch(() => this.emit(Object.assign({}, scriptEvent, { success: false })))
2561
})
2662
}
2763

@@ -32,4 +68,12 @@ export class Loader {
3268
document.body.appendChild(s)
3369
}
3470
}
71+
72+
private emit(event: LoaderEvent): void {
73+
const next: LoaderEvent = Object.assign({}, event, event.type === 'script' ? {
74+
insertScripts: this.makeInsertScripts(event.source),
75+
} : {})
76+
this.hub.dispath('back_load_completed', next)
77+
}
78+
3579
}

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ export class BackLoader {
1515
start(): BackLoader {
1616
this.hub = new EventHub()
1717
const loader = new Loader(this.hub)
18-
const { scripts, pages } = this.options
18+
const { scripts, styles, pages } = this.options
1919

2020
scripts && loader.scripts(scripts)
21+
styles && loader.styles(styles)
22+
pages && loader.pages(pages)
2123
return this
2224
}
2325

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export type LoaderOptions = {
33
pages?: string[]
44
scripts?: string[]
5+
styles?: string[]
56
}
67

78
export type LoaderEvent = {

src/utils/check.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LoaderOptions } from '../types'
44
const regs = {
55
page: /^http|^\/\//,
66
js: /(^http|^\/\/)\S+\.js$/,
7+
css: /(^http|^\/\/)\S+\.css$/,
78
}
89

910
export const pages = (pages: string[]): boolean => {
@@ -20,11 +21,18 @@ export const scripts = (scripts: string[]): boolean => {
2021
return true
2122
}
2223

24+
export const styles = (styles: string[]): boolean => {
25+
if (!Array.isArray(styles)) return Log.options.stylesError()
26+
const unnormalLinks = styles.filter(s => !regs.css.test(s))
27+
if (unnormalLinks && unnormalLinks.length) return Log.options.stylesError()
28+
return true
29+
}
2330

2431
export const options = (ops: LoaderOptions): boolean => {
2532
const checkResults: boolean[] = []
2633
ops.pages && checkResults.push(pages(ops.pages))
2734
ops.scripts && checkResults.push(scripts(ops.scripts))
35+
ops.styles && checkResults.push(styles(ops.styles))
2836

2937
if (!checkResults.length) return Log.options.isEmpty()
3038
return !`${checkResults}`.includes('false')

src/utils/constants.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/utils/log.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export const warning = (msg: string) => {
77
export const options = {
88
pagesError: () => warning('options [pages] error.'),
99
scriptsError: () => warning('options [scripts] error.'),
10+
stylesError: () => warning('options [styles] error.'),
1011
isEmpty: () => warning('options is empty!'),
1112
}

src/utils/tools.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
export const $fetch = (url: string, init: RequestInit = {}) => {
4+
return fetch(url, Object.assign({ mode: 'no-cors' }, init))
5+
.then(r => {
6+
console.log(r)
7+
return r.text()
8+
})
9+
}
10+
11+
export const isRelativeURL = (path: string) => {
12+
return path.startsWith('./') || path.startsWith('..')
13+
}

0 commit comments

Comments
 (0)