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

feat: support package exports and custom paths #679

Open
wants to merge 1 commit into
base: bundlephobia
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion client/components/AutocompleteInput/AutocompleteInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class AutocompleteInput extends PureComponent {
render() {
const { className, containerClass, autoFocus, renderAsH1 } = this.props
const { suggestions, value, isMenuVisible } = this.state
const { name, version } = parsePackageString(value)
const { name, version, path } = parsePackageString(value)
const baseFontSize =
typeof window !== 'undefined' && window.innerWidth < 640 ? 22 : 35
const maxFullSizeChars =
Expand Down Expand Up @@ -142,6 +142,10 @@ export default class AutocompleteInput extends PureComponent {
<span className="dummy-input__at-separator">@</span>
)}
<span className="dummy-input__package-version">{version}</span>
{path !== null && (
<span className="dummy-input__path-separator">/</span>
)}
<span className="dummy-input__package-path">{path}</span>
</div>
</div>
<div
Expand Down
8 changes: 8 additions & 0 deletions client/components/AutocompleteInput/AutocompleteInput.scss
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@
color: #636363;
}

.dummy-input__package-path {
color: #a9a9a9;
}

.dummy-input__at-separator {
color: $pastel-green;
}

.dummy-input__path-separator {
color: #a9a9a9;
}

