Skip to content

Commit

Permalink
fix: simplify import names
Browse files Browse the repository at this point in the history
fix #47
  • Loading branch information
posva committed Aug 27, 2022
1 parent 769c30d commit 7c01822
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/codegen/__snapshots__/generateRouteRecords.spec.ts.snap
Expand Up @@ -74,7 +74,7 @@ exports[`generateRouteRecord > generate custom imports 1`] = `
{
path: '/a',
name: '/a',
component: _page_a_vue,
component: _page_0,
/* no props */
/* no children */
},
Expand Down Expand Up @@ -113,7 +113,7 @@ exports[`generateRouteRecord > generate custom imports 1`] = `

exports[`generateRouteRecord > generate custom imports 2`] = `
Map {
"a.vue" => "_page_a_vue",
"_page_0" => "a.vue",
}
`;

Expand All @@ -122,14 +122,14 @@ exports[`generateRouteRecord > generate static imports 1`] = `
{
path: '/a',
name: '/a',
component: _page_a_vue,
component: _page_0,
/* no props */
/* no children */
},
{
path: '/b',
name: '/b',
component: _page_b_vue,
component: _page_1,
/* no props */
/* no children */
},
Expand All @@ -148,7 +148,7 @@ exports[`generateRouteRecord > generate static imports 1`] = `
{
path: 'c',
name: '/nested/file/c',
component: _page_nested_file_c_vue,
component: _page_2,
/* no props */
/* no children */
}
Expand All @@ -161,9 +161,9 @@ exports[`generateRouteRecord > generate static imports 1`] = `

exports[`generateRouteRecord > generate static imports 2`] = `
Map {
"a.vue" => "_page_a_vue",
"b.vue" => "_page_b_vue",
"nested/file/c.vue" => "_page_nested_file_c_vue",
"_page_0" => "a.vue",
"_page_1" => "b.vue",
"_page_2" => "nested/file/c.vue",
}
`;

Expand Down
4 changes: 3 additions & 1 deletion src/codegen/generateRouteMap.spec.ts
@@ -1,7 +1,9 @@
import { describe, expect, it } from 'vitest'
import { generateRouteNamedMap } from './generateRouteMap'
import { createPrefixTree } from '../core/tree'
import { DEFAULT_OPTIONS } from '../options'
import { resolveOptions } from '../options'

const DEFAULT_OPTIONS = resolveOptions()

function formatExports(exports: string) {
return exports
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/generateRouteRecords.spec.ts
@@ -1,9 +1,11 @@
import { basename } from 'pathe'
import { describe, expect, it } from 'vitest'
import { createPrefixTree, TreeNode } from '../core/tree'
import { DEFAULT_OPTIONS, ResolvedOptions } from '../options'
import { ResolvedOptions, resolveOptions } from '../options'
import { generateRouteRecord } from './generateRouteRecords'

const DEFAULT_OPTIONS = resolveOptions({})

describe('generateRouteRecord', () => {
function generateRouteRecordSimple(tree: TreeNode) {
return generateRouteRecord(tree, DEFAULT_OPTIONS, new Map())
Expand Down
10 changes: 5 additions & 5 deletions src/codegen/generateRouteRecords.ts
Expand Up @@ -68,9 +68,9 @@ ${startIndent}}`
if (node.hasDefinePage) {
const definePageDataList: string[] = []
for (const [name, filePath] of node.value.filePaths) {
const definePageData = `_definePage_${name}_${importList.size}`
definePageDataList.push(definePageData)
importList.set(`${filePath}?definePage&vue`, definePageData)
const pageDataImport = `_definePage_${name}_${importList.size}`
definePageDataList.push(pageDataImport)
importList.set(pageDataImport, `${filePath}?definePage&vue`)
}

if (definePageDataList.length) {
Expand Down Expand Up @@ -126,8 +126,8 @@ function generatePageImport(
if (mode === 'async') {
return `() => import('${filepath}')`
} else {
const importName = `_page_${filepath.replace(/[\/\.]/g, '_')}`
importList.set(filepath, importName)
const importName = `_page_${importList.size}`
importList.set(importName, filepath)
return importName
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/context.ts
Expand Up @@ -144,6 +144,7 @@ export function createRoutesContext(options: ResolvedOptions) {
}

function generateRoutes() {
// keys are import names while values are paths import __ from __
const importList = new Map<string, string>()

const routesExport = `export const routes = ${generateRouteRecord(
Expand All @@ -156,7 +157,7 @@ export function createRoutesContext(options: ResolvedOptions) {
if (options.dataFetching) {
imports += `import { _HasDataLoaderMeta, _mergeRouteRecord } from 'unplugin-vue-router/runtime'\n`
}
for (const [path, name] of importList) {
for (const [name, path] of importList) {
imports += `import ${name} from '${path}'\n`
}

Expand Down

0 comments on commit 7c01822

Please sign in to comment.