Skip to content

Commit

Permalink
fix(cli): modify the logic of create route
Browse files Browse the repository at this point in the history
  • Loading branch information
BeADre committed Jul 19, 2021
1 parent d26c420 commit d60158a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ packages/varlet-cli/lib/compiler
packages/varlet-cli/lib/config
packages/varlet-cli/lib/shared
packages/varlet-cli/lib/index.d.ts
packages/varlet-cli/site/mobile/routes.ts
packages/varlet-cli/site/pc/routes.ts
packages/varlet-cli/site/mobile/*.routes.ts
packages/varlet-cli/site/pc/*.routes.ts
packages/varlet-cli/site/site.config.json

packages/varlet-ui/site
Expand Down
11 changes: 8 additions & 3 deletions packages/varlet-cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { getDevConfig } from '../config/webpack.dev.config'
import { getPort } from 'portfinder'
import { buildMobileSiteRoutes, buildPcSiteRoutes } from '../compiler/compileRoutes'
import { ensureDirSync } from 'fs-extra'
import { SRC_DIR } from '../shared/constant'
import { resolve } from 'path'
import { SRC_DIR, SITE_PC, SITE_MOBILE } from '../shared/constant'

let mobileRouteId: string
let pcRouteId: string

export function runDevServer(port: number, config: any) {
const { host } = config.devServer
config.devServer.port = port
config.resolve.alias['@pc-routes'] = resolve(SITE_PC, `./${pcRouteId}.routes.ts`)
config.resolve.alias['@mobile-routes'] = resolve(SITE_MOBILE, `./${mobileRouteId}.routes.ts`)
const server = new WebpackDevServer(webpack(config), config.devServer)

;(server as any).showStatus = function () {}
Expand All @@ -26,8 +32,7 @@ export function runDevServer(port: number, config: any) {

export async function dev() {
ensureDirSync(SRC_DIR)

await Promise.all([buildMobileSiteRoutes(), buildPcSiteRoutes()])
;[mobileRouteId, pcRouteId] = await Promise.all([buildMobileSiteRoutes(), buildPcSiteRoutes()])

const config = getDevConfig()
const { port } = config.devServer
Expand Down
35 changes: 23 additions & 12 deletions packages/varlet-cli/src/compiler/compileRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import slash from 'slash'
import hash from 'hash-sum'
import {
DOCS_DIR_NAME,
EXAMPLE_DIR_INDEX,
EXAMPLE_DIR_NAME,
ROOT_DOCS_DIR,
SITE_MOBILE_ROUTES,
SITE_PC_ROUTES,
SRC_DIR,
SITE_PC,
SITE_MOBILE,
} from '../shared/constant'
import { pathExistsSync, readdir, readdirSync, writeFile } from 'fs-extra'
import { resolve } from 'path'
Expand Down Expand Up @@ -77,7 +78,7 @@ export async function findRootDocsPaths(): Promise<string[]> {
return dir.filter(existPath).map(slashPath)
}

export async function buildMobileSiteRoutes() {
export async function buildMobileSiteRoutes(): Promise<string> {
const examplePaths: string[] = await findExamplePaths()

const routes = examplePaths.map(
Expand All @@ -89,15 +90,20 @@ export async function buildMobileSiteRoutes() {
`
)

await writeFile(
SITE_MOBILE_ROUTES,
`export default [\
const source = `export default [\
${routes.join(',')}
]`
)

const mobileRouteId = hash(source)

const path = resolve(SITE_MOBILE, `./${mobileRouteId}.routes.ts`)

await writeFile(path, source)

return mobileRouteId
}

export async function buildPcSiteRoutes() {
export async function buildPcSiteRoutes(): Promise<string> {
const [componentDocsPaths, rootDocsPaths] = await Promise.all([findComponentDocsPaths(), findRootDocsPaths()])

const componentDocsRoutes = componentDocsPaths.map(
Expand All @@ -120,10 +126,15 @@ export async function buildPcSiteRoutes() {
`
)

await writeFile(
SITE_PC_ROUTES,
`export default [\
const source = `export default [\
${[...componentDocsRoutes, rootDocsRoutes].join(',')}
]`
)

const pcRouteId = hash(source)

const path = resolve(SITE_PC, `./${pcRouteId}.routes.ts`)

await writeFile(path, source)

return pcRouteId
}
13 changes: 1 addition & 12 deletions packages/varlet-cli/src/config/webpack.base.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
EXTENSIONS,
POSTCSS_CONFIG,
SITE_CONFIG,
SITE_MOBILE_MAIN,
SITE_MOBILE_ROUTES,
SITE_PC_MAIN,
SITE_PC_ROUTES,
TS_CONFIG,
} from '../shared/constant'
import { EXTENSIONS, POSTCSS_CONFIG, SITE_CONFIG, SITE_MOBILE_MAIN, SITE_PC_MAIN, TS_CONFIG } from '../shared/constant'
import { ForkTsCheckerWebpackPlugin } from 'fork-ts-checker-webpack-plugin/lib/ForkTsCheckerWebpackPlugin'
import { VueLoaderPlugin } from 'vue-loader'
import { pathExistsSync } from 'fs-extra'
Expand Down Expand Up @@ -56,8 +47,6 @@ export const BASE_CONFIG = {
extensions: EXTENSIONS,
alias: {
'@config': SITE_CONFIG,
'@pc-routes': SITE_PC_ROUTES,
'@mobile-routes': SITE_MOBILE_ROUTES,
},
},
module: {
Expand Down
4 changes: 2 additions & 2 deletions packages/varlet-cli/src/shared/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export const PRIMARY_COLOR = '#3a7afe'

// site
export const SITE_MOBILE_MAIN = resolve(__dirname, '../../site/mobile/main.ts')
export const SITE_MOBILE_ROUTES = resolve(__dirname, '../../site/mobile/routes.ts')
export const SITE_MOBILE = resolve(__dirname, '../../site/mobile')
export const SITE_PC_MAIN = resolve(__dirname, '../../site/pc/main.ts')
export const SITE_PC_ROUTES = resolve(__dirname, '../../site/pc/routes.ts')
export const SITE_PC = resolve(__dirname, '../../site/pc')
export const SITE_CONFIG = resolve(__dirname, '../../site/site.config.json')
export const SITE_OUTPUT_PATH = resolve(CWD, 'site')
export const SITE_PUBLIC_PATH = resolve(CWD, 'public')
Expand Down

0 comments on commit d60158a

Please sign in to comment.