国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

使用webpack搭建react開發環境的方法

來源:懂視網 責編:小采 時間:2020-11-27 22:14:39
文檔

使用webpack搭建react開發環境的方法

使用webpack搭建react開發環境的方法:1.初始化項目 mkdir react-redux && cd react-redux npm init -y 2.安裝webpack npm i webpack -D npm i -D 是 npm install --save-dev 的簡寫,是指安裝模塊并保存到 package.json 的 devDependencies中,主要在開發環境中
推薦度:
導讀使用webpack搭建react開發環境的方法:1.初始化項目 mkdir react-redux && cd react-redux npm init -y 2.安裝webpack npm i webpack -D npm i -D 是 npm install --save-dev 的簡寫,是指安裝模塊并保存到 package.json 的 devDependencies中,主要在開發環境中

1.初始化項目

mkdir react-redux && cd react-redux
npm init -y

2.安裝webpack

npm i webpack -D

npm i -D 是 npm install --save-dev 的簡寫,是指安裝模塊并保存到 package.json 的 devDependencies中,主要在開發環境中的依賴包. 如果使用webpack 4+ 版本,還需要安裝 CLI。

npm install -D webpack webpack-cli

3.新建一下項目結構

react-redux
 |- package.json
+ |- /dist
+ |- index.html
 |- /src
 |- index.js

index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
<div id="root"></div>
<script src="bundle.js"></script>
</body>
</html>

index.js

document.querySelector('#root').innerHTML = 'webpack使用';

非全局安裝下的打包。

node_modules\.bin\webpack src\index.js --output dist\bundle.js --mode development

打開dist目錄下的html顯示 webpack使用

配置webpack

1.使用配置文件

const path=require('path');
module.exports={
 entry:'./src/index.js',
 output:{
 filename:'bundle.js',
 path:path.resolve(__dirname,'dist')
 }
};

運行命令: node_modules\.bin\webpack --mode production 可以以進行打包 2.NPM 腳本(NPM Scripts) 在在 package.json 添加一個 npm 腳本(npm script): "build": "webpack --mode production" 運行 npm run build 即可打包

使用webpack構建本地服務器

webpack-dev-server 提供了一個簡單的 web 服務器,并且能夠實時重新加載。

1.安裝 npm i -D webpack-dev-server 修改配置文件webpack.config.js

const path=require('path');
module.exports={
 entry:'./src/index.js',
 output:{
 filename:'bundle.js',
 path:path.resolve(__dirname,'dist')
 },
 //以下是新增的配置
 devServer:{
 contentBase: "./dist",//本地服務器所加載的頁面所在的目錄
 historyApiFallback: true,//不跳轉
 inline: true,//實時刷新
 port:3000,
 open:true,//自動打開瀏覽器
 }
};

運行 webpack-dev-server --progress ,瀏覽器打開localhost:3000,修改代碼會實時顯示修改的結果. 添加scripts腳本,運行 npm start 自動打開 http://localhost:8080/

"start": "webpack-dev-server --open --mode development"

啟動webpack-dev-server后,在目標文件夾中是看不到編譯后的文件的,實時編譯后的文件都保存到了內存當中。因此使用webpack-dev-server進行開發的時候都看不到編譯后的文件

2.熱更新

配置一個webpack自帶的插件并且還要在主要js文件里檢查是否有module.hot

plugins:[
 //熱更新,不是刷新
 new webpack.HotModuleReplacementPlugin()
 ],

在主要js文件里添加以下代碼

if (module.hot){
 //實現熱更新
 module.hot.accept();
}

在webpack.config.js中開啟熱更新

devServer:{
 contentBase: "./dist",//本地服務器所加載的頁面所在的目錄
 historyApiFallback: true,//不跳轉
 inline: true,//實時刷新
 port:3000,
 open:true,//自動打開瀏覽器
 hot:true //開啟熱更新
 },

熱更新允許在運行時更新各種模塊,而無需進行完全刷新.

配置Html模板

1.安裝html-webpack-plugin插件

npm i html-webpack-plugin -D

2.在webpack.config.js里引用插件

const path=require('path');
let webpack=require('webpack');
let HtmlWebpackPlugin=require('html-webpack-plugin');
module.exports={
 entry:'./src/index.js',
 output:{
 //添加hash可以防止文件緩存,每次都會生成4位hash串
 filename:'bundle.[hash:4].js',
 path:path.resolve('dist')
 },
 //以下是新增的配置
 devServer:{
 contentBase: "./dist",//本地服務器所加載的頁面所在的目錄
 historyApiFallback: true,//不跳轉
 inline: true,//實時刷新
 port:3000,
 open:true,//自動打開瀏覽器
 hot:true //開啟熱更新
 },
 plugins:[
 new HtmlWebpackPlugin({
 template:'./src/index.html',
 hash:true, //會在打包好的bundle.js后面加上hash串
 })
 ]
};

