摘要:系列環境搭建一手動搭建系列環境搭建二不同環境不同配置系列環境搭建三打包性能優化實際項目中,往往不同環境有不同的構建需求。
React系列---Webpack環境搭建(一)手動搭建
React系列---Webpack環境搭建(二)不同環境不同配置
React系列---Webpack環境搭建(三)打包性能優化
實際項目中,往往不同環境有不同的構建需求。比如開發、測試和生產環境對應的后端接口地址不同,生產環境需要進行代碼混淆、壓縮等。
因此,往往還需要將webpack配置分成多個:
安裝webpack-merge,用于合并配置:
npm install webpack-merge --save-dev
安裝uglifyjs-webpack-plugin,用于js代碼壓縮:
npm install uglifyjs-webpack-plugin --save-dev
webpack -p也可以用于代碼壓縮。相對而言,使用uglifyjs-webpack-plugin,可以對壓縮進行更靈活的控制。
拆分webpack.config.js為以下幾個配置:
基礎配置 webpack.base.config.js:
const path = require("path");
const webpack = require("webpack");
const ROOT_PATH = path.resolve(__dirname);
const SRC_PATH = path.resolve(ROOT_PATH, "src");
const BUILD_PATH = path.resolve(ROOT_PATH, "dist");
module.exports = {
entry: {
index: path.resolve(SRC_PATH, "index.jsx")
},
output: {
path: BUILD_PATH,
filename: "js/[name].[hash:5].js"
},
resolve: {
extensions: [".js", ".jsx"]
},
module: {
loaders: [
{
test: /.jsx?$/,
loaders: ["eslint-loader"],
include: SRC_PATH,
enforce: "pre"
}, {
test: /.jsx?$/,
loaders: ["babel-loader"],
include: SRC_PATH,
exclude: path.resolve(ROOT_PATH, "node_modules")
}
]
}
};
開發環境配置,webpack.dev.config.js:
const path = require("path");
const webpack = require("webpack");
const HtmlwebpackPlugin = require("html-webpack-plugin");
const merge = require("webpack-merge");
const baseConfig = require("./webpack.base.config.js");
const ROOT_PATH = path.resolve(__dirname);
const SRC_PATH = path.resolve(ROOT_PATH, "src");
const devConfig = merge(baseConfig, {
devtool: "eval-source-map",
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": ""development""
}),
new HtmlwebpackPlugin({
title: "react-webpack-demo",
filename: "index.html",
template: path.resolve(SRC_PATH, "templates", "index.html")
})
]
});
module.exports = devConfig;
測試環境配置,webpack.test.config.js:
const path = require("path");
const webpack = require("webpack");
const HtmlwebpackPlugin = require("html-webpack-plugin");
const merge = require("webpack-merge")
const baseConfig = require("./webpack.base.config.js");
const ROOT_PATH = path.resolve(__dirname);
const SRC_PATH = path.resolve(ROOT_PATH, "src");
const testConfig = merge(baseConfig, {
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": ""test""
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
}),
new HtmlwebpackPlugin({
title: "react-webpack-demo",
filename: "index.html",
template: path.resolve(SRC_PATH, "templates", "index.html"),
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeAttributeQuotes: true
}
})
]
});
module.exports = testConfig;
生成環境配置,webpack.prod.config.js:
const path = require("path");
const webpack = require("webpack");
const HtmlwebpackPlugin = require("html-webpack-plugin");
const merge = require("webpack-merge")
const baseConfig = require("./webpack.base.config.js")
const ROOT_PATH = path.resolve(__dirname);
const SRC_PATH = path.resolve(ROOT_PATH, "src");
const prodConfig = merge(baseConfig, {
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": ""production""
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
}),
new HtmlwebpackPlugin({
title: "react-webpack-demo",
filename: "index.html",
template: path.resolve(SRC_PATH, "templates", "index.html"),
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeAttributeQuotes: true
}
})
]
});
module.exports = prodConfig;
修改package.json:
"scripts": {
"start": "webpack-dev-server --hot --progress --config webpack.dev.config.js",
"build:dev": "rimraf dist && webpack --progress --config webpack.dev.config.js",
"build:test": "rimraf dist && webpack --progress --config webpack.test.config.js",
"build": "rimraf dist && webpack --progress --config webpack.prod.config.js"
},
# 啟動開發調試 npm run start # 開發環境構建 npm run build:dev # 測試環境構建 npm run build:test # 生產環境構建 npm run build
項目中就可以像下面這樣子調用后端接口
接口HOST定義,host.js:
if (process.env.NODE_ENV === "development") {
module.exports = `http://192.168.1.101:8000`
} else if (process.env.NODE_ENV === "test") {
module.exports = `http://192.168.1.102:8000`
} else {
module.exports = `http://192.168.1.103:8000`
}
接口API定義,apis.js:
import host from "./host"
function getApi (api) {
return host + api
}
export default {
login: getApi("/login"),
logout: getApi("/logout"),
...
}
代碼:https://github.com/zhutx/reac...
React系列---Webpack環境搭建(一)手動搭建
React系列---Webpack環境搭建(二)不同環境不同配置
React系列---Webpack環境搭建(三)打包性能優化
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.hztianpu.com/yun/83695.html
摘要:的選項中,是文件的輸出路徑是暴露的對象名,要跟保持一致是解析包路徑的上下文,這個要跟下面要配置的保持一致。最后修改一下模板,增加引用文件給入口文件再加點依賴模塊,方便打包觀察運行打包可以看到,入口文件里依賴的,模塊,直接引用了。 React系列---Webpack環境搭建(一)手動搭建React系列---Webpack環境搭建(二)不同環境不同配置React系列---Webpack環境...
摘要:在這篇文章中我們開始利用我們之前所學搭建一個簡易的開發環境,用以鞏固我們之前學習的知識。 文章首發于我的github及個人博客,github請看https://github.com/huruji/blo...,轉載請注明出處。 在這篇文章中我們開始利用我們之前所學搭建一個簡易的React開發環境,用以鞏固我們之前學習的Webpack知識。首先我們需要明確這次開發環境需要達到的效果:1、...
摘要:官方文檔中文翻譯構建用戶界面的庫。官方文檔建議學習時以官方文檔為準,中文翻譯或者第三方作者的教程可以幫助你理清思路會用到的重要知識點我也會進行簡明的解釋,如遇到錯誤或者不理解的內容,歡迎實時指出。 前言 前面提到前端大統一的概念,如果感興趣,歡迎說說自己的看法,點擊前往。Web前端框架層出不窮,不可能面面俱到,這里給個小建議: 如果對Weex App感興趣,應該選擇vue框架; 如果...
摘要:官方文檔中文翻譯構建用戶界面的庫。官方文檔建議學習時以官方文檔為準,中文翻譯或者第三方作者的教程可以幫助你理清思路會用到的重要知識點我也會進行簡明的解釋,如遇到錯誤或者不理解的內容,歡迎實時指出。 前言 前面提到前端大統一的概念,如果感興趣,歡迎說說自己的看法,點擊前往。Web前端框架層出不窮,不可能面面俱到,這里給個小建議: 如果對Weex App感興趣,應該選擇vue框架; 如果...
閱讀 3034·2021-11-22 13:52
閱讀 1545·2021-10-14 09:43
閱讀 3890·2019-08-30 15:56
閱讀 3174·2019-08-30 13:22
閱讀 3527·2019-08-30 13:10
閱讀 1828·2019-08-26 13:45
閱讀 1326·2019-08-26 11:47
閱讀 3038·2019-08-23 18:13