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

Commit

Permalink
fix(config): normalize buildInclude & buildExclude + improved default…
Browse files Browse the repository at this point in the history
… buildInclude
  • Loading branch information
Akryum committed Dec 10, 2021
1 parent de29b2a commit 41fa34d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/guide/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ describe('file system', () => {
```
## buildExclude

An array of RegExp, module names or function of the form `(absolutePath: string) => boolean` that should not be processed during building. This can improve performance.
An array of RegExp, module names or functions of the form `(absolutePath: string) => boolean` that should not be processed during building. This can improve performance.

Default value is `[/node_modules/]`.

Expand All @@ -352,7 +352,7 @@ export default defineConfig({

## buildInclude

An array of RegExp, module names or function of the form `(absolutePath: string) => boolean` that should be processed during building. This is useful if some packages you use are using ESM format but are not correctly set up to tell Node.js to use ESM. This takes precedence over `buildExclude`.
An array of RegExp, module names or functions of the form `(absolutePath: string) => boolean` that should be processed during building. This is useful if some packages you use are using ESM format but are not correctly set up to tell Node.js to use ESM. This takes precedence over `buildExclude`.

Default value is `[/node_modules\/(vue|@vue|diff)/]`. It will be merged with your configuration.

Expand Down
13 changes: 12 additions & 1 deletion packages/peeky-config/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ export const defaultPeekyConfig: () => PeekyConfig = () => ({
emptySuiteError: false,
collectCoverageMatch: ['(src|lib)/**/*.(ts|js)'],
buildExclude: [/node_modules/],
buildInclude: [/node_modules\/(vue|@vue|diff|tslib)/],
buildInclude: [
'vitest/dist',
'vitest/src',
'@vue',
'@vueuse',
'vue-demi',
'vue',
/virtual:/,
/\.ts$/,
/\/esm\/.*\.js$/,
/\.(es|esm|esm-browser|esm-bundler|es6).js$/,
],
runtimeEnv: 'node',
mockFs: true,
})
3 changes: 2 additions & 1 deletion packages/peeky-config/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { setupConfigContentLoader } from './fs.js'
import { transformConfigCode } from './transform.js'
import { defaultPeekyConfig } from './defaults.js'
import { mergeConfig } from './util.js'
import { processConfig } from './process.js'

export interface PeekyConfigLoaderOptions {
baseDir?: string
Expand Down Expand Up @@ -45,7 +46,7 @@ export async function setupConfigLoader (options: PeekyConfigLoaderOptions = {})

config = mergeConfig(defaultPeekyConfig(), config)

return config
return processConfig(config)
} catch (e) {
if (fs.existsSync(resolvedPath)) {
fs.unlinkSync(resolvedPath)
Expand Down
21 changes: 21 additions & 0 deletions packages/peeky-config/src/process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ModuleFilterOption, PeekyConfig } from './types.js'

export function processConfig (config: PeekyConfig): PeekyConfig {
config.buildInclude = normalizeModuleFilters(config.buildInclude)
config.buildExclude = normalizeModuleFilters(config.buildExclude)
return config
}

function normalizeModuleFilters (filters: ModuleFilterOption) {
const filtersList = Array.isArray(filters) ? filters : [filters]
return filtersList.map(filter => {
if (typeof filter === 'string') {
filter = new RegExp(`node_modules/${escapeRegExp(filter)}`)
}
return filter
})
}

function escapeRegExp (text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
}
8 changes: 4 additions & 4 deletions packages/peeky-config/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { UserConfig as ViteConfig } from 'vite'
import { Awaitable } from '@peeky/utils'

export type ModuleFilter = string | RegExp | ((absolutePath: string) => boolean)

export type ModuleFilterOption =
| (string | RegExp)[]
| string
| RegExp
| ((absolutePath: string) => boolean)
| (ModuleFilter)[]
| ModuleFilter

export interface PeekyConfig {
targetDirectory?: string
Expand Down
30 changes: 9 additions & 21 deletions packages/peeky-runner/src/runtime/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import chalk from 'chalk'
import shortid from 'shortid'
import isEqual from 'lodash/isEqual.js'
import { isValidNodeImport } from 'mlly'
import type { ModuleFilterOption } from '@peeky/config'
import type { ModuleFilterOption, ModuleFilter } from '@peeky/config'
import { slash } from '@peeky/utils'
import { moduleCache, sourceMaps } from './module-cache.js'
import { mockedModules } from './mocked-files.js'
Expand Down Expand Up @@ -289,28 +289,20 @@ function exportAll (exports: any, sourceModule: any) {
* @returns
*/
function shouldExternalize (filePath: string): boolean {
if (typeof currentOptions.include === 'function') {
if (currentOptions.include(filePath)) {
return false
}
} else if (matchModuleFilter(currentOptions.include, filePath)) {
if (matchModuleFilter(currentOptions.include as ModuleFilter[], filePath)) {
return false
}

if (typeof currentOptions.exclude === 'function') {
return currentOptions.exclude(filePath)
}

return matchModuleFilter(currentOptions.exclude, filePath)
return matchModuleFilter(currentOptions.exclude as ModuleFilter[], filePath)
}

function matchModuleFilter (filters: (string | RegExp) | (string | RegExp)[], filePath: string): boolean {
const filtersList = Array.isArray(filters) ? filters : [filters]
return filtersList.some(filter => {
if (typeof filter === 'string') {
filter = new RegExp(`node_modules/${escapeRegExp(filter)}`)
function matchModuleFilter (filters: ModuleFilter[], filePath: string): boolean {
return filters.some(filter => {
if (typeof filter === 'function') {
return filter(filePath)
} else {
(filter as RegExp).test(filePath)
}
return filter.test(filePath)
})
}

Expand All @@ -332,7 +324,3 @@ async function transform (id: string) {
return await viteServer.ssrTransform(result.code, result.map, id)
}
}

function escapeRegExp (text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
}

0 comments on commit 41fa34d

Please sign in to comment.