React + Webpack简单配置(多入口)

前端之家收集整理的这篇文章主要介绍了React + Webpack简单配置(多入口)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

目前有写demo的文件,各个文件中有package.json和相关的node_modules,个人需求:

统一管理,不要重复的安装依赖————实现多入口;

现有目录结构如下:

package.json

  1. {
  2. "name": "front_web_demo","version": "1.0.0","main": "index.js","directories": {
  3. "test": "test"
  4. },"scripts": {
  5. "test": "echo \"Error: no test specified\" && exit 1","start": "webpack-dev-server --config webpack.dev.config.js","build": "webpack --config webpack.production.config.js"
  6. },"repository": {
  7. "type": "git","url": "git@git.graysoft.cn:zhangchao/front_web_demo.git"
  8. },"keywords": [],"author": "","license": "ISC","devDependencies": {
  9. "babel-core": "^6.23.1","babel-loader": "^6.3.2","babel-preset-env": "^1.1.8","babel-preset-es2015": "^6.22.0","babel-preset-react": "^6.23.0","copy-webpack-plugin": "^4.0.1","css-loader": "^0.26.1","file-loader": "^0.10.0","html-webpack-plugin": "^2.28.0","node-sass": "^4.5.0","sass-loader": "^6.0.1","style-loader": "^0.13.1","url-loader": "^0.5.7","webpack": "^2.2.1","webpack-del-plugin": "0.0.1","webpack-dev-server": "^2.4.1","webpack-notifier": "^1.5.0"
  10. },"dependencies": {
  11. "chart.js": "^2.5.0","jquery": "^3.1.1","jstree": "^3.3.3","react": "^15.4.2","react-chartjs-2": "^2.0.0","react-dom": "^15.4.2","react-intl": "^2.2.3","underscore": "^1.8.3"
  12. },"description": ""
  13. }

webpack.dev.config.js

  1. var webpack = require("webpack");
  2. var path = require("path");
  3. var glob = require("glob");
  4.  
  5. var htmlWebpackPlugin = require("html-webpack-plugin");
  6. var webpackCopyPlugin = require("copy-webpack-plugin");
  7. var webpackDelPlugin = require("webpack-del-plugin");
  8. var webpackNotifierPlugin = require("webpack-notifier");
  9.  
  10. var ROOT_PATH = path.resolve(__dirname);
  11. var SRC_PATH = path.resolve(ROOT_PATH,"source");
  12. var DIST_PATH = path.resolve(ROOT_PATH,"dist");
  13. var TEM_PATH = path.resolve(ROOT_PATH,"templates");
  14.  
  15. var ProjectArray = glob.sync(SRC_PATH + "/*/index.js");
  16. var ProjectEntries = {};
  17.  
  18. var config = {
  19. entry: {
  20. vendors:["react","react-dom","underscore","jquery"],intlVendor:["react-intl"],jstreeVendor:["jstree"],chartVendors:["react-chartjs-2","chart.js"]
  21. },output:{
  22. path:DIST_PATH,filename:"[name].js"
  23. },module:{
  24. rules:[
  25. {
  26. test:/\.(es6|js)$/,use:[
  27. {
  28. loader:"babel-loader"
  29. }
  30. ],exclude:/node_modules/
  31. },{
  32. test:/\.(scss|css)$/,use:[
  33. {
  34. loader:"style-loader"
  35. },{
  36. loader:"css-loader"
  37. },{
  38. loader:"sass-loader"
  39. }
  40. ],{
  41. test:/\.(png|jpeg|jpg|gif)$/,use:[
  42. {
  43. loader:"url-loader",}
  44. ],{
  45. test:/\.(woff2?|otf|eot|svg|ttf)$/,use:[
  46. {
  47. loader:"url-loader"
  48. }
  49. ]
  50. }
  51. ]
  52. },plugins:[
  53. new webpackNotifierPlugin({excludeWarnings: true}),new webpack.HotModuleReplacementPlugin()
  54. ],devtool:"cheap-eval-source-map",devServer:{
  55. host: "0.0.0.0",port: "6000",hot: true,inline: true,historyApiFallback: true
  56. }
  57. };
  58.  
  59. ProjectArray.forEach(function(f){
  60. var regex = new RegExp(".*\/source\/(.*?)\/index\.js");
  61. var name = regex.exec(f)[1];
  62. ProjectEntries[name] = f;
  63.  
  64. switch (name){
  65. case "React_i18n":
  66. {
  67. config.plugins.push(
  68. new htmlWebpackPlugin({
  69. title: name,filename: name + '.html',template: path.resolve(TEM_PATH,"./index.html"),inject: "body",chunks: ["vendors","intl",name]
  70. })
  71. );
  72. }
  73. break;
  74. case "JSTree":
  75. {
  76. config.plugins.push(
  77. new htmlWebpackPlugin({
  78. title: name,"jstreeVendor",name]
  79. })
  80. );
  81. }
  82. break;
  83. case "weather":
  84. {
  85. config.plugins.push(
  86. new htmlWebpackPlugin({
  87. title: name,"chartVendors",name]
  88. })
  89. );
  90. }
  91. break;
  92. default:
  93. {
  94. config.plugins.push(
  95. new htmlWebpackPlugin({
  96. title: name,name]
  97. })
  98. );
  99. }
  100. break;
  101. }
  102. });
  103.  
  104. config.entry = Object.assign({},config.entry,ProjectEntries);
  105.  
  106. module.exports = config;