.autocomplete-input__suggestions-menu {
border: 1px solid $autocomplete-border-color;
border-top: 0;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ app.prepare().then(() => {
hash: ctx => ({
name: ctx.state.resolved.name,
version: ctx.state.resolved.version,
path: ctx.state.resolved.path,
}),
}),
errorMiddleware,
Expand All @@ -128,6 +129,7 @@ app.prepare().then(() => {
hash: ctx => ({
name: ctx.state.resolved.name,
version: ctx.state.resolved.version,
path: ctx.state.resolved.path,
}),
}),
errorMiddleware,
Expand Down
4 changes: 3 additions & 1 deletion pages/package/[...packageString]/ResultPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ class ResultPage extends PureComponent {

if (this.activeQuery !== packageString) return

const newPackageString = `${results.name}@${results.version}`
const newPackageString =
`${results.name}@${results.version}` +
(results.path ? '/' + results.path : '')
this.setState(
{
inputInitialValue: newPackageString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ class ExportAnalysisSection extends Component {
startAnalysis = () => {
const { result } = this.props
const { name, version } = result
const packageString = `${name}@${version}`
const packageString =
`${name}@${version}` + (result.path ? '/' + result.path : '')
const startTime = Date.now()
let sizeStartTime

Expand Down
4 changes: 2 additions & 2 deletions server/middlewares/exports.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const buildService = new BuildService()
async function exportsMiddleware(ctx) {
let result,
priority = getRequestPriority(ctx)
const { name, version, packageString } = ctx.state.resolved
const { name, version, path, packageString } = ctx.state.resolved
const { force, package: packageQuery } = ctx.query

const buildStart = now()
Expand All @@ -26,7 +26,7 @@ async function exportsMiddleware(ctx) {
? CONFIG.CACHE.SIZE_API_HAS_VERSION
: CONFIG.CACHE.SIZE_API_DEFAULT,
}
ctx.body = { name, version, exports: result }
ctx.body = { name, version, path, exports: result }
const time = buildEnd - buildStart

logger.info(
Expand Down
6 changes: 3 additions & 3 deletions server/middlewares/exportsSizes.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const buildService = new BuildService()
async function exportSizesMiddleware(ctx) {
let result,
priority = getRequestPriority(ctx)
const { name, version, packageString } = ctx.state.resolved
const { name, version, packageString, path } = ctx.state.resolved
const { force, peek, package: packageQuery } = ctx.query

if (peek) {
return { name, version, peekSuccess: false }
}

const buildStart = now()
result = await buildService.getPackageExportSizes(packageString, priority)
result = await buildService.getPackageExportSizes(packageQuery, priority)

const buildEnd = now()

Expand All @@ -33,7 +33,7 @@ async function exportSizesMiddleware(ctx) {
: CONFIG.CACHE.SIZE_API_DEFAULT,
}

const body = { name, version, ...result }
const body = { name, version, path, ...result }
ctx.body = body
const time = buildEnd - buildStart

Expand Down
11 changes: 10 additions & 1 deletion server/middlewares/results/build.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async function buildMiddleware(ctx, next) {
scoped,
name,
version,
path,
description,
repository,
packageString,
Expand All @@ -36,7 +37,15 @@ async function buildMiddleware(ctx, next) {
: CONFIG.CACHE.SIZE_API_DEFAULT,
}

const body = { scoped, name, version, description, repository, ...result }
const body = {
scoped,
name,
version,
path,
description,
repository,
...result,
}
ctx.body = body
ctx.state.buildResult = body
const time = buildEnd - buildStart
Expand Down
7 changes: 4 additions & 3 deletions server/middlewares/results/resolvePackage.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ async function resolvePackageMiddleware(ctx, next) {
// prefill values in case resolution fails
ctx.state.resolved = {
...parsedPackage,
packageString: `${parsedPackage.name}@${parsedPackage.version}`,
packageString: parsedPackage.fullPath,
}

const resolveStart = now()
resolvedPackage = await resolvePackage(packageString)
resolvedPackage = await resolvePackage(parsedPackage.normalPath)
const resolveEnd = now()

const { name, version, repository, description } = resolvedPackage
Expand All @@ -39,8 +39,9 @@ async function resolvePackageMiddleware(ctx, next) {
const result = {
name,
version,
path: parsedPackage.path,
scoped: parsedPackage.scoped,
packageString: `${name}@${version}`,
packageString: parsedPackage.fullPath,
description: truncatedDescription,
repository: repositoryURL,
}
Expand Down
55 changes: 40 additions & 15 deletions utils/common.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,57 @@ function parsePackageString(packageString) {
// Scoped packages
let name,
version,
path,
scope,
scoped = false
const lastAtIndex = packageString.lastIndexOf('@')
const firstSlashIndex = packageString.indexOf('/')

if (packageString.startsWith('@')) {
const secondSlashIndex = packageString.indexOf('/', firstSlashIndex + 1)

scoped = true
scope = packageString.substring(1, firstSlashIndex)
if (lastAtIndex === 0) {
name = packageString
version = null
} else {
name = packageString.substring(0, lastAtIndex)
version = packageString.substring(lastAtIndex + 1)
}

name =
lastAtIndex === 0
? secondSlashIndex === -1
? packageString
: packageString.substring(0, secondSlashIndex)
: packageString.substring(0, lastAtIndex)
version =
lastAtIndex === 0
? null
: secondSlashIndex === -1
? packageString.substring(lastAtIndex + 1)
: packageString.substring(lastAtIndex + 1, secondSlashIndex)
path =
secondSlashIndex === -1
? null
: packageString.substring(secondSlashIndex + 1)
} else {
if (lastAtIndex === -1) {
name = packageString
version = null
} else {
name = packageString.substring(0, lastAtIndex)
version = packageString.substring(lastAtIndex + 1)
}
name =
lastAtIndex === -1
? firstSlashIndex === -1
? packageString
: packageString.substring(0, firstSlashIndex)
: packageString.substring(0, lastAtIndex)
version =
lastAtIndex === -1
? null
: firstSlashIndex === -1
? packageString.substring(lastAtIndex + 1)
: packageString.substring(lastAtIndex + 1, firstSlashIndex)
path =
firstSlashIndex === -1
? null
: packageString.substring(firstSlashIndex + 1)
}

return { name, version, scope, scoped }
const normalPath = name + (version ? '@' + version : '')
const fullPath = normalPath + (path ? '/' + path : '')

return { name, version, path, scope, scoped, normalPath, fullPath }
}

function daysFromToday(date) {
Expand Down