1 回答

TA貢獻2012條經驗 獲得超12個贊
我來寫例子吧;然后把我webpack.config.js配置信息。
react的css開發,一般采用模塊化的形式進行。一般react中css可以分為三部分,
快發環境給予node.js、模塊化構建用webpack.
第一:全局部分:比如base.css(用來通用的css,如:.clearfix、.mt10、.mt05之類的)。這個文件可以直接在入口文件如(index.html)中直接用<link href>的形式直接引入
第二:通用部分:比如我可以把整個webapp需要用到的按鈕樣式作為一個樣式寫到一個文件里如:common/button.css,寫法的話遵循模塊化開發的方式進行(composes);
貼端代碼如下:
.btn {
height: 35px;
line-height: 35px;
border-radius: 2px;
display: inline-block;
text-align: center;
user-select: none;
text-align: center;
box-sizing: border-box;
font-size: 14px;
}
.btn-primary {
composes : btn;
border: 1px solid #E0E0E0;
background-image: -webkit-linear-gradient(top, #f9f9f9, #e7e9eb);
background-repeat: repeat-x;
color: #333;
}
.btn-blue {
composes : btn;
border: 1px solid #0B62BD;
background-image: -webkit-linear-gradient(top, #0e7bef, #0d73da);
background-repeat: repeat-x;
color: #fff;
}
第二:組件內的css:比如我們寫了一個confirm的通用組件,那么
comfirm組件的目下應該包含兩個文件comfirm.js 和 comfirm.css
在comfirm.js中用var Styles = require('./confirm.css');的形式進行引用。
在render的時候進行調用調用形式:<button className={Styles['btn-primary']}>基礎按鈕</button>
寫組件完成后,比如我在一個編輯頁面中需要用到confirm組件,那么就可以直接requre。
confirm.css如何開發呢?一般采用composes的方式進行(比如我confirm組件中有用到button,那么就可以引用通用的button),然后可以基于composes后的進行修改,比如顏色等等。貼一段代碼如下:
.btn-cancel{
composes : btn-primary from '../style/button.css';
width:100px;
margin-right: 10px;
}
.btn-confirm{
composes : btn-blue from '../style/button.css';
width:100px;
margin-left: 10px;
}
第三:就是行內樣式了,一般會用得比較少。
webpack配置信息,涉及到樣式內圖片的解析
/**
* webpack 配置文件
* 配置獨立項、入口文件、輸出項信息
*/
var path = require('path');
var webpack = require('webpack');
var admin_components_dir = path.join(__dirname, 'admin/script/components/');
//重定向文件
var alias= {
Login : admin_components_dir + 'login/Login.js',
Layout : admin_components_dir + 'layout/Layout.js',
Article : admin_components_dir + 'article/Article.js',
Pager : admin_components_dir + 'common/Pager/Pager.js',
Tip : admin_components_dir + 'common/Tip/Tip.js',
Confirm : admin_components_dir + 'common/Confirm/Confirm.js',
Select : admin_components_dir + 'common/Select/Select.js',
ArticleForm : admin_components_dir + 'article/ArticleForm.js',
Editor : admin_components_dir + 'common/Editor/Editor.js',
CheckBox : admin_components_dir + 'common/CheckBox/CheckBox.js'
};
var config = {
devtool: 'inline-source-map',
entry: [
'webpack-dev-server/client?http://127.0.0.1:4000',
'webpack/hot/only-dev-server',
'./admin/script/temp.js'
],
output: {
path: path.join(__dirname, 'admin/dist/'),
filename: 'app.js',
publicPath: '/static/'
},
resolve: {
alias: []
},
module: {
noParse : [],
loaders: [{
test: /\.jsx?$/,
loaders: ['react-hot', 'babel'],
include: path.join(__dirname,'admin/script/')
},{
test: /\.css$/,
exclude: [
path.resolve(__dirname, 'node_modules')
],
loaders: ['style', 'css?modules&localIdentName=[name]_[local]_[hash:base64:5]','autoprefixer?{browsers:["> 5%", "ie 9"]}']
},{
test: /\.(svg|png|jpg|jpeg|gif)$/i,
loaders: ['file', 'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false']
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
]
}
//重定向文件賦值
config.resolve.alias = alias;
module.exports = config;
webpack中有css的命名規則、css代碼自動補全(就是不用寫瀏覽器前綴了)等配置
這樣第二、第三中的css都會打包到app.js中,入口文件(如:index.html)只要用<script src>的形式隱形引用即可
添加回答
舉報