Skip to content

Commit

Permalink
feat: createWeakData
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed May 23, 2024
1 parent 778e39d commit bcb1959
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
14 changes: 0 additions & 14 deletions packages/vite/src/node/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,3 @@ export type Environment =
| BuildEnvironment
| ScanEnvironment
| FutureCompatEnvironment

export function cachedByEnvironment<Data>(
create: (environment: Environment) => Data,
): (environment: Environment) => Data {
const cache = new WeakMap<Environment, Data>()
return function (environment: Environment) {
let data = cache.get(environment)
if (!data) {
data = create(environment)
cache.set(environment, data)
}
return data
}
}
4 changes: 2 additions & 2 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export { createServer } from './server'
export { preview } from './preview'
export { build, createBuilder } from './build'

// TODO: Can we remove this?
// export { optimizeDeps } from './optimizer'
export { optimizeDeps } from './optimizer'

export { formatPostcssSourceMap, preprocessCSS } from './plugins/css'
export { transformWithEsbuild } from './plugins/esbuild'
Expand Down Expand Up @@ -53,6 +52,7 @@ export type {
ResolvedDevEnvironmentOptions,
} from './config'
export type { Plugin, PluginOption, HookHandler } from './plugin'
export type { Environment } from './environment'
export type { FilterPattern } from './utils'
export type { CorsOptions, CorsOrigin, CommonServerOptions } from './http'
export type {
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import { CLIENT_ENTRY } from '../constants'
import { createIdResolver } from '../idResolver'
import {
createFilter,
createWeakData,
normalizePath,
rawRE,
requestQueryMaybeEscapedSplitRE,
requestQuerySplitRE,
transformStableResult,
urlRE,
} from '../utils'
import { type Environment, cachedByEnvironment } from '../environment'
import type { Environment } from '../environment'
import { toAbsoluteGlob } from './importMetaGlob'
import { hasViteIgnoreRE } from './importAnalysis'
import { workerOrSharedWorkerRE } from './worker'
Expand Down Expand Up @@ -174,7 +175,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
extensions: [],
})

const getFilter = cachedByEnvironment((environment: Environment) => {
const getFilter = createWeakData((environment: Environment) => {
const { include, exclude } =
environment.options.build.dynamicImportVarsOptions
return createFilter(include, exclude)
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/publicUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
mergeConfig,
mergeAlias,
createFilter,
createWeakData,
rollupVersion,
} from './utils'
export { send } from './server/send'
Expand Down
19 changes: 19 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1438,3 +1438,22 @@ export function partialEncodeURIPath(uri: string): string {
const postfix = filePath !== uri ? uri.slice(filePath.length) : ''
return filePath.replaceAll('%', '%25') + postfix
}

/**
* Creates a function that hides the complexities of a WeakMap with an initial value
* to implement object metadata. Used by plugins to implement cross hooks per
* environment metadata
*/
export function createWeakData<Key extends WeakKey, Data>(
initial: (key: Key) => Data,
): (key: Key) => Data {
const cache = new WeakMap<Key, Data>()
return function (key: Key) {
let data = cache.get(key)
if (!data) {
data = initial(key)
cache.set(key, data)
}
return data
}
}

0 comments on commit bcb1959

Please sign in to comment.