Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide asPath #124

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions buildConstructAsPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { build } = require('esbuild')
const path = require('path')

build({
entryPoints: [path.resolve(__dirname, './src/constructAsPath.ts')],
outfile: path.resolve(__dirname, './dist/outConstructAsPath.js'),
target: 'es2020',
keepNames: true,
bundle: true,
minify: true
}).catch(() => process.exit(1))
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"bin": "bin/index.js",
"scripts": {
"dev": "npm run build && ts-node projects/build.ts",
"build": "npm run rimraf && tsc",
"build": "npm run rimraf && tsc && npm run build:asPath",
"build:asPath": "node ./buildConstructAsPath.js",
"rimraf": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\"",
"lint": "eslint --ext .ts . && prettier --check \"./**/*.ts\"",
"lint:fix": "eslint --ext .ts . --fix && prettier --write \"./**/*.ts\"",
"pretest": "npm run build:asPath",
"test": "jest",
"typecheck": "tsc --noEmit"
},
Expand Down Expand Up @@ -89,6 +91,7 @@
"@types/minimist": "^1.2.2",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"esbuild": "^0.14.28",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.4.0",
"eslint-config-standard": "^16.0.3",
Expand Down
35 changes: 25 additions & 10 deletions projects/nextjs-basepath/out/lib/basic/$path.ts

Large diffs are not rendered by default.

35 changes: 25 additions & 10 deletions projects/nextjs-basepath/out/lib/ignore/$path.ts

Large diffs are not rendered by default.

38 changes: 27 additions & 11 deletions projects/nextjs-basepath/out/lib/static/$path.ts

Large diffs are not rendered by default.

38 changes: 27 additions & 11 deletions projects/nextjs-basepath/utils/$path.ts

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions projects/nextjs-custom-ext/lib/$path.ts

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions projects/nextjs-custom-ext/out/lib/basic/$path.ts

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions projects/nextjs-custom-ext/out/lib/ignore/$path.ts

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions projects/nextjs-custom-ext/out/lib/static/$path.ts

Large diffs are not rendered by default.

35 changes: 25 additions & 10 deletions projects/nextjs-src/src/out/lib/basic/$path.ts

Large diffs are not rendered by default.

35 changes: 25 additions & 10 deletions projects/nextjs-src/src/out/lib/ignore/$path.ts

Large diffs are not rendered by default.

38 changes: 27 additions & 11 deletions projects/nextjs-src/src/out/lib/static/$path.ts

Large diffs are not rendered by default.

38 changes: 27 additions & 11 deletions projects/nextjs-src/src/utils/$path.ts

Large diffs are not rendered by default.

46 changes: 33 additions & 13 deletions projects/nextjs/lib/$path.ts

Large diffs are not rendered by default.

43 changes: 31 additions & 12 deletions projects/nextjs/out/lib/basic/$path.ts

Large diffs are not rendered by default.

43 changes: 31 additions & 12 deletions projects/nextjs/out/lib/ignore/$path.ts

Large diffs are not rendered by default.

46 changes: 33 additions & 13 deletions projects/nextjs/out/lib/static/$path.ts

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/constructAsPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { interpolateAs } from 'next/dist/shared/lib/router/router.js'
import { formatUrl } from 'next/dist/shared/lib/router/utils/format-url'
import type { ParsedUrlQuery } from 'querystring'
import type { UrlObject } from 'url'

declare let objToAsPath: (url: UrlObject) => string

const omitParmsFromQuery = (query: ParsedUrlQuery, params: string[]) => {
const filteredQuery: ParsedUrlQuery = {}

Object.keys(query).forEach(key => {
if (!params.includes(key)) {
filteredQuery[key] = query[key]
}
})
return filteredQuery
}

