Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 5f7ddd2

Browse files
authored
Merge branch 'main' into fix/transpile-extends
2 parents 33ff737 + 5d4586a commit 5f7ddd2

File tree

14 files changed

+197
-424
lines changed

14 files changed

+197
-424
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"rimraf": "^3.0.2",
6464
"typescript": "^4.7.3",
6565
"unbuild": "^0.7.4",
66-
"vitest": "^0.14.1",
66+
"vitest": "^0.14.2",
6767
"vue-tsc": "^0.37.3"
6868
},
6969
"packageManager": "[email protected]",

packages/kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"hash-sum": "^2.0.0",
2222
"ignore": "^5.2.0",
2323
"jiti": "^1.13.0",
24-
"knitwork": "^0.1.1",
24+
"knitwork": "^0.1.2",
2525
"lodash.template": "^4.5.0",
2626
"mlly": "^0.5.2",
2727
"pathe": "^0.3.0",

packages/kit/src/internal/template.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { promises as fsp } from 'node:fs'
22
import lodashTemplate from 'lodash.template'
3-
import hash from 'hash-sum'
4-
import { camelCase } from 'scule'
5-
import { basename, extname } from 'pathe'
6-
import { genDynamicImport, genImport } from 'knitwork'
3+
import { genSafeVariableName, genDynamicImport, genImport } from 'knitwork'
74

85
import type { NuxtTemplate } from '@nuxt/schema'
96

@@ -26,18 +23,16 @@ export async function compileTemplate (template: NuxtTemplate, ctx: any) {
2623

2724
const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"{(.+)}"(?=,?$)/gm, r => JSON.parse(r).replace(/^{(.*)}$/, '$1'))
2825

