Skip to content

Commit

Permalink
fix: mark ScanEnvironment internal (#18008)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Sep 3, 2024
1 parent bb9e12b commit 0f26342
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
14 changes: 9 additions & 5 deletions packages/vite/rollup.dts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,15 @@ function removeInternal(s: MagicString, node: any): boolean {
})
) {
// Examples:
// function a(foo: string, /* @internal */ bar: number)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// strip trailing comma
const end = s.original[node.end] === ',' ? node.end + 1 : node.end
s.remove(node.leadingComments[0].start, end)
// function a(foo: string, /* @internal */ bar: number, baz: boolean)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// type Enum = Foo | /* @internal */ Bar | Baz
// ^^^^^^^^^^^^^^^^^^^^^
// strip trailing comma or pipe
const trailingRe = /\s*[,|]/y
trailingRe.lastIndex = node.end
const trailingStr = trailingRe.exec(s.original)?.[0] ?? ''
s.remove(node.leadingComments[0].start, node.end + trailingStr.length)
return true
}
return false
Expand Down
30 changes: 19 additions & 11 deletions packages/vite/src/node/baseEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import type { Logger } from './logger'
import type { ResolvedConfig, ResolvedEnvironmentOptions } from './config'
import type { Plugin } from './plugin'

const environmentColors = [
colors.blue,
colors.magenta,
colors.green,
colors.gray,
]

export class PartialEnvironment {
name: string
getTopLevelConfig(): ResolvedConfig {
Expand Down Expand Up @@ -122,16 +129,17 @@ export class BaseEnvironment extends PartialEnvironment {
}

/**
* This is used both to avoid users to hardcode conditions like
* !scan && !build => dev
* This class discourages users from inversely checking the `mode`
* to determine the type of environment, e.g.
*
* ```js
* const isDev = environment.mode !== 'build' // bad
* const isDev = environment.mode === 'dev' // good
* ```
*
* You should also not check against `"unknown"` specfically. It's
* a placeholder for more possible environment types.
*/
export class FutureCompatEnvironment extends BaseEnvironment {
mode = 'futureCompat' as const
export class UnknownEnvironment extends BaseEnvironment {
mode = 'unknown' as const
}

const environmentColors = [
colors.blue,
colors.magenta,
colors.green,
colors.gray,
]
6 changes: 3 additions & 3 deletions packages/vite/src/node/environment.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { DevEnvironment } from './server/environment'
import type { BuildEnvironment } from './build'
import type { ScanEnvironment } from './optimizer/scan'
import type { FutureCompatEnvironment } from './baseEnvironment'
import type { UnknownEnvironment } from './baseEnvironment'
import type { PluginContext } from './plugin'

export type Environment =
| DevEnvironment
| BuildEnvironment
| ScanEnvironment
| FutureCompatEnvironment
| /** @internal */ ScanEnvironment
| UnknownEnvironment

/**
* Creates a function that hides the complexities of a WeakMap with an initial value
Expand Down

0 comments on commit 0f26342

Please sign in to comment.