// eslint-disable-next-line
objToAsPath = (url: UrlObject) => {
let interpolatedAs = ''

const { result, params } = interpolateAs(
url.pathname ?? '',
url.pathname ?? '',
url.query as ParsedUrlQuery
)

if (result) {
interpolatedAs = formatUrl({
pathname: result,
hash: url.hash,
query:
url.query && typeof url.query !== 'string'
? omitParmsFromQuery(url.query as ParsedUrlQuery, params)
: url.query
})
}

return interpolatedAs
}
21 changes: 18 additions & 3 deletions src/createNextTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ const createMethods = (
: importName
? `, query: url${importName.startsWith('Query') ? '' : '?'}.query`
: ''
}, hash: url${importName?.startsWith('Query') ? '' : '?'}.hash })`
}, hash: url${importName?.startsWith('Query') ? '' : '?'}.hash }),\n${indent} $asPath: (url${
importName?.startsWith('Query') ? '' : '?'
}: { ${
importName ? `query${importName.startsWith('Optional') ? '?' : ''}: ${importName}, ` : ''
}hash?: string }) => (objToAsPath({ pathname: '${pathname}' as const${
slugs.length
? `, query: { ${slugs.join(', ')}${
importName ? `, ...url${importName.startsWith('Query') ? '' : '?'}.query` : ''
} }`
: importName
? `, query: url${importName.startsWith('Query') ? '' : '?'}.query`
: ''
}, hash: url${importName?.startsWith('Query') ? '' : '?'}.hash }))`

