Skip to content

Commit 408633c

Browse files
timneutkensarunoda
authored andcommitted
Remove traces of glamor (#1286)
* Remove traces of glamor As talked about with @rauchg. Glamor takes up around 60KB of the bundle (pre-gzip). Since styled-jsx is the way to go now and we support adding glamor by the user we should remove it as dependency cause it is bundled even when not used. Added rehydration to the example, since we did that in our code. There is only one thing I'm not sure about and want to discuss: what should we do with next/css. Right now I added a throw for when it is imported. I'm not sure if we should do that / some other way to notify the user it has been removed. The reasoning behind the throw is that when we would do a console.warn the user would see 'css.default.<X>' not found because we don't have the glamor dependency anymore. * Update yarn.lock * Remove test for styles
1 parent 1417f30 commit 408633c

File tree

10 files changed

+835
-957
lines changed

10 files changed

+835
-957
lines changed

client/index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { createElement } from 'react'
22
import ReactDOM from 'react-dom'
33
import { EventEmitter } from 'events'
44
import HeadManager from './head-manager'
5-
import { rehydrate } from '../lib/css'
65
import { createRouter } from '../lib/router'
76
import App from '../lib/app'
87
import evalScript from '../lib/eval-script'
@@ -13,7 +12,6 @@ const {
1312
component,
1413
errorComponent,
1514
props,
16-
ids,
1715
err,
1816
pathname,
1917
query
@@ -35,7 +33,6 @@ const container = document.getElementById('__next')
3533

3634
export default (onError) => {
3735
const emitter = new EventEmitter()
38-
if (ids && ids.length) rehydrate(ids)
3936

4037
router.subscribe(({ Component, props, err }) => {
4138
render({ Component, props, err, emitter }, onError)

examples/with-glamor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"start": "next start"
88
},
99
"dependencies": {
10-
"glamor": "^2.20.12",
10+
"glamor": "^2.20.24",
1111
"next": "^2.0.0-beta",
1212
"react": "^15.4.2",
1313
"react-dom": "^15.4.2"

examples/with-glamor/pages/_document.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ import { renderStatic } from 'glamor/server'
44
export default class MyDocument extends Document {
55
static async getInitialProps ({ renderPage }) {
66
const page = renderPage()
7-
let styles = renderStatic(() => page.html)
7+
const styles = renderStatic(() => page.html)
88
return { ...page, ...styles }
99
}
1010

11+
constructor (props) {
12+
super(props)
13+
const { __NEXT_DATA__, ids } = props
14+
if (ids) {
15+
__NEXT_DATA__.ids = this.props.ids
16+
}
17+
}
18+
1119
render () {
1220
return (
1321
<html>

examples/with-glamor/pages/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import React from 'react'
2-
import { style } from 'glamor'
2+
import { style, rehydrate } from 'glamor'
3+
4+
// Adds server generated styles to glamor cache.
5+
// Has to run before any `style()` calls
6+
// '__NEXT_DATA__.ids' is set in '_document.js'
7+
if (typeof window !== 'undefined') {
8+
rehydrate(window.__NEXT_DATA__.ids)
9+
}
310

411
export default () => <h1 {...styles.title}>My page</h1>
512

lib/css.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1 @@
1-
import { deprecated } from './utils'
2-
const css = require('glamor')
3-
// Needed because of the mutation below
4-
delete require.cache[require.resolve('glamor')]
5-
6-
for (const [k, v] of Object.entries(css)) {
7-
if (typeof v === 'function') {
8-
css[k] = deprecated(v, 'Warning: \'next/css\' is deprecated. Please refer to the migration guide: https://github.com/zeit/next.js/wiki/Migrating-from-next-css')
9-
}
10-
}
11-
12-
/**
13-
* Expose style as default and the whole object as properties
14-
* so it can be used as follows:
15-
*
16-
* import css, { merge } from 'next/css'
17-
* css({ color: 'red' })
18-
* merge({ color: 'green' })
19-
* css.merge({ color: 'blue' })
20-
*/
21-
22-
css.default = css.style
23-
Object.keys(css).forEach(key => {
24-
if (key !== 'default') {
25-
css.default[key] = css[key]
26-
}
27-
})
28-
29-
module.exports = css
1+
throw new Error(`'next/css' has been removed in Next.js 2.0. Please refer to the migration guide: https://github.com/zeit/next.js/wiki/Migrating-from-next-css`)

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
"cross-spawn": "5.0.1",
6262
"del": "2.2.2",
6363
"friendly-errors-webpack-plugin": "1.4.0",
64-
"glamor": "2.20.23",
6564
"glob-promise": "3.1.0",
6665
"htmlescape": "1.1.1",
6766
"http-status": "1.0.1",

server/document.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
import React, { Component, PropTypes } from 'react'
22
import htmlescape from 'htmlescape'
3-
import { renderStatic } from 'glamor/server'
43
import flush from 'styled-jsx/server'
54

65
export default class Document extends Component {
76
static getInitialProps ({ renderPage }) {
8-
let head
9-
let rendered
10-
let styles
11-
try {
12-
rendered = renderStatic(() => {
13-
const page = renderPage()
14-
head = page.head
15-
return page.html
16-
})
17-
} finally {
18-
styles = flush()
19-
}
20-
const { html, css, ids } = rendered
21-
const nextCSS = { css, ids, styles }
22-
return { html, head, nextCSS }
7+
const {html, head} = renderPage()
8+
const styles = flush()
9+
return { html, head, styles }
2310
}
2411

2512
static childContextTypes = {
2613
_documentProps: PropTypes.any
2714
}
2815

29-
constructor (props) {
30-
super(props)
31-
const { __NEXT_DATA__, nextCSS } = props
32-
if (nextCSS) __NEXT_DATA__.ids = nextCSS.ids
33-
}
34-
3516
getChildContext () {
3617
return { _documentProps: this.props }
3718
}
@@ -53,11 +34,10 @@ export class Head extends Component {
5334
}
5435

5536
render () {
56-
const { head, nextCSS } = this.context._documentProps
37+
const { head, styles } = this.context._documentProps
5738
return <head>
5839
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
59-
{nextCSS && nextCSS.css ? <style dangerouslySetInnerHTML={{ __html: nextCSS.css }} /> : null}
60-
{nextCSS && nextCSS.styles ? nextCSS.styles : null}
40+
{styles || null}
6141
{this.props.children}
6242
</head>
6343
}

test/integration/basic/pages/css.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/integration/basic/test/rendering.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ export default function ({ app }, suiteName, render) {
2828
expect(html.includes('I can haz meta tags')).toBeTruthy()
2929
})
3030

31-
test('css helper renders styles', async () => {
32-
const $ = await get$('/css')
33-
const redBox = $('#red-box')
34-
35-
expect(redBox.text()).toBe('This is red')
36-
expect(redBox.attr('class')).toMatch(/^css-/)
37-
})
38-
3931
test('renders styled jsx', async () => {
4032
const $ = await get$('/styled-jsx')
4133
const styleId = $('#blue-box').attr('data-jsx')

0 commit comments

Comments
 (0)