Một mục mã nguồn mở trong kho dữ liệu GitHub của TopGit: kriasoft/isomorphic-style-loader, 1.3k sao, JavaScript. CSS style loader for Webpack that is optimized for isomorphic (universal) web apps.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
CSS style loader for Webpack that works similarly to
style-loader, but is optimized for
critical path CSS
rendering and also works great in the context of
isomorphic apps.
It provides two helper methods on to the styles object - ._insertCss()
(injects CSS into the DOM) and ._getCss() (returns a CSS string).
See getting started | changelog |
Join #isomorphic-style-loader
chat room on Discord to stay up to date
How to Install
$ npm install isomorphic-style-loader --save-dev
Requirements
Webpack 5
React 16.8 or newer
css-loader configured with esModule: false (the loader reads CSS module
class names from exports.locals)
The optional getCss loader option is serialized into the generated module,
so it must be a self-contained function without closed-over variables.
Breaking Changes in v6
Static properties of the wrapped component (e.g. fetchData) are no longer
hoisted onto the component returned by withStyles. Access them via the
ComposedComponent property instead.
/* App.js */
import React from 'react'
import withStyles from 'isomorphic-style-loader/withStyles'
import s from './App.scss'
function App(props, context) {
return (
<div className={s.root}>
<h1 className={s.title}>Hello, world!</h1>
</div>
)
}
export default withStyles(s)(App) // <--
P.S.: It works great with CSS Modules!
Just decorate your React component with the
withStyles
higher-order component, and pass a function to your React app via insertCss
context variable (see React's context API)
that either calls styles._insertCss() on a client or styles._getCss()
on the server. See server-side rendering example below:
import express from 'express'
import React from 'react'
import ReactDOM from 'react-dom'
import StyleContext from 'isomorphic-style-loader/StyleContext'
import App from './App.js'
const server = express()
const port = process.env.PORT || 3000
// Server-side rendering of the React app
server.get('*', (req, res, next) => {
const css = new Set() // CSS for all rendered React components
const insertCss = (...styles) => styles.forEach(style => css.add(style._getCss()))
const body = ReactDOM.renderToString(
<StyleContext.Provider value={{ insertCss }}>
<App />
</StyleContext.Provider>
)
const html = `<!doctype html>
<html>
<head>
<script src="client.js" defer></script>
<style>${[...css].join('')}</style>
</head>
<body>
<div id="root">${body}</div>
</body>
</html>`
res.status(200).send(html)
})
server.listen(port, () => {
console.log(`Node.js app is running at http://localhost:${port}/`)
})
It should generate an HTML output similar to this one:
Regardless of how many styles components there are in the app.js bundle,
only critical CSS is going to be rendered on the server inside the <head>
section of HTML document. Critical CSS is what actually used on the
requested web page, effectively dealing with
FOUC
issue and improving client-side performance.
CSS of the unmounted components will be removed from the DOM.
Then on client-side use hydrate
to make your markup interactive:
You can also use useStyles inside your React Functional Components, instead of using withStyles.
Please note that you still need to pass insertCss function to StyleContext.Provider from top of the tree.
import React from 'react'
import useStyles from 'isomorphic-style-loader/useStyles'
import s from './App.scss'
const App = (props) => {
useStyles(s);
return (
<div className={s.root}>
<h1 className={s.title}>Hello, world!</h1>
</div>
)
};
export default App;
Đọc thêm về kriasoft/isomorphic-style-loader ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/kriasoft/isomorphic-style-loader là nguồn chính thức.
kriasoft/isomorphic-style-loader có phải mã nguồn mở không?
Có — kriasoft/isomorphic-style-loader phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/kriasoft/isomorphic-style-loader.
kriasoft/isomorphic-style-loader có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho kriasoft/isomorphic-style-loader. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
kriasoft/isomorphic-style-loader có trang demo không?
Dự án có trang chủ ở https://reactstarter.com. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
kriasoft/isomorphic-style-loader còn đang phát triển không?
Commit gần nhất trên kriasoft/isomorphic-style-loader là 2 tháng trước (theo timestamp GitHub). Repo có 140 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
kriasoft/isomorphic-style-loader dùng license gì?
kriasoft/isomorphic-style-loader phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về isomorphic-style-loader?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về isomorphic-style-loader.