運行 npm run build 進行打包,這時候每次npm run build的時候都會在dist目錄下創建很多打好的包.應該每次打包之前都將dist目錄下的文件清空,再把打包好的文件放進去,這里使用clean-webpack-plugin插件.通過 npm i clean-webpack-plugin -D 命令安裝.然后在webpack.config.js中引用插件.

const path=require('path');
let webpack=require('webpack');
let HtmlWebpackPlugin=require('html-webpack-plugin');
let CleanWebpackPlugin=require('clean-webpack-plugin');
module.exports={
 entry:'./src/index.js',
 output:{
 //添加hash可以防止文件緩存,每次都會生成4位hash串
 filename:'bundle.[hash:4].js',
 path:path.resolve('dist')
 },
 //以下是新增的配置
 devServer:{
 contentBase: "./dist",//本地服務器所加載的頁面所在的目錄
 historyApiFallback: true,//不跳轉
 inline: true,//實時刷新
 port:3000,
 open:true,//自動打開瀏覽器
 hot:true //開啟熱更新
 },
 plugins:[
 new HtmlWebpackPlugin({
 template:'./src/index.html',
 hash:true, //會在打包好的bundle.js后面加上hash串
 }),
 //打包前先清空
 new CleanWebpackPlugin('dist')
 ]
};

之后打包便不會產生多余的文件.

編譯es6和jsx

1.安裝babel npm i babel-core babel-loader babel-preset-env babel-preset-react babel-preset-stage-0 -D babel-loader: babel加載器 babel-preset-env : 根據配置的 env 只編譯那些還不支持的特性。 babel-preset-react: jsx 轉換成js

2.添加.babelrc配置文件

{
 "presets": ["env", "stage-0","react"] //從左向右解析
}

3.修改webpack.config.js

const path=require('path');
module.exports={
 entry:'./src/index.js',
 output:{
 filename:'bundle.js',
 path:path.resolve(__dirname,'dist')
 },
 //以下是新增的配置
 devServer:{
 contentBase: "./dist",//本地服務器所加載的頁面所在的目錄
 historyApiFallback: true,//不跳轉
 inline: true//實時刷新
 },
 module:{
 rules:[
 {
 test:/\.js$/,
 exclude:/(node_modules)/, //排除掉nod_modules,優化打包速度
 use:{
 loader:'babel-loader'
 }
 }
 ]
 }
};

開發環境與生產環境分離

1.安裝 webpack-merge

npm install --save-dev webpack-merge

2.新建一個名為webpack.common.js文件作為公共配置,寫入以下內容:

const path=require('path');
let webpack=require('webpack');
let HtmlWebpackPlugin=require('html-webpack-plugin');
let CleanWebpackPlugin=require('clean-webpack-plugin');
module.exports={
 entry:['babel-polyfill','./src/index.js'],
 output:{
 //添加hash可以防止文件緩存,每次都會生成4位hash串
 filename:'bundle.[hash:4].js',
 path:path.resolve(__dirname,'dist')
 },
 plugins:[
 new HtmlWebpackPlugin({
 template:'./src/index.html',
 hash:true, //會在打包好的bundle.js后面加上hash串
 }),
 //打包前先清空
 new CleanWebpackPlugin('dist'),
 new webpack.HotModuleReplacementPlugin() //查看要修補(patch)的依賴
 ],
 module:{
 rules:[
 {
 test:/\.js$/,
 exclude:/(node_modules)/, //排除掉nod_modules,優化打包速度
 use:{
 loader:'babel-loader'
 }
 }
 ]
 }
};

3.新建一個名為webpack.dev.js文件作為開發環境配置

const merge=require('webpack-merge');
const path=require('path');
let webpack=require('webpack');
const common=require('./webpack.common.js');
module.exports=merge(common,{
 devtool:'inline-soure-map',
 mode:'development',
 devServer:{
 historyApiFallback: true, //在開發單頁應用時非常有用,它依賴于HTML5 history API,如果設置為true,所有的跳轉將指向index.html
 contentBase:path.resolve(__dirname, '../dist'),//本地服務器所加載的頁面所在的目錄
 inline: true,//實時刷新
 open:true,
 compress: true,
 port:3000,
 hot:true //開啟熱更新
 },
 plugins:[
 //熱更新,不是刷新
 new webpack.HotModuleReplacementPlugin(),
 ],
});

