Skip to content

Commit c5b6710

Browse files
committed
Support for NextJS now working
1 parent ad07610 commit c5b6710

File tree

20 files changed

+84
-40
lines changed

20 files changed

+84
-40
lines changed

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ You can also define your own styles using the styles function which should retur
2020
```
2121
/* ./app.js */
2222
import React from 'react';
23-
import {StaticCSS as CSS} from '@mintuz/react-css-loader';
23+
import { StaticCSS as CSS } from '@mintuz/react-css-loader';
2424
2525
const MyComponent = () => {
2626
return <div className="my-custom-element"/>;
@@ -47,7 +47,8 @@ The file system adapter will resolve css files from your local file system and a
4747

4848
```
4949
import App from './app'; /* React application */
50-
import {Resolver, FileSystemAdapter} from '@mintuz/react-css-loader/server';
50+
import { Resolver } from '@mintuz/react-css-loader';
51+
import FileSystemAdapter from '@mintuz/react-css-loader/file';
5152
5253
const cssResolver = new Resolver(React.createElement(App), new FileSystemAdapter({
5354
folderPath: `${__dirname}/styles/`,
@@ -74,7 +75,8 @@ console.log(cssString);
7475

7576
```
7677
import App from './app';
77-
import {Resolver, FileSystemAdapter} from '@mintuz/react-css-loader/server';
78+
import { Resolver } from '@mintuz/react-css-loader';
79+
import FileSystemAdapter from '@mintuz/react-css-loader/file';
7880
7981
const cssResolver = new Resolver(React.createElement(App), new FileSystemAdapter({
8082
folderPath: `${__dirname}/styles/`,
@@ -99,7 +101,8 @@ If you serve your CSS via a CDN, this is for you. Rather than resolving the CSS
99101

100102
```
101103
import App from './app';
102-
import {Resolver, CDNAdapter} from '@mintuz/react-css-loader/server';
104+
import { Resolver } from '@mintuz/react-css-loader';
105+
import CDNAdapter from '@mintuz/react-css-loader/cdn';
103106
104107
const cssResolver = new Resolver(React.createElement(App), new CDNAdapter({
105108
cdnRoot: 'https://my-cdn.com'

cdn.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import CDNAdapter from './dist/adapter/cdn';
2+
export default CDNAdapter;

examples/express-cdn/src/App/Body.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { StaticCSS as CSS } from '../../../../index';
2+
import { StaticCSS as CSS } from '../../../../';
33

44
function Body(props) {
55
return (

examples/express-cdn/src/App/Header.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { StaticCSS as CSS } from '../../../../index';
2+
import { StaticCSS as CSS } from '../../../../';
33

44
function Header() {
55
return (

examples/express-cdn/src/App/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import Header from './Header';
33
import Body from './Body';
4-
import { StaticCSS as CSS } from '../../../../index';
4+
import { StaticCSS as CSS } from '../../../../';
55

66
function Root(props) {
77
return (

examples/express-cdn/src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import express from 'express';
22
import React from 'react';
33
import ReactDOMServer from 'react-dom/server';
44
import App from './App';
5-
import { Resolver, CDNAdapter } from '../../../server';
5+
import { Resolver } from '../../../';
6+
import CDNAdapter from '../../../cdn';
67

78
const app = express();
89

examples/express-file-system/src/App/Body.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { StaticCSS as CSS } from '../../../../index';
2+
import { StaticCSS as CSS } from '../../../../';
33

44
function Body(props) {
55
return (

examples/express-file-system/src/App/Header.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { StaticCSS as CSS } from '../../../../index';
2+
import { StaticCSS as CSS } from '../../../../';
33

44
function Header() {
55
return (

examples/express-file-system/src/App/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import Header from './Header';
33
import Body from './Body';
4-
import { StaticCSS as CSS } from '../../../../index';
4+
import { StaticCSS as CSS } from '../../../../';
55

66
function Root(props) {
77
return (

examples/express-file-system/src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import React from 'react';
33
import ReactDOMServer from 'react-dom/server';
44
import { path } from 'app-root-path';
55
import App from './App';
6-
import { Resolver, FileSystemAdapter } from '../../../server';
6+
import { Resolver } from '../../../';
7+
import FileSystemAdapter from '../../../file';
78

89
const app = express();
910

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { StaticCSS as CSS } from '../../../';
2+
3+
function Header() {
4+
return (
5+
<div className="example-header">
6+
My example Header
7+
</div>
8+
);
9+
}
10+
11+
Header.displayName = "Header";
12+
13+
export default CSS(Header);

examples/nextjs-cdn/pages/_app.js

+27-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React from 'react';
22
import App, { Container } from 'next/app';
3+
import Head from 'next/head';
4+
import { Resolver } from '../../../';
5+
import CDNAdapter from '../../../cdn';
36

47
export default class MyApp extends App {
58
static async getInitialProps({ Component, ctx }) {
@@ -9,33 +12,39 @@ export default class MyApp extends App {
912
pageProps = await Component.getInitialProps(ctx);
1013
}
1114

12-
return import('../../../server').then(async ({Resolver, CDNAdapter}) => {
13-
const cssResolver = new Resolver(Component, new CDNAdapter());
14-
const css = await cssResolver.render();
15-
16-
return { pageProps, css };
17-
}).catch(() => {
18-
return { pageProps };
19-
});
20-
21-
// console.log(CSS);
15+
const cssResolver = new Resolver(
16+
React.createElement(Component, pageProps),
17+
new CDNAdapter({
18+
cdnRoot: '/static', /* Map to nextjs static folder */
19+
}),
20+
);
2221

22+
/* Rather than use .render(), lets return the raw response so we can trasnform it. */
23+
const rawStyles = await cssResolver.resolve();
2324

25+
/* We don't want everything sent down to the client, let's only send this. */
26+
const styles = rawStyles.map(style => {
27+
return style.path;
28+
});
2429

25-
// const cssResolver = new Resolver(Component, new CDNAdapter());
26-
// const css = await cssResolver.render();
27-
28-
// console.log(css);
29-
30-
return { pageProps };
30+
return { pageProps, styles };
3131
}
3232

3333
render () {
34-
const { Component, pageProps, css } = this.props;
35-
console.log(css);
34+
const { Component, pageProps, styles } = this.props;
3635

3736
return (
3837
<Container>
38+
<Head>
39+
{
40+
styles.map(style => {
41+
/* Render a link style using react for each stylesheet found */
42+
return (
43+
<link rel="stylesheet" href={style} />
44+
);
45+
})
46+
}
47+
</Head>
3948
<Component {...pageProps} />
4049
</Container>
4150
)

examples/nextjs-cdn/pages/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import React from 'react';
12
import { StaticCSS as CSS } from '../../../index';
3+
import Header from '../components/Header';
24

35
function Homepage() {
46
return (
5-
<div>
6-
My example homepage
7-
</div>
7+
<React.Fragment>
8+
<Header />
9+
<div>
10+
My example homepage
11+
</div>
12+
</React.Fragment>
813
);
914
}
1015

examples/nextjs-cdn/static/Header.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.example-header {
2+
background-color: red;
3+
color: black;
4+
padding: 16px;
5+
margin-bottom: 16px;
6+
}

examples/nextjs-cdn/static/Homepage.css

+2
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ html,
22
body {
33
background-color: black;
44
color: white;
5+
padding: 0;
6+
margin: 0;
57
}

file.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
import FileAdapter from './dist/adapter/file';
3+
export default FileAdapter;

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { default as StaticCSS } from './dist/static-container';
22
export { default as DynamicCSS } from './dist/dynamic-container';
3+
export { default as Resolver } from './dist/resolver';

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@mintuz/react-css-loader",
3-
"version": "2.0.1",
4-
"main": "server.js",
3+
"version": "3.0.0",
4+
"main": "index.js",
55
"private": false,
66
"description": "A React high order component that provides functionality for tree shaking CSS per component",
77
"homepage": "http://github.com/mintuz/react-css-loader/tree/master",

server.js

-3
This file was deleted.

src/__test__/index.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3-
import { StaticCSS as CSS, DynamicCSS } from '../../index';
4-
import { Resolver, FileSystemAdapter, CDNAdapter } from '../../server';
3+
import { StaticCSS as CSS, DynamicCSS, Resolver } from '../../index';
4+
import FileSystemAdapter from '../../file';
5+
import CDNAdapter from '../../cdn';
56

67
function buildComponent(displayName) {
78
function MyComponent(props) {

0 commit comments

Comments
 (0)