npm init
一直回車就好
npm i --save react react-dom npm i --save-dev babel-loader @babel/core @babel/preset-env @babel/preset-react npm i --save-dev webpack webpack-cli webpack-dev-server
react相關依賴包
webpack依賴包
babel相關依賴包,把jsx轉成js可識別的函數
建立一個config目錄,用於存放webpack的相關配置
建立一個src目錄,用於存放工做代碼
在config文件夾中建立webpack.config.dev.js文件,配置出口/入口/loader/插件
const resolve = (dir) => path.join(__dirname, '../', dir) module.exports = { mode: 'development', // 指定運行環境 devtool: 'cheap-module-eval-source-map', // source-map entry: { app: resolve('src/index.jsx') }, output: { filename: '[name].js', path: resolve('dist') }, module: { } plugins: [ ] }
接下來裝插件和loader完成dev的webpack配置
npm i --save-dev html-webpack-plugin
這個插件的做用是複製一個文件到另一個文件裏,咱們能夠用來複制一個入口模板的HTML到打包目錄裏
plugins: [ new HtmlWebpackPlugin({ filename: resolve('dist/index.html'), template: resolve("static/index.html") }) ]
npm i --save-dev style-loader css-loader postcss-loader sass-loader npm i --save-dev file-laoder npm i --save-dev svg-inline-loader npm i --save-dev react-hot-loader
- 把sass成webpack識別的模塊
- 爲了引入圖片
- 爲了引入svg
- 熱更新loader
module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, loader: 'babel-loader', options: { plugins: ['react-hot-loader/babel'], // 熱更新插件 presets: ['@babel/preset-env', '@babel/preset-react'] // jsx轉爲js函數 }, }, { test: /\.scss/, // 把scss轉爲webpack可識別的模塊 loaders: [ 'style-loader', { loader: 'css-loader', options: { importLoaders: 1, }, }, { loader: 'postcss-loader', options: { options: {}, } }, 'sass-loader' ] }, { test: /\.(png|jpg|gif)$/, use: [ { loader: 'file-loader', options: { name: '[path][name].[ext]' } } ] }, { test: /\.svg/, use: [ { loader: 'svg-inline-loader' } ] } ] },
devServer: { // dev server host: 'localhost', port: '8001', open: true },
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "webpack-dev-server --config config/webpack.config.dev.js" },
const path = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const resolve = (dir) => path.join(__dirname, '../', dir) module.exports = { mode: 'development', devtool: 'cheap-module-eval-source-map', entry: { // 建立src文件夾/並建立index.jsx入口文件 app: resolve('src/index.jsx') }, output: { filename: '[name].js', path: resolve('dist') }, resolve: { extensions: ['.js', '.jsx', '.scss', '.css'] }, module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, loader: 'babel-loader', options: { plugins: ['react-hot-loader/babel'], presets: ['@babel/preset-env', '@babel/preset-react'] }, }, { test: /\.scss/, loaders: [ 'style-loader', { loader: 'css-loader', options: { importLoaders: 1, }, }, { loader: 'postcss-loader', options: { options: {}, } }, 'sass-loader' ] }, { test: /\.(png|jpg|gif)$/, use: [ { loader: 'file-loader', options: { name: '[path][name].[ext]' } } ] }, { test: /\.svg/, use: [ { loader: 'svg-inline-loader' } ] } ] }, devServer: { host: 'localhost', port: '8001', open: true }, plugins: [ new HtmlWebpackPlugin({ filename: resolve('dist/index.html'), template: resolve("static/index.html") }) ] }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>hello webpack</title> </head> <body> <div id="root"></div> </body> </html>
import ReactDOM from 'react-dom' import React from 'react' ReactDOM.render( <h1>hello webpack</h1>, document.getElementById('root') )
npm run dev 運行成功後,瀏覽器會自動打開一個有熱更新的簡單的react的開發環境就配置好了。若是有什麼問題歡迎留言指正css