全部源代碼、文檔和圖片都在 github 的倉庫裏,點擊進入倉庫javascript
npm i node-sass sass-loader -D
module.exports = { module: { rules: [ { test: /\.css$/, use: ['style-loader', { loader: 'css-loader', options: { importLoaders: 1, modules: true, localIdentName: '[name]_[local]_[hash:base64:5]' } } ] } ] } }
module.exports = { module: { rules: [ { test: /\.css?$/, use: ['isomorphic-style-loader', { loader: "css-loader", options: { importLoaders: 1, modules: true, localIdentName: '[name]_[local]_[hash:base64:5]' } }] } ] } }
/** * /src/containers/Home/index.css */ .wrapper { background: orange; } .title { color: red; font-size: 26px; }
// /src/containers/Home/index.js import styles from './index.css'; class Home extends Component { render() { return ( <div className={styles.wrapper}> <h2 className={styles.title}>HELLO, HOME PAGE</h2> </div> ); } }
可是這樣有兩個問題css
import styles from './index.css'; console.log(styles); { wrapper: 'index_wrapper_2wP7c', title: 'index_title_39dQ8', _getContent: [Function], _getCss: [Function], _insertCss: [Function] } -------------------------------------------------- console.log(styles._getContent()); [ [ './node_modules/_css-loader@2.1.1@css-loader/dist/cjs.js?!./src/containers/Home/index.css', '.index_wrapper_2wP7c {\r\n background: orange;\r\n}\r\n\r\n.index_title_39dQ8 {\r\n color: red;\ \n font-size: 26px;\r\n}\r\n', '' ], toString: [Function: toString], i: [Function], locals: { wrapper: 'index_wrapper_2wP7c', title: 'index_title_39dQ8', _getContent: [Function], _getCss: [Function], _insertCss: [Function] } ] -------------------------------------------------- console.log(styles._getCss()); .index_wrapper_2wP7c { background: orange; } .index_title_39dQ8 { color: red; font-size: 26px; }
componentWillMount() { let staticContext = this.props.staticContext; if (staticContext) { if (staticContext) { staticContext.csses.push(styles._getCss()); } } }
export default (req, res) => { let context = { csses: [] }; Promise.all(promises).then(() => { let domContent = renderToString( <Provider store={store}> <StaticRouter context={context} location={req.path}> { renderRoutes(routes) } </StaticRouter> </Provider> ); let cssStr = context.csses.length ? context.csses.join('\n') : ''; let html = ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"> <title>react-ssr</title> <style>${cssStr}</style> </head> <body> <div id="root">${domContent}</div> <script> window.context = { state: ${JSON.stringify(store.getState())} } </script> <script src="/client.js"></script> </body> </html> `; res.send(html); }); };
import React, { Component } from 'react'; export default (DecoratedComponent, styles) => { return class NewComponent extends Component { componentWillMount() { if (this.props.staticContext) { this.props.staticContext.csses.push(styles._getCss()); } } render() { return (<DecoratedComponent {...this.props} />); } }; };
import WithStyle from '../../withStyle'; export default connect(mapStateToProps, mapDispatchToProps)(WithStyle(Home, styles));
const ExportHome = connect(mapStateToProps, mapDispatchToProps)(WithStyle(Home, styles)); ExportHome.loadData = store => store.dispatch(UserActions.getSchoolList()); export default ExportHome;