Skip to content

Commit

Permalink
feat(rollup)!: change some variables into functions (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Dec 26, 2023
1 parent 59ccda9 commit c2bd834
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-lemons-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pkgr/rollup": major
---

feat!: change some variables into functions
4 changes: 2 additions & 2 deletions packages/rollup/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
__PROD__,
arrayify,
identify,
monorepoPkgs,
getMonorepoPkgs,
tryExtensions,
tryFile,
tryGlob,
Expand Down Expand Up @@ -212,7 +212,7 @@ ConfigOptions = {}): RollupOptions[] => {
pkg.endsWith('/package.json') ? pkg : `${pkg}/package.json`,
),
)
: monorepoPkgs
: getMonorepoPkgs()

pkgs = pkgs.map(pkg => pkg.replace(/[/\\]package\.json$/, ''))

Expand Down
27 changes: 18 additions & 9 deletions packages/utils/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ export const tryRequirePkg = <T>(pkg: string): T | undefined => {
} catch {}
}

export const isTsAvailable = isPkgAvailable('typescript')
export const isTsAvailable = () => isPkgAvailable('typescript')

export const isAngularAvailable = isPkgAvailable('@angular/core/package.json')
export const isAngularAvailable = () => isPkgAvailable('@angular/core')

export const isMdxAvailable =
isPkgAvailable('@mdx-js/mdx/package.json') ||
isPkgAvailable('@mdx-js/react/package.json')
export const isMdxAvailable = () =>
isPkgAvailable('@mdx-js/mdx') || isPkgAvailable('@mdx-js/react')

export const isReactAvailable = isPkgAvailable('react')
export const isReactAvailable = () => isPkgAvailable('react')

export const isSvelteAvailable = isPkgAvailable('svelte')
export const isSvelteAvailable = () => isPkgAvailable('svelte')

export const isVueAvailable = isPkgAvailable('vue')
export const isVueAvailable = () => isPkgAvailable('vue')

export const tryGlob = (
paths: string[],
Expand Down Expand Up @@ -60,21 +59,31 @@ export const tryGlob = (
)
}

/**
* type guard for non-empty values
*/
export const identify = <T>(
_: T,
): _ is Exclude<
T,
'' | (T extends boolean ? false : boolean) | null | undefined
> => !!_

/**
* flat array and remove nullish values
*/
export const arrayify = <
T,
R = T extends Array<infer S> ? NonNullable<S> : NonNullable<T>,
>(
...args: Array<R | R[]>
) =>
args.reduce<R[]>((arr, curr) => {
arr.push(...(Array.isArray(curr) ? curr : curr == null ? [] : [curr]))
if (curr != null) {
arr.push(
...(Array.isArray(curr) ? curr.filter(it => it != null) : [curr]),
)
}
return arr
}, [])

Expand Down
32 changes: 19 additions & 13 deletions packages/utils/src/monorepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ import path from 'node:path'

import { tryGlob, tryRequirePkg } from './helpers.js'

const pkg =
tryRequirePkg<{ workspaces?: string[] }>(path.resolve('package.json')) ?? {}
const getPkgPaths = () => {
const pkg =
tryRequirePkg<{ workspaces?: string[] }>(path.resolve('package.json')) ?? {}

const lernaConfig =
tryRequirePkg<{ packages?: string[] }>(path.resolve('lerna.json')) ?? {}
const lernaConfig =
tryRequirePkg<{ packages?: string[] }>(path.resolve('lerna.json')) ?? {}

const pkgsPath = lernaConfig.packages ?? pkg.workspaces ?? []
return lernaConfig.packages ?? pkg.workspaces ?? []
}

export const isMonorepo = Array.isArray(pkgsPath) && pkgsPath.length > 0
export const isMonorepo = () => {
const pkgPaths = getPkgPaths()
return Array.isArray(pkgPaths) && pkgPaths.length > 0
}

export const monorepoPkgs = isMonorepo
? tryGlob(
pkgsPath.map(pkg =>
pkg.endsWith('/package.json') ? pkg : `${pkg}/package.json`,
),
)
: []
export const getMonorepoPkgs = () =>
isMonorepo()
? tryGlob(
getPkgPaths().map(pkg =>
pkg.endsWith('/package.json') ? pkg : `${pkg}/package.json`,
),
)
: []
47 changes: 47 additions & 0 deletions patches/@1stg+eslint-config+7.0.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
diff --git a/node_modules/@1stg/eslint-config/_util.js b/node_modules/@1stg/eslint-config/_util.js
index 050e160..790fa33 100644
--- a/node_modules/@1stg/eslint-config/_util.js
+++ b/node_modules/@1stg/eslint-config/_util.js
@@ -4,12 +4,12 @@ const { preferPrettier } = require('@1stg/config')
const {
isMonorepo,
isPkgAvailable,
- monorepoPkgs,
+ getMonorepoPkgs,
tryRequirePkg,
} = require('@pkgr/utils')

-if (isMonorepo) {
- exports.allowModules = monorepoPkgs.reduce((acc, pkg) => {
+if (isMonorepo()) {
+ exports.allowModules = getMonorepoPkgs().reduce((acc, pkg) => {
const pkgJson = tryRequirePkg(path.resolve(pkg, 'package.json'))
if (!pkgJson) {
return acc
diff --git a/node_modules/@1stg/eslint-config/overrides.js b/node_modules/@1stg/eslint-config/overrides.js
index 3c54ff2..f48f293 100644
--- a/node_modules/@1stg/eslint-config/overrides.js
+++ b/node_modules/@1stg/eslint-config/overrides.js
@@ -550,15 +550,15 @@ exports.overrides = []
// eslint-disable-next-line unicorn/prefer-spread
.concat(
isPkgAvailable('@babel/core') && exports.js,
- isTsAvailable && exports.ts,
- isReactAvailable && exports.react,
- isReactAvailable && exports.reactHooks,
- isReactAvailable && exports.reactTs,
+ isTsAvailable() && exports.ts,
+ isReactAvailable() && exports.react,
+ isReactAvailable() && exports.reactHooks,
+ isReactAvailable() && exports.reactTs,
// The order matters, the later should to be preferred
exports.markup,
- isAngularAvailable && exports.angular,
- isVueAvailable && exports.vue,
- isSvelteAvailable && exports.svelte,
+ isAngularAvailable() && exports.angular,
+ isVueAvailable() && exports.vue,
+ isSvelteAvailable() && exports.svelte,
exports.md,
exports.mdx,
isPkgAvailable('jest') && exports.jest,

0 comments on commit c2bd834

Please sign in to comment.