Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 06c5afa

Browse files
committed
build: refine source code structure to reach full typescript
1 parent a49fa3d commit 06c5afa

28 files changed

+189
-159
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
lib
33
.temp
44
types
5+
src/**/*.js
56

67
### Code ###
78
# Visual Studio Code - https://code.visualstudio.com/

examples/blog/.vuepress/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
title: `ULIVZ`,
33
plugins: [
4-
[require('../../../lib'), {
4+
[require('../../../lib/node'), {
55
directories: [
66
{
77
// Unique ID of current classification

examples/blog/.vuepress/theme/layouts/Layout.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
<router-link v-if="$pagination.hasPrev" :to="$pagination.prevLink">Prev</router-link>
1010
<router-link v-if="$pagination.hasNext" :to="$pagination.nextLink">Next</router-link>
1111
</div>
12-
13-
<Pagination />
12+
13+
<Pagination/>
1414
</div>
1515
</template>
1616

1717
<script>
18-
import Pagination from '../../../../../src/components/Pagination'
18+
import { Pagination } from '../../../../../lib/client/components.js'
1919
2020
export default {
2121
components: { Pagination },
2222
created() {
2323
console.log(this.$route.meta.pid)
2424
console.log(this.$currentTag)
25-
}
25+
},
2626
}
2727
</script>

examples/directory-classifier/.vuepress/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
title: `ULIVZ`,
33
plugins: [
4-
[require('../../../lib'), {
4+
[require('../../../lib/node'), {
55
directories: [
66
{
77
// Unique ID of current classification

examples/frontmatter-classifier/.vuepress/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
title: `ULIVZ`,
33
plugins: [
4-
[require('../../../lib'), {
4+
[require('../../../lib/node'), {
55
frontmatters: [
66
{
77
// Unique ID of current classification

package.json

+17-35
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
"description": "Offical blog plugin for VuePress",
55
"scripts": {
66
"lint": "xo",
7-
"dev": "npm run cpc && tsc -skipLibCheck --watch",
8-
"cpc": "cp -r src/client lib",
9-
"build": "tsc -skipLibCheck && npm run cpc",
7+
"dev": "concurrently \"yarn dev:client\" \"yarn dev:node\" \"yarn build:components\"",
8+
"build": "concurrently \"yarn build:client\" \"yarn build:node\" \"yarn build:components\"",
9+
"dev:client": "tsc -p tsconfig.client.json --watch",
10+
"dev:node": "tsc -p tsconfig.node.json --watch",
11+
"build:client": "tsc -p tsconfig.client.json",
12+
"build:node": "tsc -p tsconfig.node.json",
13+
"build:components": "mkdir -p lib/client/components && cp -r src/client/components lib/client",
1014
"dev:docs": "vuepress dev docs --temp docs/.temp",
1115
"build:docs": "vuepress build docs --temp docs/.temp",
1216
"example": "node examples/launch.js",
1317
"prepare": "npm run build",
1418
"prepublishOnly": "npm run build && conventional-changelog -p angular -r 2 -i CHANGELOG.md -s"
1519
},
16-
"main": "lib/index.js",
20+
"main": "lib/node/index.js",
1721
"files": [
1822
"lib",
1923
"docs",
@@ -25,24 +29,17 @@
2529
},
2630
"author": "ULIVZ <[email protected]>",
2731
"license": "MIT",
32+
"dependencies": {
33+
"vuejs-paginate": "^2.1.0"
34+
},
2835
"devDependencies": {
36+
"concurrently": "^4.1.0",
2937
"conventional-changelog-cli": "^2.0.1",
30-
"eslint-config-prettier": "^3.3.0",
31-
"eslint-config-rem": "^4.0.0",
32-
"eslint-config-xo-typescript": "^0.3.0",
33-
"eslint-plugin-prettier": "^3.0.0",
34-
"eslint-plugin-typescript": "^0.14.0",
35-
"husky": "^1.2.0",
3638
"inquirer": "^6.3.1",
37-
"lint-staged": "^8.1.0",
3839
"nodemon": "^1.18.7",
39-
"prettier": "^1.15.2",
4040
"ts-node": "^7.0.1",
41-
"typescript": "^3.1.4",
42-
"typescript-eslint-parser": "^21.0.2",
43-
"vuejs-paginate": "^2.1.0",
44-
"vuepress": "^1.0.0",
45-
"xo": "^0.23.0"
41+
"typescript": "3.1.4",
42+
"vuepress": "^1.0.0"
4643
},
4744
"xo": {
4845
"extends": [
@@ -63,24 +60,9 @@
6360
"typescript/no-var-requires": "off",
6461
"no-implicit-globals": "off",
6562
"import/no-unresolved": "off",
66-
"import/no-extraneous-dependencies": "off"
67-
}
68-
},
69-
"husky": {
70-
"hooks": {
71-
"pre-commit": "lint-staged"
72-
}
73-
},
74-
"lint-staged": {
75-
"linters": {
76-
"*.{ts,js}": [
77-
"xo --fix",
78-
"git add"
79-
],
80-
"*.{json,md}": [
81-
"prettier --write",
82-
"git add"
83-
]
63+
"import/no-extraneous-dependencies": "off",
64+
"typescript/no-use-before-define": "off",
65+
"typescript/no-type-alias": "off"
8466
}
8567
},
8668
"publishConfig": {

src/client/classification.js renamed to src/client/classification.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// @ts-ignore
12
import { findPageByKey } from '@app/util'
2-
import frontmatterClassifiedPageMap from '@dynamic/vuepress_blog/frontmatterClassified'
3+
// @ts-ignore
4+
import frontmatterClassifiedMap from '@dynamic/vuepress_blog/frontmatterClassified'
5+
import { VuePressPage } from '../types/VuePress'
36

47
class Classifiable {
8+
private _metaMap: any
9+
510
constructor(metaMap, pages) {
611
this._metaMap = Object.assign({}, metaMap)
712
Object.keys(this._metaMap).forEach(name => {
@@ -27,7 +32,12 @@ class Classifiable {
2732
}
2833

2934
toArray() {
30-
const tags = []
35+
const tags: Array<{
36+
name: string
37+
pages: VuePressPage[]
38+
path: string
39+
}> = []
40+
3141
Object.keys(this._metaMap).forEach(name => {
3242
const { pages, path } = this._metaMap[name]
3343
tags.push({ name, pages, path })
@@ -41,18 +51,22 @@ class Classifiable {
4151
}
4252

4353
export default ({ Vue }) => {
44-
const computed = Object.keys(frontmatterClassifiedPageMap)
54+
const computed = Object.keys(frontmatterClassifiedMap)
4555
.map(classifiedType => {
46-
const map = frontmatterClassifiedPageMap[classifiedType]
56+
const map = frontmatterClassifiedMap[classifiedType]
4757
const helperName = `$${classifiedType}`
4858
return {
4959
[helperName]() {
60+
// @ts-ignore
5061
const { pages } = this.$site
5162
const classified = new Classifiable(map, pages)
5263
return classified
5364
},
54-
[`$current${classifiedType.charAt(0).toUpperCase() + classifiedType.slice(1)}`]() {
65+
[`$current${classifiedType.charAt(0).toUpperCase() +
66+
classifiedType.slice(1)}`]() {
67+
// @ts-ignore
5568
const tagName = this.$route.meta.id
69+
// @ts-ignore
5670
return this[helperName].getItemByName(tagName)
5771
},
5872
}

src/client/components.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @ts-ignore
2+
import Pagination from './components/Pagination.vue'
3+
// @ts-ignore
4+
import SimplePagination from './components/SimplePagination.vue'
5+
6+
export { Pagination, SimplePagination }

src/components/Pagination.vue renamed to src/client/components/Pagination.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212

1313
<script>
1414
import Paginate from 'vuejs-paginate'
15-
15+
1616
export default {
1717
data() {
1818
return {
1919
page: 0,
2020
}
2121
},
2222
components: { Paginate },
23+
created() {
24+
this.page = this.$pagination.paginationIndex + 1
25+
},
2326
methods: {
2427
clickCallback(pageNum) {
2528
const link = this.$pagination.getSpecificPageLink(pageNum - 1)
26-
console.log(link)
2729
this.$router.push(link)
2830
},
2931
},

src/client/init.js

-2
This file was deleted.

src/client/pagination.js renamed to src/client/pagination.ts

+17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import Vue from 'vue'
2+
// @ts-ignore
23
import paginations from '@dynamic/vuepress_blog/paginations'
34
import _debug from 'debug'
45

56
const debug = _debug('plugin-blog:pagination')
67

78
class Pagination {
9+
public paginationIndex: number
10+
11+
public _paginationPages: any
12+
13+
public _currentPage: any
14+
15+
public _matchedPages: any
16+
17+
public _indexPage: string
18+
819
constructor(pagination, pages, route) {
920
debug(pagination)
1021
const { pages: paginationPages } = pagination
@@ -71,11 +82,14 @@ class Pagination {
7182
}
7283

7384
class PaginationGateway {
85+
private paginations: any
86+
7487
constructor(paginations) {
7588
this.paginations = paginations
7689
}
7790

7891
get pages() {
92+
// @ts-ignore
7993
return Vue.$vuepress.$get('siteData').pages
8094
}
8195

@@ -96,15 +110,18 @@ export default ({ Vue }) => {
96110
methods: {
97111
$getPagination(pid, id) {
98112
id = id || pid
113+
// @ts-ignore
99114
return gateway.getPagination(pid, id, this.$route)
100115
},
101116
},
102117
computed: {
103118
$pagination() {
119+
// @ts-ignore
104120
if (!this.$route.meta.pid || !this.$route.meta.id) {
105121
return {}
106122
}
107123

124+
// @ts-ignore
108125
return this.$getPagination(this.$route.meta.pid, this.$route.meta.id)
109126
},
110127
},

src/interface/Classifier.ts

-9
This file was deleted.

src/interface/ExtraPages.ts

-5
This file was deleted.

src/handleOptions.ts renamed to src/node/handleOptions.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BlogPluginOptions } from './interface/Options'
22
import { ExtraPage } from './interface/ExtraPages'
33
import { PageEnhancer } from './interface/PageEnhancer'
4-
import { AppContext } from './interface/VuePress'
4+
import { VuePressContext } from './interface/VuePress'
55
import { InternalPagination, PaginationConfig } from './interface/Pagination'
66
import { FrontmatterClassificationPage } from './interface/Frontmatter'
77
import {
@@ -19,7 +19,10 @@ import { ClassifierTypeEnum } from './interface/Classifier'
1919
* @returns {*}
2020
*/
2121

22-
export function handleOptions(options: BlogPluginOptions, ctx: AppContext) {
22+
export function handleOptions(
23+
options: BlogPluginOptions,
24+
ctx: VuePressContext,
25+
) {
2326
const { directories = [], frontmatters = [] } = options
2427

2528
const pageEnhancers: PageEnhancer[] = []

src/index.ts renamed to src/node/index.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as path from 'path'
22
import { handleOptions } from './handleOptions'
3-
import { registerPagination } from './pagination'
3+
import { registerPaginations } from './pagination'
44
import { BlogPluginOptions } from './interface/Options'
5-
import { AppContext, Page } from './interface/VuePress'
65
import { logPages, resolvePaginationConfig } from './util'
76
import { ClassifierTypeEnum, DefaultLayoutEnum } from './interface/Classifier'
7+
import { VuePressContext, VuePressPage } from './interface/VuePress'
88

9-
function injectExtraAPI(ctx: AppContext) {
9+
function injectExtraAPI(ctx: VuePressContext) {
1010
const { layoutComponentMap } = ctx.themeAPI
1111

1212
/**
@@ -22,7 +22,7 @@ function injectExtraAPI(ctx: AppContext) {
2222
}
2323
}
2424

25-
module.exports = (options: BlogPluginOptions, ctx: AppContext) => {
25+
module.exports = (options: BlogPluginOptions, ctx: VuePressContext) => {
2626
injectExtraAPI(ctx)
2727

2828
const {
@@ -38,7 +38,7 @@ module.exports = (options: BlogPluginOptions, ctx: AppContext) => {
3838
/**
3939
* 1. Execute `pageEnhancers` generated in handleOptions
4040
*/
41-
extendPageData(pageCtx: Page) {
41+
extendPageData(pageCtx: VuePressPage) {
4242
const { frontmatter: rawFrontmatter } = pageCtx
4343

4444
pageEnhancers.forEach(({ when, data = {}, frontmatter = {} }) => {
@@ -143,14 +143,14 @@ module.exports = (options: BlogPluginOptions, ctx: AppContext) => {
143143
logPages(`Automatically Added Index Pages`, allExtraPages)
144144

145145
await Promise.all(allExtraPages.map(async page => ctx.addPage(page)))
146-
await registerPagination(paginations, ctx)
146+
await registerPaginations(paginations, ctx)
147147
},
148148

149149
/**
150150
* Generate tag and category metadata.
151151
*/
152152
async clientDynamicModules() {
153-
const frontmatterClassifiedPageMap = ctx.frontmatterClassificationPages.reduce(
153+
const frontmatterClassifiedMap = ctx.frontmatterClassificationPages.reduce(
154154
(map, page) => {
155155
map[page.id] = page.map
156156
return map
@@ -164,7 +164,7 @@ module.exports = (options: BlogPluginOptions, ctx: AppContext) => {
164164
{
165165
name: `${PREFIX}/frontmatterClassified.js`,
166166
content: `export default ${JSON.stringify(
167-
frontmatterClassifiedPageMap,
167+
frontmatterClassifiedMap,
168168
null,
169169
2,
170170
)}`,
@@ -193,8 +193,8 @@ export default ${serializePaginations(ctx.serializedPaginations, [
193193
},
194194

195195
enhanceAppFiles: [
196-
path.resolve(__dirname, 'client/classification.js'),
197-
path.resolve(__dirname, 'client/pagination.js'),
196+
path.resolve(__dirname, '../client/classification.js'),
197+
path.resolve(__dirname, '../client/pagination.js'),
198198
],
199199
}
200200
}

0 commit comments

Comments
 (0)