Fil*_*ton 7 css lazy-loading reactjs webpack
我有一些SCSS主题文件,我想提取到CSS文件,然后将它们加载到页面中.我希望能够contenthash用于长期缓存.
由于我使用的是Webpack 4,我也使用mini-css-extract-plugin.我开始在我的webpack配置中创建splitChunks的路径.
// webpack.config.js
module.exports = {
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "[name].[contenthash].css",
chunkFilename: "[id].[contenthash].css"
})
],
optimization: {
splitChunks: {
cacheGroups: {
'vendor': {
// custom commons chunk for js
},
'theme-a': {
test: /theme-a.\scss/,
},
'theme-b': {
test: /theme-b.\scss/,
},
// more themes
}
}
}
module: {
rules: [
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试在我的应用中动态导入 css:
// app.js
class App extends React.Component {
// constructor
login(themeName) {
import(/* webpackChunkName: "`${themeName}`" */ `./path/to/${themeName}.scss`).then(theme => {
// do something with `theme`
}
}
// other stuff
}
Run Code Online (Sandbox Code Playgroud)
我需要能够动态加载该css文件login(),我只是不确定如何生成它时引用它[contenthash].
mini-css-extract-plugin.编辑:在此处创建了一个mini-css-extract-plugin问题.
我的解决方案最终使用了extract-text-webpack-plugin.我的配置现在看起来像这样:
// webpack.config.js
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ExtractThemeA = new ExtractTextPlugin({ filename: 'themeA.[hash].css', allChunks: true});
module.exports = {
plugins: [
ExtractThemeA,
// more plugins for other css files
],
optimization: {
splitChunks: {
cacheGroups: {
// Note: No changes to splitChunks
'vendor': {
// custom commons chunk for js
}
}
}
module: {
rules: [
{
test: /theme-a\.scss$/,
use: ExtractThemeA.extract([ 'css-loader', 'sass-loader' ])
},
// more module rules for each css file needed
]
}
}
Run Code Online (Sandbox Code Playgroud)
然后,这些块可以通过我的文件名获得HtmlWebpackPlugin:
<!-- HtmlWebpackPlugin Template -->
<script>
// provides me with an array of file name strings
var themesManifest = <%= htmlWebpackPlugin.files.css %>
</script>
Run Code Online (Sandbox Code Playgroud)
对不起,我的小姐理解,
您可能只需要制作两个不同的 scss 文件并根据需要导入它们。theme.scss admin.scss或者像这样
这就是我现在在 React 中做 scss 的方式
在 App.js 中
import styles from '../../stylesheet/main.scss'
// could be as well
import styles1 from '../../stylesheet/theme.scss' // some file
import styles2 from '../../stylesheet/admin.scss' // some other file
const App = () => {
<div className={styles.action_feed} >{ content }</div>
}
Run Code Online (Sandbox Code Playgroud)
在 main.scss 中
.action_feed {
position: fixed;
width: 350px;
height: auto;
max-height: 100%;
max-width: 100%;
top: 0;
left: 0;
z-index: 9999;
}
Run Code Online (Sandbox Code Playgroud)
我想你也可以这样做
const themeName = 'main'
import(`./stylesheet/${themeName}.scss`, (css) => {
// meaby set this to state? not quite sure how should properly handle
// dynamically imported css
this.setState({ css: css.action_feed })
// or possible this way. I have done some non-React dom manipulation
// this way before
document.querySelector('body').classList.add(css.action_feed)
})
<div className={this.state.css}>{ content }</div>
Run Code Online (Sandbox Code Playgroud)
您也应该查看 React 的新 Refs API。它可能会给你一些很好的灵活性,将className-attr 赋予所需的元素。
具有被设置为splitChunks.chunks以all作品虽然我反正觉得在这种情况下,
| 归档时间: |
|
| 查看次数: |
2312 次 |
| 最近记录: |