Skip to content

Commit 623e264

Browse files
committed
Perf(Explore Config): Reduce size by removing glob support and trivial utils
1 parent 2e80cdc commit 623e264

File tree

197 files changed

+199
-15290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

197 files changed

+199
-15290
lines changed

packages/explore-config/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,31 @@ npm install explore-config
6262

6363
## Usage
6464
```ts
65-
exploreConfig(
66-
sources: Pattern | Pattern[],
67-
options?: {
68-
cwd?: string,
69-
keys?: string[],
70-
on?: {
71-
found?: (foundPath: string) => void,
72-
notFound?: () => void
73-
}
74-
}
75-
): any
65+
exploreConfig(name: string, options: ExploreConfigOptions)
7666
```
77-
7867
```ts
7968
import exploreConfig from 'explore-config'
8069

81-
config = exploreConfig('master.css.*')
82-
// {...}
70+
/**
71+
* 1. explore -> techor.config.js
72+
* 2. explore -> techor.config.ts
73+
* 3. explore -> techor.config.cjs
74+
* 4. explore -> techor.config.cts
75+
* 5. explore -> techor.config.mjs
76+
* 6. explore -> techor.config.mts
77+
* */
78+
const config = exploreConfig('techor.config')
79+
80+
console.log(config)
81+
// -> {...}
82+
```
83+
```ts
84+
export interface ExploreConfigOptions {
85+
extensions?: ('js' | 'ts' | 'cjs' | 'cts' | 'mjs' | 'mts')[]
86+
resolvedKeys?: string[]
87+
cwd?: string
88+
found?: (basename: string, configPath: string) => void
89+
}
8390
```
8491

8592
<br>

packages/explore-config/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@
5555
"access": "public"
5656
},
5757
"dependencies": {
58-
"@techor/extend": "workspace:^",
59-
"@techor/log": "workspace:^",
60-
"@techor/glob": "workspace:^",
6158
"cross-import": "workspace:^"
6259
}
6360
}

packages/explore-config/src/index.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-
import path from 'path'
2-
import log from '@techor/log'
3-
import extend from '@techor/extend'
4-
import { explorePathSync } from '@techor/glob'
51
import crossImport from 'cross-import'
2+
import { existsSync } from 'fs'
3+
import { resolve } from 'path'
64

