Skip to content

Commit

Permalink
feat: remove config.build from dynamicImportVars plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Apr 29, 2024
1 parent a6fc1dd commit 8231283
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 14 additions & 0 deletions packages/vite/src/node/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,17 @@ const environmentColors = [
colors.green,
colors.gray,
]

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
}
}
15 changes: 10 additions & 5 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { ImportSpecifier } from 'es-module-lexer'
import { parse as parseJS } from 'acorn'
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
import type { Plugin } from '../plugin'
import type { Environment } from '../environment'
import { cachedByEnvironment } from '../environment'
import type { ResolvedConfig } from '../config'
import { CLIENT_ENTRY } from '../constants'
import { createIdResolver } from '../idResolver'
Expand Down Expand Up @@ -159,9 +161,12 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
tryIndex: false,
extensions: [],
})
const { include, exclude, warnOnError } =
config.build.dynamicImportVarsOptions
const filter = createFilter(include, exclude)

const getFilter = cachedByEnvironment((environment: Environment) => {
const { include, exclude } =
environment.options.build.dynamicImportVarsOptions
return createFilter(include, exclude)
})

return {
name: 'vite:dynamic-import-vars',
Expand All @@ -182,7 +187,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
const { environment } = this
if (
!environment ||
!filter(importer) ||
!getFilter(environment)(importer) ||
importer === CLIENT_ENTRY ||
!hasDynamicImportRE.test(source)
) {
Expand Down Expand Up @@ -233,7 +238,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
config.root,
)
} catch (error) {
if (warnOnError) {
if (environment.options.build.dynamicImportVarsOptions.warnOnError) {
this.warn(error)
} else {
this.error(error)
Expand Down

0 comments on commit 8231283

Please sign in to comment.