Unverified Commit 2225e589 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

[dev-overlay] minify css (#76370)

### Why?

Our internal comments in the css literal templates, are being shipped to
the client along with the dev overlay, which is a bad practice.


https://github.com/vercel/next.js/blob/7f5a1d39e8a94b0b424ef9538be9dd2802e6ccb6/packages/next/src/client/components/react-dev-overlay/ui/components/errors/error-overlay-bottom-stack/index.tsx#L104-L107

### How?

Added minifying css util that includes removing comments.

| Before | After |
|--------|--------|
| ![CleanShot 2025-02-22 at 17 56
33@2x](https://github.com/user-attachments/assets/7af3d4a2-2ca2-4702-991d-beef84587837)
| ![CleanShot 2025-02-22 at 17 56
18@2x](https://github.com/user-attachments/assets/8fa9a3b7-b475-448f-a4ff-63f517f8df90)
|

- Renamed `noop` util to `css`.
- Added minifying in `css` util.
- Added unit test for `css` util.
- Removed unnecessary `css` util usage as it's only required for the
entry point (where it injects to `<style>` tag).
- Updated README (will follow up to improve).

Note: This loses the benefit of VS Code extension
`styled-components.vscode-styled-components`, so dropping it from
recommendations.

| Before | After |
|--------|--------|
| ![CleanShot 2025-02-22 at 17 24
52@2x](https://github.com/user-attachments/assets/4b73f1b9-42a8-44f0-9083-7117be973bed)
| ![CleanShot 2025-02-22 at 17 24
58@2x](https://github.com/user-attachments/assets/1b5e9448-0802-4abc-8bb5-01bacdb2d2c5)
|

Closes NDX-750
Closes NDX-839
parent 9b0edbb7
......@@ -23,9 +23,6 @@
"ms-vscode.vscode-js-profile-flame",
// MDX Authoring
"unifiedjs.vscode-mdx",
// CSS Authoring (mainly Dev Overlay)
"styled-components.vscode-styled-components"
"unifiedjs.vscode-mdx"
]
}
......@@ -3,3 +3,8 @@ TODO
### Folder Structure
- `ui` - This should be isolated from the rest of the codebase to make it easier to be excluded from compiliation and be bundled.
### Styling
- The entrypoint needs to use `css` util that minifies styles.
- Entrypoint is which are using `<style>` tag to inject styles.
import { noop as css } from '../utils/noop-template'
import { css } from '../utils/css'
import { useInsertionEffect } from 'react'
export const FontStyles = () => {
......
......@@ -5,7 +5,6 @@ import { HotlinkedText } from '../hot-linked-text'
import { ExternalIcon } from '../../icons/external'
import { getFrameSource } from '../../../utils/stack-frame'
import { useOpenInEditor } from '../../utils/use-open-in-editor'
import { noop as css } from '../../../utils/noop-template'
export const CallStackFrame: React.FC<{
frame: OriginalStackFrame
......@@ -69,7 +68,7 @@ export const CallStackFrame: React.FC<{
)
}
export const CALL_STACK_FRAME_STYLES = css`
export const CALL_STACK_FRAME_STYLES = `
[data-nextjs-call-stack-frame-ignored] {
padding: var(--size-1_5) var(--size-2);
margin-bottom: var(--size-1);
......
......@@ -7,7 +7,6 @@ import { useMemo } from 'react'
import { HotlinkedText } from '../hot-linked-text'
import { getFrameSource } from '../../../utils/stack-frame'
import { useOpenInEditor } from '../../utils/use-open-in-editor'
import { noop as css } from '../../../utils/noop-template'
import { ExternalIcon } from '../../icons/external'
import { FileIcon } from '../../icons/file'
......@@ -112,7 +111,7 @@ export function CodeFrame({ stackFrame, codeFrame }: CodeFrameProps) {
)
}
export const CODE_FRAME_STYLES = css`
export const CODE_FRAME_STYLES = `
[data-nextjs-codeframe] {
background-color: var(--color-background-200);
overflow: hidden;
......
import * as React from 'react'
import { noop as css } from '../../../utils/noop-template'
import { cx } from '../../utils/cx'
function useCopyLegacy(content: string) {
......@@ -246,7 +245,7 @@ function CopySuccessIcon() {
)
}
export const COPY_BUTTON_STYLES = css`
export const COPY_BUTTON_STYLES = `
.nextjs-data-copy-button {
color: inherit;
}
......
import { noop as css } from '../../../utils/noop-template'
const styles = css`
const styles = `
[data-nextjs-dialog-root] {
--next-dialog-radius: var(--rounded-xl);
--next-dialog-footer-height: 48px;
......
import type { OriginalStackFrame } from '../../../../utils/stack-frame'
import { useMemo, useState, useRef } from 'react'
import { CallStackFrame } from '../../call-stack-frame/call-stack-frame'
import { noop as css } from '../../../../utils/noop-template'
interface CallStackProps {
frames: OriginalStackFrame[]
......@@ -119,7 +118,7 @@ function ChevronUpDown() {
)
}
export const CALL_STACK_STYLES = css`
export const CALL_STACK_STYLES = `
.error-overlay-call-stack-container {
position: relative;
margin-top: var(--size-2);
......
......@@ -7,7 +7,6 @@ import { NextLogo } from './next-logo'
import { useIsDevBuilding } from '../../../../../../dev/dev-build-indicator/internal/initialize'
import { useIsDevRendering } from '../../../../utils/dev-indicator/dev-render-indicator'
import { useDelayedRender } from '../../../hooks/use-delayed-render'
import { noop as css } from '../../../../utils/noop-template'
import { TurbopackInfo } from './dev-tools-info/turbopack-info'
import { RouteInfo } from './dev-tools-info/route-info'
import { StopIcon } from '../../../icons/stop-icon'
......@@ -538,7 +537,7 @@ function useClickOutside(
//////////////////////////////////////////////////////////////////////////////////////
export const DEV_TOOLS_INDICATOR_STYLES = css`
export const DEV_TOOLS_INDICATOR_STYLES = `
.dev-tools-indicator-menu {
-webkit-font-smoothing: antialiased;
display: flex;
......
import { noop as css } from '../../../../../utils/noop-template'
export function DevToolsInfo({
title,
children,
......@@ -43,7 +41,7 @@ export function DevToolsInfo({
)
}
export const DEV_TOOLS_INFO_STYLES = css`
export const DEV_TOOLS_INFO_STYLES = `
[data-info-popover] {
-webkit-font-smoothing: antialiased;
display: flex;
......
import { DevToolsInfo } from './dev-tools-info'
import { noop as css } from '../../../../../utils/noop-template'
function StaticRouteContent() {
return (
......@@ -105,7 +104,7 @@ export function RouteInfo({
)
}
export const DEV_TOOLS_INFO_ROUTE_INFO_STYLES = css`
export const DEV_TOOLS_INFO_ROUTE_INFO_STYLES = `
.dev-tools-info-link {
}
`
import { DevToolsInfo } from './dev-tools-info'
import { CopyButton } from '../../../copy-button'
import { noop as css } from '../../../../../utils/noop-template'
export function TurbopackInfo({
isOpen,
......@@ -111,7 +110,7 @@ export function TurbopackInfo({
)
}
export const DEV_TOOLS_INFO_TURBOPACK_INFO_STYLES = css`
export const DEV_TOOLS_INFO_TURBOPACK_INFO_STYLES = `
.dev-tools-info-code {
background: var(--color-gray-400);
color: var(--color-gray-1000);
......
import { forwardRef, useEffect, useRef, useState } from 'react'
import { noop as css } from '../../../../utils/noop-template'
import { css } from '../../../../utils/css'
import mergeRefs from '../../../utils/merge-refs'
import { useMinimumLoadingTimeMultiple } from './use-minimum-loading-time-multiple'
......
import { noop as css } from '../../../../utils/noop-template'
import { DialogBody } from '../../dialog'
type ErrorOverlayDialogBodyProps = {
......@@ -14,4 +13,4 @@ export function ErrorOverlayDialogBody({
)
}
export const DIALOG_BODY_STYLES = css``
export const DIALOG_BODY_STYLES = ``
import { Dialog } from '../../dialog/dialog'
import { noop as css } from '../../../../utils/noop-template'
type ErrorOverlayDialogProps = {
children?: React.ReactNode
......@@ -26,7 +25,7 @@ export function ErrorOverlayDialog({
)
}
export const DIALOG_STYLES = css`
export const DIALOG_STYLES = `
.error-overlay-dialog {
overflow-y: auto;
-webkit-font-smoothing: antialiased;
......
import { DialogHeader } from '../../dialog/dialog-header'
import { noop as css } from '../../../../utils/noop-template'
type ErrorOverlayDialogHeaderProps = {
children?: React.ReactNode
......@@ -15,7 +14,7 @@ export function ErrorOverlayDialogHeader({
)
}
export const DIALOG_HEADER_STYLES = css`
export const DIALOG_HEADER_STYLES = `
.nextjs-container-errors-header {
position: relative;
}
......
import { noop as css } from '../../../../utils/noop-template'
export function EnvironmentNameLabel({
environmentName,
}: {
......@@ -8,7 +6,7 @@ export function EnvironmentNameLabel({
return <span data-nextjs-environment-name-label>{environmentName}</span>
}
export const ENVIRONMENT_NAME_LABEL_STYLES = css`
export const ENVIRONMENT_NAME_LABEL_STYLES = `
[data-nextjs-environment-name-label] {
padding: var(--size-0_5) var(--size-1_5);
margin: 0;
......
import { useState, useRef, useLayoutEffect } from 'react'
import { noop as css } from '../../../../utils/noop-template'
export type ErrorMessageType = React.ReactNode
......@@ -44,7 +43,7 @@ export function ErrorMessage({ errorMessage }: ErrorMessageProps) {
)
}
export const styles = css`
export const styles = `
.nextjs__container_errors_wrapper {
position: relative;
}
......
import { noop as css } from '../../../../utils/noop-template'
export function ErrorOverlayBottomStack({
count,
activeIdx,
......@@ -35,7 +33,7 @@ export function ErrorOverlayBottomStack({
)
}
export const styles = css`
export const styles = `
.layer {
width: 100%;
height: var(--stack-layer-height);
......
import { useState, useCallback } from 'react'
import { ThumbsUp } from '../../../../icons/thumbs/thumbs-up'
import { ThumbsDown } from '../../../../icons/thumbs/thumbs-down'
import { noop as css } from '../../../../../utils/noop-template'
import { cx } from '../../../../utils/cx'
interface ErrorFeedbackProps {
......@@ -83,7 +82,7 @@ export function ErrorFeedback({ errorCode, className }: ErrorFeedbackProps) {
)
}
export const styles = css`
export const styles = `
.error-feedback {
display: flex;
align-items: center;
......
import { ErrorFeedback } from './error-feedback/error-feedback'
import { styles as feedbackStyles } from './error-feedback/error-feedback'
import { noop as css } from '../../../../utils/noop-template'
export type ErrorOverlayFooterProps = {
errorCode: string | undefined
......@@ -23,7 +22,7 @@ export function ErrorOverlayFooter({
)
}
export const styles = css`
export const styles = `
.error-overlay-footer {
display: flex;
flex-direction: row;
......
......@@ -8,7 +8,6 @@ import {
styles as toolbarStyles,
} from '../error-overlay-toolbar/error-overlay-toolbar'
import { ErrorOverlayFooter } from '../error-overlay-footer/error-overlay-footer'
import { noop as css } from '../../../../utils/noop-template'
import {
ErrorMessage,
styles as errorMessageStyles,
......@@ -138,7 +137,7 @@ export function ErrorOverlayLayout({
)
}
export const styles = css`
export const styles = `
${OVERLAY_STYLES}
${DIALOG_STYLES}
${DIALOG_HEADER_STYLES}
......
......@@ -2,7 +2,6 @@ import type { VersionInfo } from '../../../../../../../server/dev/parse-version-
import { ErrorOverlayPagination } from '../error-overlay-pagination/error-overlay-pagination'
import { VersionStalenessInfo } from '../../version-staleness-info/version-staleness-info'
import { noop as css } from '../../../../utils/noop-template'
import type { ReadyRuntimeError } from '../../../../utils/get-error-by-type'
type ErrorOverlayNavProps = {
......@@ -42,7 +41,7 @@ export function ErrorOverlayNav({
)
}
export const styles = css`
export const styles = `
[data-nextjs-error-overlay-nav] {
display: flex;
justify-content: space-between;
......
......@@ -5,7 +5,6 @@ import {
useRef,
useState,
} from 'react'
import { noop as css } from '../../../../utils/noop-template'
import { LeftArrow } from '../../../icons/left-arrow'
import { RightArrow } from '../../../icons/right-arrow'
import type { ReadyRuntimeError } from '../../../../utils/get-error-by-type'
......@@ -152,7 +151,7 @@ export function ErrorOverlayPagination({
)
}
export const styles = css`
export const styles = `
.error-overlay-pagination {
-webkit-font-smoothing: antialiased;
display: flex;
......
import type { DebugInfo } from '../../../../types'
import { NodejsInspectorButton } from './nodejs-inspector-button'
import { noop as css } from '../../../../utils/noop-template'
import { CopyStackTraceButton } from './copy-stack-trace-button'
import { DocsLinkButton } from './docs-link-button'
......@@ -24,7 +23,7 @@ export function ErrorOverlayToolbar({
)
}
export const styles = css`
export const styles = `
.error-overlay-toolbar {
display: flex;
gap: var(--size-1_5);
......
import { noop as css } from '../../../../utils/noop-template'
export type ErrorType =
| 'Build Error'
| 'Runtime Error'
......@@ -22,7 +20,7 @@ export function ErrorTypeLabel({ errorType }: ErrorTypeLabelProps) {
)
}
export const styles = css`
export const styles = `
.nextjs__container_errors_label {
padding: var(--size-0_5) var(--size-1_5);
margin: 0;
......
import { noop as css } from '../../../../utils/noop-template'
import { Overlay, type OverlayProps } from '../../overlay/overlay'
export function ErrorOverlayOverlay({ children, ...props }: OverlayProps) {
return <Overlay {...props}>{children}</Overlay>
}
export const OVERLAY_STYLES = css`
export const OVERLAY_STYLES = `
[data-nextjs-dialog-overlay] {
padding: initial;
top: 10vh;
......
import { noop as css } from '../../../utils/noop-template'
const styles = css`
const styles = `
[data-nextjs-dialog-overlay] {
position: fixed;
top: 0;
......
import { useOpenInEditor } from '../../utils/use-open-in-editor'
import { noop as css } from '../../../utils/noop-template'
type EditorLinkProps = {
file: string
......@@ -44,7 +43,7 @@ export function EditorLink({ file, location }: EditorLinkProps) {
)
}
export const EDITOR_LINK_STYLES = css`
export const EDITOR_LINK_STYLES = `
[data-with-open-in-editor-link] svg {
width: auto;
height: var(--size-font-small);
......
......@@ -3,7 +3,6 @@ import * as React from 'react'
import { HotlinkedText } from '../hot-linked-text'
import { EditorLink } from './editor-link'
import { ExternalIcon } from '../../icons/external'
import { noop as css } from '../../../utils/noop-template'
import { getFrameSource } from '../../../utils/stack-frame'
import { useOpenInEditor } from '../../utils/use-open-in-editor'
import { FileIcon } from '../../icons/file'
......@@ -144,7 +143,7 @@ export const Terminal: React.FC<TerminalProps> = function Terminal({
)
}
export const TERMINAL_STYLES = css`
export const TERMINAL_STYLES = `
[data-nextjs-terminal]::selection,
[data-nextjs-terminal] *::selection {
background-color: var(--color-ansi-selection);
......
import { noop as css } from '../../../utils/noop-template'
const styles = css`
const styles = `
.nextjs-toast {
position: fixed;
bottom: var(--size-gap-double);
......
import type { VersionInfo } from '../../../../../../server/dev/parse-version-info'
import { cx } from '../../utils/cx'
import { noop as css } from '../../../utils/noop-template'
export function VersionStalenessInfo({
versionInfo,
......@@ -88,7 +87,7 @@ export function getStaleness({ installed, staleness, expected }: VersionInfo) {
return { text, indicatorClass, title }
}
export const styles = css`
export const styles = `
.nextjs-container-build-error-version-status {
-webkit-font-smoothing: antialiased;
display: flex;
......
import React, { useCallback, useMemo } from 'react'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { Terminal } from '../components/terminal'
import { noop as css } from '../../utils/noop-template'
import { ErrorOverlayLayout } from '../components/errors/error-overlay-layout/error-overlay-layout'
import type { ErrorBaseProps } from '../components/errors/error-overlay/error-overlay'
......@@ -48,4 +47,4 @@ export const BuildError: React.FC<BuildErrorProps> = function BuildError({
)
}
export const styles = css``
export const styles = ``
import { useState, useMemo, useEffect, useRef, Suspense } from 'react'
import type { DebugInfo } from '../../types'
import { Overlay } from '../components/overlay'
import { noop as css } from '../../utils/noop-template'
import { RuntimeError } from './runtime-error'
import { getErrorSource } from '../../../../../shared/lib/error-source'
import { HotlinkedText } from '../components/hot-linked-text'
......@@ -208,7 +207,7 @@ export function Errors({
)
}
export const styles = css`
export const styles = `
.nextjs-error-with-static {
bottom: calc(var(--size-gap-double) * 4.5);
}
......
import { noop as css } from '../../../utils/noop-template'
export { PseudoHtmlDiff } from '../../components/hydration-diff/diff-view'
export const PSEUDO_HTML_DIFF_STYLES = css`
export const PSEUDO_HTML_DIFF_STYLES = `
[data-nextjs-container-errors-pseudo-html] {
padding: var(--size-2) 0;
margin: var(--size-2) 0;
......
import { useMemo } from 'react'
import { CodeFrame } from '../../components/code-frame/code-frame'
import { CallStack } from '../../components/errors/call-stack/call-stack'
import { noop as css } from '../../../utils/noop-template'
import { PSEUDO_HTML_DIFF_STYLES } from './component-stack-pseudo-html'
import {
useFrames,
......@@ -43,6 +42,6 @@ export function RuntimeError({ error, dialogResizerRef }: RuntimeErrorProps) {
)
}
export const styles = css`
export const styles = `
${PSEUDO_HTML_DIFF_STYLES}
`
import * as React from 'react'
import { noop as css } from '../../utils/noop-template'
import { css } from '../../utils/css'
export function Base() {
return (
......
import { noop as css } from '../../utils/noop-template'
import { css } from '../../utils/css'
// TODO: Replace the existing colors in Base.tsx.
export function Colors() {
......
......@@ -14,7 +14,7 @@ import { styles as containerRuntimeErrorStyles } from '../container/runtime-erro
import { COPY_BUTTON_STYLES } from '../components/copy-button'
import { CALL_STACK_FRAME_STYLES } from '../components/call-stack-frame/call-stack-frame'
import { DEV_TOOLS_INDICATOR_STYLES } from '../components/errors/dev-tools-indicator/dev-tools-indicator'
import { noop as css } from '../../utils/noop-template'
import { css } from '../../utils/css'
import { EDITOR_LINK_STYLES } from '../components/terminal/editor-link'
import { ENVIRONMENT_NAME_LABEL_STYLES } from '../components/errors/environment-name-label/environment-name-label'
import { DEV_TOOLS_INFO_STYLES } from '../components/errors/dev-tools-indicator/dev-tools-info/dev-tools-info'
......
import * as React from 'react'
import { noop as css } from '../../utils/noop-template'
import { css } from '../../utils/css'
export function CssReset() {
return (
......
import { css } from './css'
describe('css template literal tag', () => {
test('combines strings and interpolations correctly', () => {
const color = 'red'
const result = css`
color: ${color};
background: blue;
`
expect(result).toBe('color:red;background:blue;')
})
test('removes all whitespace', () => {
const result = css`
margin: 0;
padding: 10px;
`
expect(result).toBe('margin:0;padding:10px;')
})
test('removes CSS comments', () => {
const result = css`
color: blue; /* this is a comment */
/* multi-line
comment */
/** jsdoc */
margin: 10px;
`
expect(result).toBe('color:blue;margin:10px;')
})
test('handles multiple interpolations', () => {
const width = '100px'
const height = '200px'
const result = css`
width: ${width};
height: ${height};
color: blue; /* this is a comment */
/* multi-line
comment */
/** jsdoc */
`
expect(result).toBe('width:100px;height:200px;color:blue;')
})
test('handles empty strings', () => {
const result = css``
expect(result).toBe('')
})
test('handles CSS wildcard selector', () => {
const result = css`
* {
margin: 0;
padding: 0;
}
`
expect(result).toBe('*{margin:0;padding:0}')
})
})
export function noop(
export function css(
strings: TemplateStringsArray,
...keys: readonly string[]
) {
): string {
const lastIndex = strings.length - 1
return (
const str =
// Convert template literal into a single line string
strings.slice(0, lastIndex).reduce((p, s, i) => p + s + keys[i], '') +
strings[lastIndex]
return (
str
// Remove comments
.replace(/\/\*[\s\S]*?\*\//g, '')
// Remove whitespace, tabs, and newlines
.replace(/\s+/g, ' ')
// Remove spaces before and after semicolons, and spaces after commas
.replace(/\s*([:;,{}])\s*/g, '$1')
// Remove extra semicolons
.replace(/;+}/g, '}')
// Trim leading and trailing whitespaces
.trim()
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment