Skip to content

Commit

Permalink
chore: minify better
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Jan 13, 2025
1 parent 918d628 commit 5c65888
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'
import { it, expect } from 'vitest'
import { render, createPortal, type HostContainer } from './index'
import { render, createPortal, type HostContainer } from 'react-nil'

// Let React know that we'll be testing effectful components
declare global {
Expand Down
19 changes: 8 additions & 11 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import {
ConcurrentRoot,
} from 'react-reconciler/constants.js'

// @ts-ignore
const __DEV__ = /* @__PURE__ */ (() => typeof process !== 'undefined' && process.env.NODE_ENV !== 'production')()

// TODO: upstream to DefinitelyTyped for React 19
// https://github.com/facebook/react/issues/28956
type EventPriority = number
Expand Down Expand Up @@ -112,7 +109,7 @@ function createReconciler<
const reconciler = Reconciler(config as any)

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

// Report when an error was detected in a previous render
// https://github.com/facebook/react/pull/23207
const logRecoverableError = /* @__PURE__ */ (() =>
typeof reportError === 'function'
? // In modern browsers, reportError will dispatch an error event,
// emulating an uncaught JavaScript error.
reportError
: // In older browsers and test environments, fallback to console.error.
console.error)()
function logRecoverableError(error: any): void {
// In modern browsers, reportError will dispatch an error event,
// emulating an uncaught JavaScript error.
if (typeof reportError === 'function') return reportError(error)
// In older browsers and test environments, fallback to console.error.
else return console.error(error)
}

const container: HostContainer = { head: null }
const root = /* @__PURE__ */ (reconciler as any).createContainer(
Expand Down
23 changes: 20 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import * as vite from 'vite'
import * as path from 'node:path'
import { defineConfig } from 'vite'

export default defineConfig({
export default vite.defineConfig({
resolve: {
alias: {
'react-nil': path.resolve(__dirname, 'src/index.tsx'),
},
},
build: {
minify: false,
sourcemap: true,
target: 'es2020',
lib: {
Expand All @@ -26,5 +30,18 @@ export default defineConfig({
this.emitFile({ type: 'asset', fileName: `index.d.${ext}`, source: `export * from '../src/index.tsx'` })
},
},
{
name: 'vite-minify',
renderChunk: {
order: 'post',
async handler(code, { fileName }) {
// Preserve pure annotations, but remove all other comments and whitespace
code = code.replaceAll('/* @__PURE__ */', '__PURE__ || ')
const result = await vite.transformWithEsbuild(code, fileName, { minify: true, target: 'es2020' })
result.code = result.code.replaceAll('__PURE__||', '/*@__PURE__*/')
return result
},
},
},
],
})

0 comments on commit 5c65888

Please sign in to comment.