4.新建一個名為webpack.prod.js的文件作為生產環境配置

const merge = require('webpack-merge');
 const path=require('path');
 let webpack=require('webpack');
 const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
 const common = require('./webpack.common.js');
 module.exports = merge(common, {
 mode:'production',
 plugins: [
 new UglifyJSPlugin()
 ]
 });

配置react

1.安裝react、

react-dom npm i react react-dom -S 

2.新建App.js,添加以下內容.

import React from 'react';
class App extends React.Component{
 render(){
 return (<div>佳佳加油</div>);
 }
}
export default App;

3.在index.js添加以下內容.

import React from 'react';
import ReactDOM from 'react-dom';
import {AppContainer} from 'react-hot-loader';
import App from './App';
ReactDOM.render(
 <AppContainer>
 <App/>
 </AppContainer>,
 document.getElementById('root')
);

if (module.hot) {
 module.hot.accept();
}

4.安裝 react-hot-loader

npm i -D react-hot-loader 

5.修改配置文件 在 webpack.config.js 的 entry 值里加上 react-hot-loader/patch,一定要寫在entry 的最前面,如果有 babel-polyfill 就寫在babel-polyfill 的后面

6.在 .babelrc 里添加 plugin, "plugins": ["react-hot-loader/babel"]

處理SASS

1.安裝 style-loader css-loader url-loader

npm install style-loader css-loader url-loader --save-dev 

2.安裝 sass-loader node-sass

npm install sass-loader node-sass --save-dev 

3.安裝 mini-css-extract-plugin ,提取單獨打包css文件

npm install --save-dev mini-css-extract-plugin 

4.配置webpack配置文件

webpack.common.js

{
 test:/\.(png|jpg|gif)$/,
 use:[
 "url-loader"
 ]
},

webpack.dev.js

{
 test:/\.scss$/,
 use:[
 "style-loader",
 "css-loader",
 "sass-loader"
 ]
}

webpack.prod.js

const merge = require('webpack-merge');
 const path=require('path');
 let webpack=require('webpack');
 const MiniCssExtractPlugin=require("mini-css-extract-plugin");
 const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
 const common = require('./webpack.common.js');
 module.exports = merge(common, {
 mode:'production',
 module:{
 rules:[
 {
 test:/\.scss$/,
 use:[
 // fallback to style-loader in development
 process.env.NODE_ENV !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader,
 "css-loader",
 "sass-loader"
 ]
 }
 ]
 },
 plugins: [
 new UglifyJSPlugin(),
 new MiniCssExtractPlugin({
 // Options similar to the same options in webpackOptions.output
 // both options are optional
 filename: "[name].css",
 chunkFilename: "[id].css"
 })
 ]
 });

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

使用webpack搭建react開發環境的方法

使用webpack搭建react開發環境的方法:1.初始化項目 mkdir react-redux && cd react-redux npm init -y 2.安裝webpack npm i webpack -D npm i -D 是 npm install --save-dev 的簡寫,是指安裝模塊并保存到 package.json 的 devDependencies中,主要在開發環境中
推薦度:
標簽: 創建 開發 配置
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 一本综合久久国产二区 | 亚洲成人一区在线 | 久久久久无码国产精品一区 | 国产成人久久精品二区三区牛 | 国产传媒一区二区三区四区五区 | 国产va在线播放 | 亚洲欧洲日韩国产 | 一区二区视频在线播放 | 91国内精品久久久久免费影院 | 国产高清在线免费视频 | 国产91精品高清一区二区三区 | 亚洲视频播放 | 国内精品久久久久久久aa护士 | 精品国产免费人成在线观看 | 久久精品国产精品亚洲毛片 | 久久亚洲精品国产精品婷婷 | 日韩一区三区 | 亚洲欧美日韩第一页 | 在线视频欧美日韩 | 久久久久88色偷偷免费 | 亚洲一区 中文字幕 久久 | 国产啪在线91 | 天码毛片一区二区三区入口 | 国产精品第一区在线观看 | 91久久精品一区二区三区 | 亚洲欧美精选 | 国产精品视频一区二区三区w | 综合伊人久久在一二三区 | 殴美aⅴ| 99久久国内精品成人免费 | 欧美第一页| 二区久久国产乱子伦免费精品 | 国产精品手机视频一区二区 | 欧美性野久久久久久久久 | 亚州一区二区 | 另类国产精品一区二区 | 久久99精品久久 | 国产高清美女一级a毛片久久 | 亚洲欧美一区二区三区九九九 | 91精品国产高清91久久久久久 | 毛片免费观看成人 |