Skip to content

Commit 5c65888

Browse files
chore: minify better
1 parent 918d628 commit 5c65888

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

src/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import { it, expect } from 'vitest'
3-
import { render, createPortal, type HostContainer } from './index'
3+
import { render, createPortal, type HostContainer } from 'react-nil'
44

55
// Let React know that we'll be testing effectful components
66
declare global {

src/index.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import {
88
ConcurrentRoot,
99
} from 'react-reconciler/constants.js'
1010

11-
// @ts-ignore
12-
const __DEV__ = /* @__PURE__ */ (() => typeof process !== 'undefined' && process.env.NODE_ENV !== 'production')()
13-
1411
// TODO: upstream to DefinitelyTyped for React 19
1512
// https://github.com/facebook/react/issues/28956
1613
type EventPriority = number
@@ -112,7 +109,7 @@ function createReconciler<
112109
const reconciler = Reconciler(config as any)
113110

114111
reconciler.injectIntoDevTools({
115-
bundleType: __DEV__ ? 1 : 0,
112+
bundleType: typeof process !== 'undefined' && process.env.NODE_ENV !== 'production' ? 1 : 0,
116113
rendererPackageName: 'react-nil',
117114
version: React.version,
118115
})
@@ -263,13 +260,13 @@ const reconciler = /* @__PURE__ */ createReconciler<
263260

264261
// Report when an error was detected in a previous render
265262
// https://github.com/facebook/react/pull/23207
266-
const logRecoverableError = /* @__PURE__ */ (() =>
267-
typeof reportError === 'function'
268-
? // In modern browsers, reportError will dispatch an error event,
269-
// emulating an uncaught JavaScript error.
270-
reportError
271-
: // In older browsers and test environments, fallback to console.error.
272-
console.error)()
263+
function logRecoverableError(error: any): void {
264+
// In modern browsers, reportError will dispatch an error event,
265+
// emulating an uncaught JavaScript error.
266+
if (typeof reportError === 'function') return reportError(error)
267+
// In older browsers and test environments, fallback to console.error.
268+
else return console.error(error)
269+
}
273270

274271
const container: HostContainer = { head: null }
275272
const root = /* @__PURE__ */ (reconciler as any).createContainer(

vite.config.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import * as vite from 'vite'
12
import * as path from 'node:path'
2-
import { defineConfig } from 'vite'
33

4-
export default defineConfig({
4+
export default vite.defineConfig({
5+
resolve: {
6+
alias: {
7+
'react-nil': path.resolve(__dirname, 'src/index.tsx'),
8+
},
9+
},
510
build: {
6-
minify: false,
711
sourcemap: true,
812
target: 'es2020',
913
lib: {
@@ -26,5 +30,18 @@ export default defineConfig({
2630
this.emitFile({ type: 'asset', fileName: `index.d.${ext}`, source: `export * from '../src/index.tsx'` })
2731
},
2832
},
33+
{
34+
name: 'vite-minify',
35+
renderChunk: {
36+
order: 'post',
37+
async handler(code, { fileName }) {
38+
// Preserve pure annotations, but remove all other comments and whitespace
39+
code = code.replaceAll('/* @__PURE__ */', '__PURE__ || ')
40+
const result = await vite.transformWithEsbuild(code, fileName, { minify: true, target: 'es2020' })
41+
result.code = result.code.replaceAll('__PURE__||', '/*@__PURE__*/')
42+
return result
43+
},
44+
},
45+
},
2946
],
3047
})

0 commit comments

Comments
 (0)