29-
const importName = (src: string) => `${camelCase(basename(src, extname(src))).replace(/[^a-zA-Z?\d\s:]/g, '')}_${hash(src)}`
30-
3126
const importSources = (sources: string | string[], { lazy = false } = {}) => {
3227
if (!Array.isArray(sources)) {
3328
sources = [sources]
3429
}
3530
return sources.map((src) => {
3631
if (lazy) {
37-
return `const ${importName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
32+
return `const ${genSafeVariableName(src)} = ${genDynamicImport(src, { comment: `webpackChunkName: ${JSON.stringify(src)}` })}`
3833
}
39-
return genImport(src, importName(src))
34+
return genImport(src, genSafeVariableName(src))
4035
}).join('\n')
4136
}
4237

43-
export const templateUtils = { serialize, importName, importSources }
38+
export const templateUtils = { serialize, importName: genSafeVariableName, importSources }

packages/kit/src/resolve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,5 @@ async function existsSensitive (path: string) {
148148

149149
export async function resolveFiles (path: string, pattern: string | string[]) {
150150
const files = await globby(pattern, { cwd: path, followSymbolicLinks: true })
151-
return files.filter(p => !isIgnored(p)).map(p => resolve(path, p))
151+
return files.map(p => resolve(path, p)).filter(p => !isIgnored(p))
152152
}

packages/nuxt/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"h3": "^0.7.8",
5050
"hash-sum": "^2.0.0",
5151
"hookable": "^5.1.1",
52-
"knitwork": "^0.1.1",
52+
"knitwork": "^0.1.2",
5353
"magic-string": "^0.26.2",
5454
"mlly": "^0.5.2",
5555
"nitropack": "^0.4.4",
@@ -66,7 +66,7 @@
6666
"unplugin": "^0.7.0",
6767
"untyped": "^0.4.4",
6868
"vue": "^3.2.37",
69-
"vue-bundle-renderer": "^0.3.8",
69+
"vue-bundle-renderer": "^0.3.9",
7070
"vue-router": "^4.0.16"
7171
},
7272
"devDependencies": {

packages/nuxt/src/core/templates.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { templateUtils } from '@nuxt/kit'
22
import type { Nuxt, NuxtApp, NuxtTemplate } from '@nuxt/schema'
3-
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString } from 'knitwork'
3+
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genObjectFromRawEntries, genString, genSafeVariableName } from 'knitwork'
44

55
import { isAbsolute, join, relative } from 'pathe'
66
import { resolveSchema, generateTypes } from 'untyped'
@@ -50,7 +50,7 @@ export const clientPluginTemplate = {
5050
const clientPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'server')
5151
return [
5252
templateUtils.importSources(clientPlugins.map(p => p.src)),
53-
`export default ${genArrayFromRaw(clientPlugins.map(p => templateUtils.importName(p.src)))}`
53+
`export default ${genArrayFromRaw(clientPlugins.map(p => genSafeVariableName(p.src)))}`
5454
].join('\n')
5555
}
5656
}
@@ -64,7 +64,7 @@ export const serverPluginTemplate = {
6464
templateUtils.importSources(serverPlugins.map(p => p.src)),
6565
`export default ${genArrayFromRaw([
6666
'preload',
67-
...serverPlugins.map(p => templateUtils.importName(p.src))
67+
...serverPlugins.map(p => genSafeVariableName(p.src))
6868
])}`
6969
].join('\n')
7070
}

packages/nuxt/src/pages/module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { existsSync } from 'node:fs'
22
import { defineNuxtModule, addTemplate, addPlugin, addVitePlugin, addWebpackPlugin, findPath } from '@nuxt/kit'
33
import { resolve } from 'pathe'
4-
import { genDynamicImport, genString, genArrayFromRaw, genImport, genObjectFromRawEntries } from 'knitwork'
4+
import { genDynamicImport, genString, genArrayFromRaw, genImport, genObjectFromRawEntries, genSafeVariableName } from 'knitwork'
55
import escapeRE from 'escape-string-regexp'
66
import { distDir } from '../dirs'
7-
import { resolvePagesRoutes, normalizeRoutes, resolveMiddleware, getImportName } from './utils'
7+
import { resolvePagesRoutes, normalizeRoutes, resolveMiddleware } from './utils'
88
import { TransformMacroPlugin, TransformMacroPluginOptions } from './macros'
99

1010
export default defineNuxtModule({
@@ -113,8 +113,8 @@ export default defineNuxtModule({
113113
const namedMiddleware = middleware.filter(mw => !mw.global)
114114
const namedMiddlewareObject = genObjectFromRawEntries(namedMiddleware.map(mw => [mw.name, genDynamicImport(mw.path)]))
115115
return [
116-
...globalMiddleware.map(mw => genImport(mw.path, getImportName(mw.name))),
117-
`export const globalMiddleware = ${genArrayFromRaw(globalMiddleware.map(mw => getImportName(mw.name)))}`,
116+
...globalMiddleware.map(mw => genImport(mw.path, genSafeVariableName(mw.name))),
117+
`export const globalMiddleware = ${genArrayFromRaw(globalMiddleware.map(mw => genSafeVariableName(mw.name)))}`,
118118
`export const namedMiddleware = ${namedMiddlewareObject}`
119119
].join('\n')
120120
}

packages/nuxt/src/pages/utils.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { basename, extname, normalize, relative, resolve } from 'pathe'
22
import { encodePath } from 'ufo'
33
import { NuxtMiddleware, NuxtPage } from '@nuxt/schema'
44
import { resolveFiles, useNuxt } from '@nuxt/kit'
5-
import { kebabCase, pascalCase } from 'scule'
6-
import { genImport, genDynamicImport, genArrayFromRaw } from 'knitwork'
5+
import { kebabCase } from 'scule'
6+
import { genImport, genDynamicImport, genArrayFromRaw, genSafeVariableName } from 'knitwork'
77
import escapeRE from 'escape-string-regexp'
88

99
enum SegmentParserState {
@@ -88,15 +88,11 @@ export function generateRoutesFromFiles (files: string[], pagesDir: string): Nux
8888
}
8989

9090
parent.push(route)
91-
// TODO: https://github.com/vuejs/router/issues/1435
92-
parent.sort((a, b) => getSortablePath(a.path).localeCompare(getSortablePath(b.path)))
9391
}
9492

9593
return prepareRoutes(routes)
9694
}
9795

98-
const getSortablePath = (path: string) => path.replace(/^\//, '').replace(/:/, 'Z')
99-
10096
function getRoutePath (tokens: SegmentToken[]): string {
10197
return tokens.reduce((path, token) => {
10298
return (
@@ -233,7 +229,7 @@ export function normalizeRoutes (routes: NuxtPage[], metaImports: Set<string> =
233229
imports: metaImports,
234230
routes: genArrayFromRaw(routes.map((route) => {
235231
const file = normalize(route.file)
236-
const metaImportName = getImportName(file) + 'Meta'
232+
const metaImportName = genSafeVariableName(file) + 'Meta'
237233
metaImports.add(genImport(`${file}?macro=true`, [{ name: 'meta', as: metaImportName }]))
238234
return {
239235
...Object.fromEntries(Object.entries(route).map(([key, value]) => [key, JSON.stringify(value)])),
@@ -271,10 +267,6 @@ function hasSuffix (path: string, suffix: string) {
271267
return basename(path).replace(extname(path), '').endsWith(suffix)
272268
}
273269

274-
export function getImportName (name: string) {
275-
return pascalCase(name).replace(/[^\w]/g, r => '_' + r.charCodeAt(0))
276-
}
277-
278270
function uniqueBy <T, K extends keyof T> (arr: T[], key: K) {
279271
const res: T[] = []
280272
const seen = new Set<T[K]>()

packages/nuxt/test/pages.test.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,11 @@ describe('pages:generateRoutesFromFiles', () => {
112112
file: `${pagesDir}/index.vue`,
113113
children: []
114114
},
115-
{
116-
name: 'nonopt-slug',
117-
path: '/nonopt/:slug',
118-
file: `${pagesDir}/nonopt/[slug].vue`,
119-
children: []
120-
},
121-
{
122-
name: 'opt-slug',
123-
path: '/opt/:slug?',
124-
file: `${pagesDir}/opt/[[slug]].vue`,
125-
children: []
126-
},
127115
{
128116
children: [],
129-
name: 'bar',
130-
file: 'pages/[bar]/index.vue',
131-
path: '/:bar'
117+
name: 'slug',
118+
file: 'pages/[slug].vue',
119+
path: '/:slug'
132120
},
133121
{
134122
children: [
@@ -144,9 +132,21 @@ describe('pages:generateRoutesFromFiles', () => {
144132
path: '/:foo?'
145133
},
146134
{
147-
name: 'slug',
148-
path: '/:slug',
149-
file: `${pagesDir}/[slug].vue`,
135+
children: [],
136+
name: 'bar',
137+
file: 'pages/[bar]/index.vue',
138+
path: '/:bar'
139+
},
140+
{
141+
name: 'nonopt-slug',
142+
path: '/nonopt/:slug',
143+
file: `${pagesDir}/nonopt/[slug].vue`,
144+
children: []
145+
},
146+
{
147+
name: 'opt-slug',
148+
path: '/opt/:slug?',
149+
file: `${pagesDir}/opt/[[slug]].vue`,
150150
children: []
151151
},
152152
{
@@ -161,17 +161,17 @@ describe('pages:generateRoutesFromFiles', () => {
161161
description: 'should generate correct catch-all route',
162162
files: [`${pagesDir}/[...slug].vue`, `${pagesDir}/index.vue`],
163163
output: [
164-
{
165-
name: 'index',
166-
path: '/',
167-
file: `${pagesDir}/index.vue`,
168-
children: []
169-
},
170164
{
171165
name: 'slug',
172166
path: '/:slug(.*)*',
173167
file: `${pagesDir}/[...slug].vue`,
174168
children: []
169+
},
170+
{
171+
name: 'index',
172+
path: '/',
173+
file: `${pagesDir}/index.vue`,
174+
children: []
175175
}
176176
]
177177
},

packages/test-utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"devDependencies": {
2525
"playwright": "^1.22.2",
2626
"unbuild": "latest",
27-
"vitest": "^0.14.1"
27+
"vitest": "^0.14.2"
2828
},
2929
"peerDependencies": {
3030
"vue": "^3.2.37"

0 commit comments

Comments
 (0)