Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-spoons-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/table-core': patch
---

fix(table-core): guard `process.env` reads so the package works in bundler-less environments without a `process` global
5 changes: 3 additions & 2 deletions packages/table-core/src/core/columns/constructColumn.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isDevelopmentEnv } from '../../utils'
import type { Table_Internal } from '../../types/Table'
import type { CellData, RowData } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
Expand Down Expand Up @@ -74,7 +75,7 @@ export function constructColumn<
for (let i = 0; i < keys.length; i++) {
const key = keys[i]!
result = result?.[key]
if (process.env.NODE_ENV === 'development' && result === undefined) {
if (isDevelopmentEnv() && result === undefined) {
console.warn(
`"${key}" in deeply nested key "${accessorKey}" returned undefined.`,
)
Expand All @@ -90,7 +91,7 @@ export function constructColumn<
}

if (!id) {
if (process.env.NODE_ENV === 'development') {
if (isDevelopmentEnv()) {
throw new Error(
resolvedColumnDef.accessorFn
? `coreColumnsFeature require an id when using an accessorFn`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeObjectMap } from '../../utils'
import { isDevelopmentEnv, makeObjectMap } from '../../utils'
import { orderLeafColumns } from '../featureStubs'
import { constructColumn } from './constructColumn'
import type { Table_Internal } from '../../types/Table'
Expand Down Expand Up @@ -272,7 +272,7 @@ export function table_getColumn<
): Column<TFeatures, TData, unknown> | undefined {
const column = table.getAllFlatColumnsById()[columnId]

if (process.env.NODE_ENV === 'development' && !column) {
if (isDevelopmentEnv() && !column) {
console.warn(`[Table] Column with id '${columnId}' does not exist.`)
}

Expand Down
9 changes: 7 additions & 2 deletions packages/table-core/src/core/rows/coreRowsFeature.utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { flattenBy, hasOwn, makeObjectMap } from '../../utils'
import {
flattenBy,
hasOwn,
isDevelopmentEnv,
makeObjectMap,
} from '../../utils'
import { constructCell } from '../cells/constructCell'
import { expandRowModel } from '../featureStubs'
import type { Table_Internal } from '../../types/Table'
Expand Down Expand Up @@ -333,7 +338,7 @@ export function table_getRow<
if (!row) {
row = table.getCoreRowModel().rowsById[rowId]
if (!row) {
if (process.env.NODE_ENV === 'development') {
if (isDevelopmentEnv()) {
throw new Error(`getRow could not find row with ID: ${rowId}`)
}
throw new Error()
Expand Down
4 changes: 2 additions & 2 deletions packages/table-core/src/core/table/constructTable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { shallow } from '@tanstack/store'
import { coreFeatures } from '../coreFeatures'
import { cloneState, hasOwn } from '../../utils'
import { cloneState, hasOwn, isDevelopmentEnv } from '../../utils'
import { atomToStore } from '../reactivity/coreReactivityFeature.utils'
import { table_syncExternalStateToBaseAtoms } from './coreTablesFeature.utils'
import type { Atom } from '@tanstack/store'
Expand Down Expand Up @@ -223,7 +223,7 @@ export function constructTable<
}

if (
process.env.NODE_ENV === 'development' &&
isDevelopmentEnv() &&
(tableOptions.debugAll || tableOptions.debugTable)
) {
const features = Object.keys(table._features)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
cloneState,
functionalUpdate,
isDevelopmentEnv,
isFunction,
setStateSlice,
} from '../../utils'
Expand Down Expand Up @@ -85,7 +86,7 @@ export function column_getAutoFilterFn<

const filterFn = filterFns?.[filterFnName]

if (process.env.NODE_ENV === 'development' && !filterFn) {
if (isDevelopmentEnv() && !filterFn) {
console.warn(
`filterFn '${filterFnName}' (auto) for column '${column.id}' is not registered`,
)
Expand Down Expand Up @@ -123,7 +124,7 @@ export function column_getFilterFn<
: filterFns?.[column.columnDef.filterFn as string]

if (
process.env.NODE_ENV === 'development' &&
isDevelopmentEnv() &&
!filterFn &&
column.columnDef.filterFn !== 'auto' // the auto picker warns on its own
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filterFn_includesString } from '../column-filtering/filterFns'
import { cloneState, isFunction } from '../../utils'
import { cloneState, isDevelopmentEnv, isFunction } from '../../utils'
import type { Column_Internal } from '../../types/Column'
import type { FilterFn } from '../column-filtering/columnFilteringFeature.types'
import type { CellData, RowData } from '../../types/type-utils'
Expand Down Expand Up @@ -75,7 +75,7 @@ export function table_getGlobalFilterFn<
: filterFns?.[globalFilterFn as string]

if (
process.env.NODE_ENV === 'development' &&
isDevelopmentEnv() &&
!filterFn &&
globalFilterFn != null
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasOwn, makeObjectMap } from '../../utils'
import { hasOwn, isDevelopmentEnv, makeObjectMap } from '../../utils'
import type { Cell } from '../../types/Cell'
import type { Column, Column_Internal } from '../../types/Column'
import type { Row } from '../../types/Row'
Expand Down Expand Up @@ -192,7 +192,7 @@ export function column_getAutoAggregationFn<

const aggregationFn = column.table._rowModelFns.aggregationFns?.[name]

if (process.env.NODE_ENV === 'development' && !aggregationFn) {
if (isDevelopmentEnv() && !aggregationFn) {
console.warn(
`aggregationFn '${name}' (auto) for column '${column.id}' is not registered`,
)
Expand All @@ -213,7 +213,7 @@ function resolveAggregationFn<

const aggregationFn =
column.table._rowModelFns.aggregationFns?.[ref as string]
if (process.env.NODE_ENV === 'development' && !aggregationFn) {
if (isDevelopmentEnv() && !aggregationFn) {
console.warn(
`aggregationFn '${String(ref)}' for column '${column.id}' is not registered`,
)
Expand Down Expand Up @@ -291,7 +291,7 @@ export function column_getAggregationFns<
: undefined

if (id === undefined) {
if (process.env.NODE_ENV === 'development') {
if (isDevelopmentEnv()) {
console.warn(
`aggregationFn at index ${i} for column '${column.id}' needs a stable id`,
)
Expand All @@ -301,7 +301,7 @@ export function column_getAggregationFns<
}

if (ids[id]! > 1) {
if (process.env.NODE_ENV === 'development') {
if (isDevelopmentEnv()) {
console.warn(
`aggregationFn id '${id}' for column '${column.id}' is duplicated`,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { cloneState, isFunction, setStateSlice } from '../../utils'
import {
cloneState,
isDevelopmentEnv,
isFunction,
setStateSlice,
} from '../../utils'
import { reSplitAlphaNumeric, sortFn_basic } from './sortFns'
import type { CellData, RowData, Updater } from '../../types/type-utils'
import type { TableFeatures } from '../../types/TableFeatures'
Expand Down Expand Up @@ -146,7 +151,7 @@ export function column_getAutoSortFn<
let sortFn = sortFns?.[sortFnName]

if (!sortFn) {
if (process.env.NODE_ENV === 'development') {
if (isDevelopmentEnv()) {
console.warn(
`sortFn '${sortFnName}' (auto) for column '${column.id}' is not registered`,
)
Expand Down Expand Up @@ -230,7 +235,7 @@ export function column_getSortFn<

const sortFn = sortFns?.[column.columnDef.sortFn as string]

if (process.env.NODE_ENV === 'development' && !sortFn) {
if (isDevelopmentEnv() && !sortFn) {
console.warn(
`sortFn '${String(column.columnDef.sortFn)}' for column '${column.id}' is not registered`,
)
Expand Down
21 changes: 19 additions & 2 deletions packages/table-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,23 @@ export function setStateSlice<K extends (string & {}) | keyof TableState_All>(

type AnyFunction = (...args: any) => any

/**
* Returns whether the runtime is a development environment.
*
* Reads `process.env.NODE_ENV` when the runtime provides a `process` global.
* Bundler-less environments (plain browsers, embedded runtimes) have no
* `process`, and are treated as production so dev-only checks are skipped
* instead of throwing. A bundler replacement of `process.env.NODE_ENV` with
* `'development'` is still honored because it makes the direct read safe.
*/
export function isDevelopmentEnv(): boolean {
try {
return process.env.NODE_ENV === 'development'
} catch {
return false
}
}

/**
* Returns whether a value is a function.
*/
Expand Down Expand Up @@ -405,7 +422,7 @@ export function tableMemo<
let debug: boolean | undefined
let debugCache: boolean | undefined

if (process.env.NODE_ENV === 'development') {
if (isDevelopmentEnv()) {
const { debugAll } = table.options
const { parentName } = getFunctionNameInfo(fnName, '.')

Expand Down Expand Up @@ -465,7 +482,7 @@ export function tableMemo<
: undefined

const debugOptions =
process.env.NODE_ENV === 'development'
isDevelopmentEnv()
? {
onBeforeCompare: () => {
if (debugCache) {
Expand Down
4 changes: 2 additions & 2 deletions packages/table-core/src/worker/createWorkerRowModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tableMemo } from '../utils'
import { isDevelopmentEnv, tableMemo } from '../utils'
import { getTableWorkerBridge, syncTableWorker } from './createTableWorker'
import { rebuildRowModel } from './rebuildRowModel'
import { tableWorkerPipeline } from './tableWorkerProtocol'
Expand Down Expand Up @@ -60,7 +60,7 @@ export function createWorkerRowModel(
let warned = false

const warnOnce = (message: string) => {
if (process.env.NODE_ENV === 'development' && !warned) {
if (isDevelopmentEnv() && !warned) {
warned = true
console.warn(`[table-worker] ${message}`)
}
Expand Down
23 changes: 23 additions & 0 deletions packages/table-core/tests/unit/processEnv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { constructTable } from '../../src'
import { testFeatures } from '../fixtures/features'

describe('bundler-less environments without a process global', () => {
afterEach(() => {
vi.unstubAllGlobals()
})

it('constructs a table when process is undefined', () => {
vi.stubGlobal('process', undefined)
expect(typeof process).toBe('undefined')

const table = constructTable({
features: testFeatures({}),
columns: [{ id: 'firstName', accessorKey: 'firstName' }],
data: [{ firstName: 'Tanner' }],
} as any) as any

expect(table).toBeTruthy()
expect(table.getRowModel().rows).toHaveLength(1)
})
})