export default (
input: string,
Expand All @@ -34,7 +46,7 @@ export default (
const regExpChunk = `\\.(${pageExtensions.join('|').replace(/\./g, '\\.')})$`
const indexPageRegExp = new RegExp(`^index${regExpChunk}`)
const pageExtRegExp = new RegExp(regExpChunk)
const imports: string[] = []
const imports: string[] = ["import type { UrlObject } from 'url'"]
const getImportName = (file: string) => {
const result = parseQueryFromTS(output, file, imports.length)

Expand Down Expand Up @@ -156,8 +168,11 @@ export default (

const text = createPathObjString(input, rootIndent, '', [], `{\n<% props %>\n}`, rootMethods)

const fileData = fs.readFileSync(path.resolve(__dirname, '../dist/outConstructAsPath.js'), 'utf8')
const objToAsPath = `let objToAsPath:(arg: UrlObject) => string;\n// @ts-ignore\n// eslint-disable-next-line\n// prettier-ignore\n${fileData}`

return `${imports.join('\n')}${
imports.length ? '\n\n' : ''
}export const pagesPath = ${text}\n\nexport type PagesPath = typeof pagesPath
}${objToAsPath}\n\nexport const pagesPath = ${text}\n\nexport type PagesPath = typeof pagesPath
`
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"strictPropertyInitialization": false,
"target": "es5"
},
"include": ["src/**/*"]
"include": ["src/**/*"],
"exclude": ["src/constructAsPath.ts"]
}
126 changes: 126 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,132 @@ es6-object-assign@^1.1.0:
resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c"
integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw=

esbuild-android-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-android-64/-/esbuild-android-64-0.14.28.tgz#69c7a8a4f4a888eb5584afb035524b0fda7affff"
integrity sha512-A52C3zq+9tNwCqZ+4kVLBxnk/WnrYM8P2+QNvNE9B6d2OVPs214lp3g6UyO+dKDhUdefhfPCuwkP8j2A/+szNA==

esbuild-android-arm64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-android-arm64/-/esbuild-android-arm64-0.14.28.tgz#110ff82019e75b866b53844c32f19f7933b4ce36"
integrity sha512-sm0fDEGElZhMC3HLZeECI2juE4aG7uPfMBMqNUhy9CeX399Pz8rC6e78OXMXInGjSdEAwQmCOHmfsP7uv3Q8rA==

esbuild-darwin-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-darwin-64/-/esbuild-darwin-64-0.14.28.tgz#d929ce16035da6047504fe8a71587d2ac9b756ed"
integrity sha512-nzDd7mQ44FvsFHtOafZdBgn3Li5SMsnMnoz1J2MM37xJmR3wGNTFph88KypjHgWqwbxCI7MXS1U+sN4qDeeW6Q==

esbuild-darwin-arm64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.14.28.tgz#75e1cb75c2230c541be1707c6751395fee9f6bbd"
integrity sha512-XEq/bLR/glsUl+uGrBimQzOVs/CmwI833fXUhP9xrLI3IJ+rKyrZ5IA8u+1crOEf1LoTn8tV+hInmX6rGjbScw==

esbuild-freebsd-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-freebsd-64/-/esbuild-freebsd-64-0.14.28.tgz#3579fd41f4c090d52e1a9134743e591c6aea49d7"
integrity sha512-rTKLgUj/HEcPeE5XZ7IZwWpFx7IWMfprN7QRk/TUJE1s1Ipb58esboIesUpjirJz/BwrgHq+FDG9ChAI8dZAtQ==

esbuild-freebsd-arm64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-freebsd-arm64/-/esbuild-freebsd-arm64-0.14.28.tgz#de1c102a40005fa9da5160c0242b2de89ffd2d7b"
integrity sha512-sBffxD1UMOsB7aWMoExmipycjcy3HJGwmqE4GQZUTZvdiH4GhjgUiVdtPyt7kSCdL40JqnWQJ4b1l8Y51oCF4Q==

esbuild-linux-32@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-linux-32/-/esbuild-linux-32-0.14.28.tgz#cdb8ac2000df06044450bf33a93b9d63d61bb669"
integrity sha512-+Wxidh3fBEQ9kHcCsD4etlBTMb1n6QY2uXv3rFhVn88CY/JP782MhA57/ipLMY4kOLeSKEuFGN4rtjHuhmRMig==

esbuild-linux-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-linux-64/-/esbuild-linux-64-0.14.28.tgz#b1e961d42af89dab8c3c0ce86420a7657765f0ae"
integrity sha512-7+xgsC4LvR6cnzaBdiljNnPDjbkwzahogN+S9uy9AoYw7ZjPnnXc6sjQAVCbqGb7MEgrWdpa6u/Tao79i4lWxg==

esbuild-linux-arm64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-linux-arm64/-/esbuild-linux-arm64-0.14.28.tgz#f69e6ace792a4985b9760b443dbf627e5e3d2126"
integrity sha512-EjRHgwg+kgXABzyoPGPOPg4d5wZqRnZ/ZAxBDzLY+i6DS8OUfTSlZHWIOZzU4XF7125WxRBg9ULbrFJBl+57Eg==

esbuild-linux-arm@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-linux-arm/-/esbuild-linux-arm-0.14.28.tgz#9c2fa45578686370a5d782314f321a2c6b641270"
integrity sha512-L5isjmlLbh9E0WVllXiVETbScgMbth/+XkXQii1WwgO1RvLIfaGrVFz8d2n6EH/ImtgYxPYGx+OcvIKQBc91Rg==

esbuild-linux-mips64le@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-linux-mips64le/-/esbuild-linux-mips64le-0.14.28.tgz#99d78f0380640aa7faa2c4c49ac21229bdf33c7c"
integrity sha512-krx9SSg7yfiUKk64EmjefOyiEF6nv2bRE4um/LiTaQ6Y/6FP4UF3/Ou/AxZVyR154uSRq63xejcAsmswXAYRsw==

esbuild-linux-ppc64le@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-linux-ppc64le/-/esbuild-linux-ppc64le-0.14.28.tgz#7388fa0c76033b4ca85b74071cb793d41ae77642"
integrity sha512-LD0Xxu9g+DNuhsEBV5QuVZ4uKVBMup0xPIruLweuAf9/mHXFnaCuNXUBF5t0DxKl7GQ5MSioKtnb92oMo+QXEw==

esbuild-linux-riscv64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-linux-riscv64/-/esbuild-linux-riscv64-0.14.28.tgz#99e4a8afe4762e927ebe02009e1927e38f3256ab"
integrity sha512-L/DWfRh2P0vxq4Y+qieSNXKGdMg+e9Qe8jkbN2/8XSGYDTPzO2OcAxSujob4qIh7iSl+cknbXV+BvH0YFR0jbg==

esbuild-linux-s390x@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-linux-s390x/-/esbuild-linux-s390x-0.14.28.tgz#38a625399ffc78f3b8b555ebe2013347256a9a8a"
integrity sha512-rrgxmsbmL8QQknWGnAL9bGJRQYLOi2AzXy5OTwfhxnj9eqjo5mSVbJXjgiq5LPUAMQZGdPH5yaNK0obAXS81Zw==

esbuild-netbsd-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-netbsd-64/-/esbuild-netbsd-64-0.14.28.tgz#fdc09dd69313f42be034276cc780bf60c09266b6"
integrity sha512-h8wntIyOR8/xMVVM6TvJxxWKh4AjmLK87IPKpuVi8Pq0kyk0RMA+eo4PFGk5j2XK0D7dj8PcSF5NSlP9kN/j0A==

esbuild-openbsd-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-openbsd-64/-/esbuild-openbsd-64-0.14.28.tgz#9d7b0ca421ae580ab945c69c33eabd793262a84c"
integrity sha512-HBv18rVapbuDx52/fhZ/c/w6TXyaQAvRxiDDn5Hz/pBcwOs3cdd2WxeIKlWmDoqm2JMx5EVlq4IWgoaRX9mVkw==

esbuild-sunos-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-sunos-64/-/esbuild-sunos-64-0.14.28.tgz#5b82807ebe435519a2689e1a4d50b8a3cc5c64c0"
integrity sha512-zlIxePhZxKYheR2vBCgPVvTixgo/ozOfOMoP6RZj8dxzquU1NgeyhjkcRXucbLCtmoNJ+i4PtWwPZTLuDd3bGg==

esbuild-windows-32@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-windows-32/-/esbuild-windows-32-0.14.28.tgz#5cf740782fadc865c00aa0d8388e42012bcf496e"
integrity sha512-am9DIJxXlld1BOAY/VlvBQHMUCPL7S3gB/lnXIY3M4ys0gfuRqPf4EvMwZMzYUbFKBY+/Qb8SRgPRRGhwnJ8Kg==

esbuild-windows-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-windows-64/-/esbuild-windows-64-0.14.28.tgz#6e3ec1b0225d668a2da21e2ffeff2353b5c9a567"
integrity sha512-78PhySDnmRZlsPNp/W/5Fim8iivlBQQxfhBFIqR7xwvfDmCFUSByyMKP7LCHgNtb04yNdop8nJJkJaQ8Xnwgiw==

esbuild-windows-arm64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-windows-arm64/-/esbuild-windows-arm64-0.14.28.tgz#c527d52ec7d1f868259d0f74ecc4003e8475125d"
integrity sha512-VhXGBTo6HELD8zyHXynV6+L2jWx0zkKnGx4TmEdSBK7UVFACtOyfUqpToG0EtnYyRZ0HESBhzPSVpP781ovmvA==

esbuild@^0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.14.28.tgz#7738635d2ea19e446bd319d83a1802545e6aebb8"
integrity sha512-YLNprkCcMVKQ5sekmCKEQ3Obu/L7s6+iij38xNKyBeSmSsTWur4Ky/9zB3XIGT8SCJITG/bZwAR2l7YOAXch4Q==
optionalDependencies:
esbuild-android-64 "0.14.28"
esbuild-android-arm64 "0.14.28"
esbuild-darwin-64 "0.14.28"
esbuild-darwin-arm64 "0.14.28"
esbuild-freebsd-64 "0.14.28"
esbuild-freebsd-arm64 "0.14.28"
esbuild-linux-32 "0.14.28"
esbuild-linux-64 "0.14.28"
esbuild-linux-arm "0.14.28"
esbuild-linux-arm64 "0.14.28"
esbuild-linux-mips64le "0.14.28"
esbuild-linux-ppc64le "0.14.28"
esbuild-linux-riscv64 "0.14.28"
esbuild-linux-s390x "0.14.28"
esbuild-netbsd-64 "0.14.28"
esbuild-openbsd-64 "0.14.28"
esbuild-sunos-64 "0.14.28"
esbuild-windows-32 "0.14.28"
esbuild-windows-64 "0.14.28"
esbuild-windows-arm64 "0.14.28"

escalade@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
Expand Down