webpack.production.config.js

  1. var webpack = require("webpack");
  2. var path = require("path");
  3. var glob = require("glob");
  4.  
  5. var htmlWebpackPlugin = require("html-webpack-plugin");
  6. var webpackCopyPlugin = require("copy-webpack-plugin");
  7. var webpackDelPlugin = require("webpack-del-plugin");
  8. var webpackNotifierPlugin = require("webpack-notifier");
  9.  
  10. var ROOT_PATH = path.resolve(__dirname);
  11. var SRC_PATH = path.resolve(ROOT_PATH,plugins:[
  12. new webpackDelPlugin({match: DIST_PATH}),new webpackNotifierPlugin({excludeWarnings: true}),new webpack.optimize.UglifyJsPlugin({minimize: true}),new webpack.optimize.CommonsChunkPlugin({
  13. name : 'vendors',filename: 'vendors.js'
  14. })
  15. ]
  16. };
  17.  
  18. ProjectArray.forEach(function(f){
  19. var regex = new RegExp(".*\/source\/(.*?)\/index\.js");
  20. var name = regex.exec(f)[1];
  21. ProjectEntries[name] = f;
  22.  
  23. switch (name){
  24. case "React_i18n":
  25. {
  26. config.plugins.push(
  27. new htmlWebpackPlugin({
  28. title: name,name]
  29. })
  30. );
  31. }
  32. break;
  33. case "JSTree":
  34. {
  35. config.plugins.push(
  36. new htmlWebpackPlugin({
  37. title: name,name]
  38. })
  39. );
  40. }
  41. break;
  42. case "weather":
  43. {
  44. config.plugins.push(
  45. new htmlWebpackPlugin({
  46. title: name,name]
  47. })
  48. );
  49. }
  50. break;
  51. default:
  52. {
  53. config.plugins.push(
  54. new htmlWebpackPlugin({
  55. title: name,name]
  56. })
  57. );
  58. }
  59. break;
  60. }
  61.  
  62.  
  63. var AssetArray = glob.sync(SRC_PATH + "/"+ name +"/*/");
  64. AssetArray.forEach(function(f){
  65. var assetRegex = new RegExp(".*\/source\/"+ name +"\/(.*?)\/");
  66. var asset = assetRegex.exec(f)[1];
  67.  
  68. if(asset !== "components"){
  69. config.plugins.push(
  70. new webpackCopyPlugin([
  71. {
  72. from:path.resolve(SRC_PATH,name,asset),to:path.resolve(DIST_PATH,asset)
  73. },{
  74. from:path.resolve(SRC_PATH,asset)
  75. }
  76. ])
  77. )
  78. }
  79. })
  80.  
  81. });
  82.  
  83. config.entry = Object.assign({},ProjectEntries);
  84.  
  85. module.exports = config;

.babelrc

  1. {
  2. "presets": [
  3. "react",["env",{
  4. "targets": {
  5. "browsers": ["last 2 versions","safari >= 7"]
  6. }
  7. }]
  8. ]
  9. }

附带官网Clock组件类的代码

  1. var React = require('react');
  2. var ReactDOM = require('react-dom');
  3. var Clock = React.createClass({
  4.  
  5. getInitialState: function() {
  6. return {date: new Date()};
  7. },render:function(){
  8. return (
  9. <div>
  10. <h1>Hello,world!</h1>
  11. <h2>It is {this.state.date.toLocaleTimeString()}.</h2>
  12. </div>
  13. );
  14. },componentDidMount:function(){
  15. this.timerID = setInterval(
  16. //此处必须bind(this),因为如果不绑定,setInterval里的this已经变化
  17. function(){this.tick()}.bind(this),1000
  18. );
  19. },componentWillUnmount:function(){
  20. clearInterval(this.timerID);
  21. },tick : function(){
  22. this.setState({
  23. date:new Date()
  24. })
  25. }
  26. });
  27.  
  28. ReactDOM.render(
  29. <Clock />,document.body
  30. );

猜你在找的React相关文章