Skip to content

Commit da73788

Browse files
committed
Switch to a global __DEV__
1 parent 9d12d54 commit da73788

File tree

7 files changed

+49
-23
lines changed

7 files changed

+49
-23
lines changed

Diff for: packages/docs/.vitepress/config.mts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfigWithTheme } from 'vitepress'
22
import { resolve } from 'node:path'
33

4-
export default defineConfigWithTheme({
4+
export default ({ mode }: { mode: string }) => defineConfigWithTheme({
55
srcDir: './src',
66
outDir: './dist',
77
base: '/vue-vnode-utils',
@@ -14,6 +14,10 @@ export default defineConfigWithTheme({
1414
alias: {
1515
'@skirtle/vue-vnode-utils': resolve(__dirname, '../../vue-vnode-utils/src/index.ts')
1616
}
17+
},
18+
19+
define: {
20+
__DEV__: JSON.stringify(mode !== 'production')
1721
}
1822
},
1923

Diff for: packages/vue-vnode-utils/global.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare let __DEV__: boolean

Diff for: packages/vue-vnode-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"vue": "^3.2.0"
3535
},
3636
"devDependencies": {
37+
"@rollup/plugin-replace": "^6.0.1",
3738
"@tsconfig/node18": "^18.2.4",
3839
"@types/jsdom": "^21.1.7",
3940
"@types/node": "^22.7.4",

Diff for: packages/vue-vnode-utils/src/index.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import {
1515
type VNodeChild
1616
} from 'vue'
1717

18-
// @ts-ignore
19-
const DEV = process.env.NODE_ENV !== 'production'
20-
2118
export const isComment = (vnode: unknown): vnode is (null | undefined | boolean | (VNode & { type: typeof CommentVNode })) => {
2219
return getType(vnode) === 'comment'
2320
}
@@ -157,7 +154,7 @@ const getFragmentChildren = (fragmentVNode: VNode | VNodeArrayChildren): VNodeAr
157154
return children
158155
}
159156

160-
if (DEV) {
157+
if (__DEV__) {
161158
warn('getFragmentChildren', `Unknown children for fragment: ${typeOf(children)}`)
162159
}
163160

@@ -174,7 +171,7 @@ export type IterationOptions = {
174171

175172
// esbuild can remove an identity function, so long as it uses a function declaration
176173
function freeze<T>(obj: T): T {
177-
if (DEV) {
174+
if (__DEV__) {
178175
return Object.freeze(obj)
179176
}
180177

@@ -225,14 +222,14 @@ export const addProps = (
225222
callback: (vnode: VNode) => (Record<string, unknown> | null | void),
226223
options: IterationOptions = COMPONENTS_AND_ELEMENTS
227224
): VNodeArrayChildren => {
228-
if (DEV) {
225+
if (__DEV__) {
229226
checkArguments('addProps', [children, callback, options], ['array', 'function', 'object'])
230227
}
231228

232229
return replaceChildrenInternal(children, (vnode) => {
233230
const props = callback(vnode)
234231

235-
if (DEV) {
232+
if (__DEV__) {
236233
const typeofProps = typeOf(props)
237234

238235
if (!['object', 'null', 'undefined'].includes(typeofProps)) {
@@ -251,7 +248,7 @@ export const replaceChildren = (
251248
callback: (vnode: VNode) => (VNode | VNodeArrayChildren | string | number | void),
252249
options: IterationOptions = SKIP_COMMENTS
253250
): VNodeArrayChildren => {
254-
if (DEV) {
251+
if (__DEV__) {
255252
checkArguments('replaceChildren', [children, callback, options], ['array', 'function', 'object'])
256253
}
257254

@@ -293,7 +290,7 @@ const replaceChildrenInternal = (
293290
if (vnode) {
294291
const newNodes = callback(vnode) ?? vnode
295292

296-
if (DEV) {
293+
if (__DEV__) {
297294
const typeOfNewNodes = typeOf(newNodes)
298295

299296
if (!['array', 'vnode', 'string', 'number', 'undefined'].includes(typeOfNewNodes)) {
@@ -324,7 +321,7 @@ export const betweenChildren = (
324321
callback: (previousVNode: VNode, nextVNode: VNode) => (VNode | VNodeArrayChildren | string | number | void),
325322
options: IterationOptions = SKIP_COMMENTS
326323
): VNodeArrayChildren => {
327-
if (DEV) {
324+
if (__DEV__) {
328325
checkArguments('betweenChildren', [children, callback, options], ['array', 'function', 'object'])
329326
}
330327

@@ -336,7 +333,7 @@ export const betweenChildren = (
336333
if (previousVNode) {
337334
insertedNodes = callback(previousVNode, vnode)
338335

339-
if (DEV) {
336+
if (__DEV__) {
340337
const typeOfInsertedNodes = typeOf(insertedNodes)
341338

342339
if (!['array', 'vnode', 'string', 'number', 'undefined'].includes(typeOfInsertedNodes)) {
@@ -364,7 +361,7 @@ export const someChild = (
364361
callback: (vnode: VNode) => unknown,
365362
options: IterationOptions = ALL_VNODES
366363
): boolean => {
367-
if (DEV) {
364+
if (__DEV__) {
368365
checkArguments('someChild', [children, callback, options], ['array', 'function', 'object'])
369366
}
370367

@@ -398,7 +395,7 @@ export const everyChild = (
398395
callback: (vnode: VNode) => unknown,
399396
options: IterationOptions = ALL_VNODES
400397
): boolean => {
401-
if (DEV) {
398+
if (__DEV__) {
402399
checkArguments('everyChild', [children, callback, options], ['array', 'function', 'object'])
403400
}
404401

@@ -410,7 +407,7 @@ export const eachChild = (
410407
callback: (vnode: VNode) => void,
411408
options: IterationOptions = ALL_VNODES
412409
): void => {
413-
if (DEV) {
410+
if (__DEV__) {
414411
checkArguments('eachChild', [children, callback, options], ['array', 'function', 'object'])
415412
}
416413

@@ -424,7 +421,7 @@ export const findChild = (
424421
callback: (vnode: VNode) => unknown,
425422
options: IterationOptions = ALL_VNODES
426423
): (VNode | undefined) => {
427-
if (DEV) {
424+
if (__DEV__) {
428425
checkArguments('findChild', [children, callback, options], ['array', 'function', 'object'])
429426
}
430427

@@ -443,7 +440,7 @@ export const findChild = (
443440
const COLLAPSIBLE_WHITESPACE_RE = /\S|\u00a0/
444441

445442
export const isEmpty = (children: VNodeArrayChildren): boolean => {
446-
if (DEV) {
443+
if (__DEV__) {
447444
checkArguments('isEmpty', [children], ['array'])
448445
}
449446

@@ -459,15 +456,15 @@ export const isEmpty = (children: VNodeArrayChildren): boolean => {
459456
}
460457

461458
export const extractSingleChild = (children: VNodeArrayChildren): VNode | undefined => {
462-
if (DEV) {
459+
if (__DEV__) {
463460
checkArguments('extractSingleChild', [children], ['array'])
464461
}
465462

466463
const node = findChild(children, () => {
467464
return true
468465
}, COMPONENTS_AND_ELEMENTS)
469466

470-
if (DEV) {
467+
if (__DEV__) {
471468
someChildInternal(children, (vnode) => {
472469
let warning = ''
473470

Diff for: packages/vue-vnode-utils/tsconfig.app.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "@vue/tsconfig/tsconfig.dom.json",
3-
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
3+
"include": ["*.d.ts", "src/**/*", "src/**/*.vue"],
44
"exclude": ["src/**/__tests__/*"],
55
"compilerOptions": {
66
"composite": true,

Diff for: packages/vue-vnode-utils/vite.config.mts

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve } from 'node:path'
22
import { fileURLToPath, URL } from 'node:url'
33

44
import { defineConfig } from 'vite'
5+
import replace from '@rollup/plugin-replace'
56
import vue from '@vitejs/plugin-vue'
67
import dts from 'vite-plugin-dts'
78

@@ -17,6 +18,12 @@ export default defineConfig(({ mode }) => {
1718

1819
return {
1920
plugins: [
21+
replace({
22+
preventAssignment: true,
23+
values: {
24+
__DEV__: mode === 'production' ? 'false' : mode === 'development' ? 'true' : '!(process.env.NODE_ENV === "production")'
25+
}
26+
}),
2027
vue(),
2128
dtsPlugin
2229
],
@@ -25,9 +32,6 @@ export default defineConfig(({ mode }) => {
2532
'@': fileURLToPath(new URL('./src', import.meta.url))
2633
}
2734
},
28-
define: ['production', 'development'].includes(mode) ? {
29-
'process.env.NODE_ENV': JSON.stringify(mode)
30-
} : {},
3135
build: {
3236
target: 'es2019',
3337
emptyOutDir: false,

Diff for: pnpm-lock.yaml

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)