7-
export default function exploreConfig(
8-
source: string | string[],
9-
options?: {
10-
cwd?: string,
11-
keys?: string[],
12-
on?: {
13-
found?: (foundPath: string) => void,
14-
notFound?: () => void
15-
}
16-
}
17-
) {
18-
options = extend({
19-
keys: ['config', 'default'],
20-
on: {
21-
found: (foundPath: string) => log.ok`**${foundPath}** config file found`,
22-
notFound: () => log.i`No **${source}** config file found`
23-
}
5+
export interface ExploreConfigOptions {
6+
extensions?: ('js' | 'ts' | 'cjs' | 'cts' | 'mjs' | 'mts')[]
7+
resolvedKeys?: string[]
8+
cwd?: string
9+
found?: (basename: string, configPath: string) => void
10+
}
11+
12+
export default function exploreConfig(name: string, options: ExploreConfigOptions = {}) {
13+
options = Object.assign({
14+
extensions: ['js', 'mjs', 'ts', 'cjs', 'cts', 'mts'],
15+
resolvedKeys: ['config', 'default'],
2416
}, options)
25-
const { on, keys } = options
26-
let foundConfig: any
27-
const foundPath = explorePathSync(source, options)
28-
if (foundPath) {
29-
const foundConfigModule = crossImport(path.resolve(options.cwd || '', foundPath))
30-
for (const key of keys) {
31-
foundConfig = foundConfigModule[key]
32-
if (foundConfig) {
33-
break
34-
}
17+
let config: any
18+
// try to find the config file with the given name and options.extensions
19+
let foundConfigPath: string | undefined
20+
let foundBasename: string | undefined
21+
for (const eachExtension of options.extensions) {
22+
const eachBasename = name + '.' + eachExtension
23+
const eachPath = resolve(options.cwd || '', eachBasename)
24+
if (existsSync(eachPath)) {
25+
foundConfigPath = eachPath
26+
foundBasename = eachBasename
27+
break
3528
}
36-
if (!foundConfig) {
37-
foundConfig = foundConfigModule
29+
}
30+
if (foundConfigPath) {
31+
const configModule = crossImport(foundConfigPath)
32+
for (const eachResolvedKey of options.resolvedKeys) {
33+
config = configModule[eachResolvedKey]
34+
if (config) break
3835
}
39-
on.found?.(foundPath)
40-
} else {
41-
on.notFound?.()
36+
if (!config) config = configModule
37+
options?.found(foundBasename, foundConfigPath)
4238
}
43-
return foundConfig
44-
}
39+
return config
40+
}

packages/explore-config/tests/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ const __filename = fileURLToPath(import.meta.url)
66
const __dirname = path.dirname(__filename)
77

88
it('read .js config', () => {
9-
const config = exploreConfig('master.css.js', { cwd: __dirname })
9+
const config = exploreConfig('master.css', { cwd: __dirname, extensions: ['js'] })
1010
expect(config).toEqual({ colors: { primary: '#ff0' } })
1111
})
1212

1313
it('read .ts config', () => {
14-
const config = exploreConfig('master.css.ts', { cwd: __dirname })
14+
const config = exploreConfig('master.css', { cwd: __dirname, extensions: ['ts'] })
1515
expect(config).toEqual({ colors: { primary: '#ff0' } })
1616
})
1717

1818
it('read .mjs config', () => {
19-
const config = exploreConfig('master.css.mjs', { cwd: __dirname })
19+
const config = exploreConfig('master.css', { cwd: __dirname, extensions: ['mjs'] })
2020
expect(config).toEqual({ colors: { primary: '#ff0' } })
2121
})
2222

2323
it('cannot read config', () => {
24-
const config = exploreConfig('vite.config.*')
24+
const config = exploreConfig('vite.config')
2525
expect(config).toBeUndefined()
2626
})

packages/techor/src/commands/build.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import clsx from 'clsx'
1616
import getWideExternal from '../utils/get-wide-external'
1717
import { BuildCommonOptions, Config, default as defaultConfig } from '../config'
1818
import yargsParser, { Options as YargsParserOptions } from 'yargs-parser'
19-
import exploreConfig from 'explore-config'
19+
import loadConfig from '../load-config'
2020

2121
// core plugins
2222
import esmShim from '../plugins/esm-shim'
@@ -50,7 +50,7 @@ export default async function build() {
5050
const { _, ...cmdConfig } = yargsParser(process.argv.slice(2), yargsParserOptions)
5151
const [commandName, ...commandInputs] = _ as [string, ...string[]]
5252
try {
53-
const useConfig = exploreConfig('techor.config.*') as Config
53+
const useConfig = loadConfig()
5454
const config = extend(defaultConfig, useConfig, { build: cmdConfig }) as Config
5555
if (commandName === 'dev') {
5656
process.env.NODE_ENV = 'development'
@@ -59,7 +59,7 @@ export default async function build() {
5959
process.env.NODE_ENV = 'production'
6060
}
6161

62-
if (process.env.DEBUG) console.log('[DEBUG] cmdConfig', cmdConfig)
62+
if (process.env.DEBUG) console.log('[techor] cmdConfig', cmdConfig)
6363
const pkg: PackageJson = readJSONFileSync('package.json') || {}
6464
const { dependencies, peerDependencies, optionalDependencies, types } = pkg
6565
const buildMap = new Map<RollupInputOptions['input'], BuildOptions>()
@@ -101,7 +101,7 @@ export default async function build() {
101101
for (const [eachInput, eachBuildOptions] of buildMap) {
102102
for (const eachOutputOptions of eachBuildOptions.outputOptionsList) {
103103
if (normalize(eachOutputOptions.output.file) === normalize(extendedBuild.output.file)) {
104-
if (process.env.DEBUG) console.log('[DEBUG] extendedBuild', extendedBuild)
104+
if (process.env.DEBUG) console.log('[techor] extendedBuild', extendedBuild)
105105
return
106106
}
107107
}
@@ -147,7 +147,7 @@ export default async function build() {
147147
]
148148
.filter((existence) => existence)
149149
)
150-
if (process.env.DEBUG) console.log('[DEBUG] buildOptions', buildOptions)
150+
if (process.env.DEBUG) console.log('[techor] buildOptions', buildOptions)
151151
buildMap.set(entries, buildOptions)
152152
}
153153
}

packages/techor/src/commands/version.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import yargsParser, { Options as YargsParserOptions } from 'yargs-parser'
77
import exploreConfig from 'explore-config'
88
import extend from '@techor/extend'
99
import { Config, default as defaultConfig } from '../config'
10+
import loadConfig from '../load-config'
1011

1112
export const yargsParserOptions: YargsParserOptions = {
1213
alias: {
@@ -23,7 +24,7 @@ export const yargsParserOptions: YargsParserOptions = {
2324
export default async function version() {
2425
const { _, ...cmdConfig } = yargsParser(process.argv.slice(2), yargsParserOptions)
2526
const [commandName, version] = _ as [string, ...string[]]
26-
const useConfig = exploreConfig('techor.config.*') as Config
27+
const useConfig = loadConfig()
2728
const config = extend(defaultConfig, useConfig, { version: cmdConfig }) as Config
2829
if (!config.version.workspaces) {
2930
const packageManager = explorePackageManager()

packages/techor/src/load-config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import exploreConfig from 'explore-config'
2+
import { Config } from './config'
3+
import log from '@techor/log'
4+
5+
export default function loadConfig() {
6+
const basename = 'techor.config'
7+
const useConfig = exploreConfig(basename, {
8+
found: (basename) => log.i`Loaded **${basename}**`
9+
}) as Config
10+
if (useConfig) {
11+
return useConfig
12+
}
13+
}

packages/techor/tests/master/sources/package.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/techor/tests/master/sources/packages/class-variant/.eslintrc.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

packages/techor/tests/master/sources/packages/class-variant/README.md

Lines changed: 0 additions & 56 deletions
This file was deleted.

packages/techor/tests/master/sources/packages/class-variant/jest.config.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)