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

Commit a3bf553

Browse files
authored
Merge pull request #4 from WittBulter/chore-build
build(pkg): v0.1.2
2 parents daae9c2 + 1ee1a27 commit a3bf553

File tree

11 files changed

+60
-27
lines changed

11 files changed

+60
-27
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
language: node_js
33

4+
cache:
5+
directories:
6+
- "node_modules"
7+
48
notifications:
59
email:
610
on_success: never
@@ -15,7 +19,6 @@ branches:
1519
before_install: git fetch --depth=1000
1620

1721
install:
18-
- npm i -g typescript webpack
1922
- npm i
2023

2124
script:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</div>
55

66
<br><br><br>
7-
### back-loader
7+
### back-loader (~ 1.2kb gzipped)
88
> a easy and steady preload lib, you can use it to load all kinds of resources.
99
1010
>use **back-loader** in your project, you can quickly preload more pictures, styles(.css) or scripts, it will make your website more fluent.

build/main/webpack.prod.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module.exports = {
1313
filename: '[name].js',
1414
},
1515

16+
target: 'web',
17+
1618
resolve: {
1719
extensions: ['.ts', '.js'],
1820
},
@@ -38,16 +40,13 @@ module.exports = {
3840
],
3941
},
4042
],
41-
42-
exprContextCritical: false,
4343
},
4444

4545
plugins: [
4646
new webpack.NoEmitOnErrorsPlugin(),
4747

4848
new webpack.optimize.UglifyJsPlugin({
49-
mangle: { keep_fnames: true },
50-
sourceMap: false,
49+
compress: { warnings: false },
5150
}),
5251
],
5352
}

examples/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const handler = async() => {
55
const backLoader = new BackLoader({
66
// scripts: ['https://code.jquery.com/jquery-3.2.1.slim.min.js'],
77
// pages: ['https://google.com'],
8-
images: ['https://cn.bing.com/az/hprichbg/rb/BarHarborCave_ZH-CN8055769470_1920x1080.jpg'],
8+
images: ['https://www.bing.com/az/hprichbg/rb/TadamiTrain_ROW14602114613_1920x1080.jpg'],
99
})
1010
backLoader.start().on(event => {
1111
console.log(event)

package-lock.json

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"name": "back-loader",
3-
"version": "0.1.0",
4-
"description": "",
5-
"main": "release/index.d.ts",
6-
"unpkg": "release/back-loader.js",
3+
"version": "0.1.3",
4+
"description": "a easy and steady preload lib",
5+
"main": "release/index",
6+
"unpkg": "release/index.js",
77
"author": "WittBulter(nanazuimeng123@gmail.com)",
88
"homepage": "https://github.com/DhyanaChina/back-loader#readme",
99
"license": "MIT",
1010
"scripts": {
1111
"test": "./node_modules/.bin/mocha",
12-
"cover:local": "./node_modules/.bin/nyc mocha && open coverage/lcov-report/index.html",
13-
"cover:hook": "./node_modules/.bin/nyc mocha && cat ./coverage/lcov.info | coveralls",
12+
"cover:local": "./node_modules/.bin/nyc mocha;open coverage/lcov-report/index.html",
13+
"cover:hook": "./node_modules/.bin/nyc mocha;cat ./coverage/lcov.info | coveralls",
1414
"ex": "tsc ./build/webpack.dev.ts && env DEBUG=true webpack-dev-server --config build/webpack.dev.js --inline --progress --port 1338 && npm run clean:tsc",
1515
"ex:build": "tsc ./build/webpack.prod.ts && rm -rf dist && webpack --config build/webpack.prod.js && npm run clean:tsc",
16-
"release": "tsc ./build/main/webpack.prod.ts && rm -rf release && webpack --config build/main/webpack.prod.js && npm run clean:tsc",
16+
"release": "tsc -p ./tsconfig.json",
1717
"clean:tsc": "find build -name '*.js' | xargs rm -f",
1818
"clean:cover": "rm -rf coverage .nyc_output",
1919
"clean:build": "rm -rf dist release",
@@ -42,6 +42,7 @@
4242
"@types/html-webpack-plugin": "^2.30.1",
4343
"@types/jsdom": "^11.0.4",
4444
"@types/mocha": "^2.2.46",
45+
"@types/parse5": "^3.0.0",
4546
"@types/webpack": "^3.8.2",
4647
"@types/webpack-merge": "^4.1.1",
4748
"awesome-typescript-loader": "^3.4.1",

src/core/event.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ export class EventHub {
3939
// just remove one
4040
if (done) return this.removeOne(eventType, done)
4141
// remove this type
42-
this.eventRecords
43-
.filter(re => re.type === eventType)
44-
.forEach(re => this.source.removeEventListener(re.type, re.handle))
42+
this.eventRecords = this.eventRecords
43+
.map(re => {
44+
if (re.type !== eventType) return re
45+
this.source.removeEventListener(re.type, re.handle)
46+
return null
47+
})
48+
.filter(r => r)
4549
}
4650

4751
removeAll(): void {
4852
this.eventRecords
49-
.forEach(re => this.source.removeEventListener(re.type, re.handle))
53+
.forEach(re => this.source.removeEventListener(re.type, re.handle))
54+
this.eventRecords = []
5055
}
5156

5257
private removeOne(eventType: string, done?: EventHubListener): void {
53-
const record: EventRecord = this.eventRecords.find(record => {
58+
const index: number = this.eventRecords.findIndex(record => {
5459
return record.type === eventType && record.done === done
5560
})
56-
if (!record) return
57-
this.source.removeEventListener(eventType, record.handle)
61+
if (index < 0) return
62+
this.source.removeEventListener(eventType, this.eventRecords[index].handle)
63+
this.eventRecords.splice(index, 1)
5864
}
5965

6066
}

src/core/loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ export class Loader {
2222

2323
pages(urls: string[]): void {
2424
urls.forEach(url => {
25-
$fetch(url, { mode: 'cors' })
25+
$fetch(url, { mode: 'no-cors' })
2626
.then(html => {
2727
this.scripts(filterResources(html, 'script'))
2828
this.styles(filterResources(html, 'style'))
2929
})
30-
.catch(() => {
30+
.catch((e) => {
3131
this.emit(Object.assign({}, this.baseEvent, {
3232
type: 'page', source: url, success: false,
3333
}))

src/utils/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// resource load use no-cors mode default
22
// html load must use cors
33
export const $fetch = (url: string, init: RequestInit = {}) => {
4-
return fetch(url, Object.assign({ mode: 'no-cors' }, init))
4+
return fetch(url, Object.assign({ mode: 'cors', cache: 'reload' }, init))
55
.then(r => r.text())
66
}
77

test/core/event.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,19 @@ describe('Core function test', () => {
2323
hub.dispath('test_listen_2', 'once')
2424
})
2525

26-
})
26+
it('should nerve receive any notice', done => {
27+
hub.listen('test_listen_3', (e, d) => expect.fail(d, null))
28+
hub.removeAll()
29+
hub.dispath('test_listen_3', 'once')
30+
setTimeout(done, 100)
31+
})
32+
33+
it('get an event and reject an event', done => {
34+
hub.listen('test_listen_4', () => process.exit(1))
35+
hub.listen('test_listen_5', () => done())
36+
hub.remove('test_listen_4')
37+
hub.dispath('test_listen_4', 'once')
38+
hub.dispath('test_listen_5', 'once')
39+
})
40+
41+
})

0 commit comments

Comments
 (0)