webpack构建简单的react开发环境
- 目录结构
- package.json
- webpack配置
- .babelrc
- src
- 编译
- 效果图
目录结构
package.json
{
"name": "react-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack.config.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.36.1",
"webpack-cli": "^3.3.6"
}
}
webpack配置
const path = require('path');
// 导入自动生成HTMl文件的插件
const htmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
mode: 'development',// 设置mode
plugins: [ // 添加plugins节点配置插件
new htmlWebpackPlugin({
template: path.resolve(__dirname, 'src/index.html'),//模板路径
filename: 'index.html'//自动生成的HTML文件的名称
})
],
module: { // 用来配置第三方loader模块的
rules: [ // 文件的匹配规则
{ test: /\.js|jsx$/, use: 'babel-loader', exclude: /node_modules/ }
]
}
};
.babelrc
{
"presets": ["env", "stage-0", "react"],
"plugins": ["transform-runtime"]
}
src
编译
npm run build