本文目标

从零搭建出一套支持react+less+typescript+mobx的webpack配置

最简化webpack配置

首页要初始化yarn和安装webpack的依赖

yarn init -y
yarn add webpack webpack-cli -D

根目录下新建webpack.config.js文件,内容如下

const path = require('path');module.exports = {mode: 'development',// 入口 这里应用程序开始执行entry: path.resolve(__dirname, 'src/index.tsx'),// 出口
    output: {// 输出文件的目标路径path: path.resolve(__dirname, 'dist'),// 输出的文件名filename: 'bundle.js',// 输出解析文件的目录。静态资源最终访问路径 = output.publicPath + 资源loader或插件等配置路径publicPath: '/'}
}

使用命令进行打包

webpack --mode production

另外亦可以将命令配置到 package.json 中的 scripts 字段

"scripts": {"build": "webpack --mode production"
},

执行命令 yarn build 即可打包

使用模版html

html-webpack-plugin 插件 可以指定template模板文件,将会在output目录下,生成html文件,并引入打包后的js.

安装依赖

yarn add html-webpack-plugin -D

在webpack.config.js增加plugins配置

const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {//...other code
    plugins: [new HtmlWebpackPlugin({template: path.resolve(__dirname, 'src/index.html')})]
}

html-webpack-plugin 插件 还支持title、minify、filename等其他参数

配置webpack-dev-server

webpack-dev-server提供了一个简单的Web服务器和实时热更新的能力,有助于开发。

安装依赖

yarn add webpack-dev-server -D

在webpack.config.js中增加devServer配置

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {//...other code
    devServer: {hot: true,port: 3000,open: true,contentBase: path.resolve(__dirname, 'dist'),// 开发模式下写/就行啦publicPath: '/',}
}

在 package.json 的 scripts 字段中增加 start模式

"scripts": {"start": "webpack-dev-server --mode development","build": "webpack --mode production"
},

这样我们就可以通过yarn start来启动服务啦

官网devServer

链接Webpack中publicPath详解

支持加载css文件

通过使用不同的 style-loader 和 css-loader, 可以将 css 文件转换成js    文件类型。

安装依赖

yarn add style-loader css-loader -D

在webpack.config.js中增加loader配置

module.exports = {//other code
    module: {rules: [{test: /\.css/,use: ['style-loader', 'css-loader'],exclude: /node_modules/,include: path.resolve(__dirname, 'src')}]}
}

loader 可以配置以下参数:

  • test: 匹配处理文件的扩展名的正则表达式
  • use: loader名称
  • include/exclude: 手动指定必须处理的文件夹或屏蔽不需要处理的文件夹
  • query: 为loader提供额外的设置选项

支持图片加载

需要引入两个loader

  • file-loader: 解决CSS等文件中的引入图片路径问题
  • url-loader: 当图片小于limit的时候会把图片Base64编码,大于limit参数的时候还是使用file-loader进行拷贝

如果希望图片存放在单独的目录下,那么需要指定outputPath

 

安装依赖

yarn add url-loader file-loader -D

在 webpack.config.js 中增加 loader 的配置(增加在 module.rules 的数组中)。

module.exports = {//other code
    module: {rules: [{test: /\.(gif|jpg|png|bmp|eot|woff|woff2|ttf|svg)/,use: [{loader: 'url-loader',options: {limit: 8192,outputPath: 'images'}}]}]}
} 

支持编译less

很多前端都喜欢写less,所以支持less也是需要的。(sass配置方法基本相同)

安装依赖

yarn add less less-loader -D

在 webpack.config.js 中增加 loader 的配置(module.rules 数组中)。

module.exports = {//other code
    module: {rules: [{test: /\.less/,use: ['style-loader', 'css-loader', 'less-loader'],exclude: /node_modules/,include: path.resolve(__dirname, 'src')},]}
}        

 

支持转义 ES6/ES7/JSX(react)

安装ES6/ES7/JSX 转义需要 Babel 的依赖,支持装饰器。

yarn add @babel/core babel-loader @babel/preset-env @babel/preset-react @babel/plugin-proposal-decorators @babel/plugin-proposal-object-rest-spread -D

在 webpack.config.js 中增加 loader 的配置(module.rules 数组中)。

module.exports = {//other code
    module: {rules: [{test: /\.jsx?$/,use: [{loader: 'babel-loader',options: {presets: ['@babel/preset-env', '@babel/react'],plugins: [["@babel/plugin-proposal-decorators", { "legacy": true }]]}}],include: path.resolve(__dirname, 'src'),exclude: /node_modules/},]}
}

压缩JS文件

安装依赖

yarn add uglifyjs-webpack-plugin -D

在 webpack.config.js 中增加 optimization 的配置

const UglifyWebpackPlugin = require('uglifyjs-webpack-plugin');module.exports = {//other code
    optimization: {minimizer: [new UglifyWebpackPlugin({parallel: 4})]}
}

分离出CSS

因为CSS的下载和JS可以并行,当一个HTML文件很大的时候,可以把CSS单独提取出来加载

安装依赖

yarn add mini-css-extract-plugin -D

在 webpack.config.js 中增加 plugins 的配置,并且将 'style-loader' 修改为 { loader: MiniCssExtractPlugin.loader}。CSS打包在单独目录,那么配置filename。

const MiniCssExtractPlugin = require('mini-css-extract-plugin');module.exports = {//other code
    module: {rules: [{test: /\.css/,use: [{ loader: MiniCssExtractPlugin.loader}, 'css-loader'],exclude: /node_modules/,include: path.resolve(__dirname, 'src')},{test: /\.less/,use: [{ loader: MiniCssExtractPlugin.loader }, 'css-loader', 'less-loader'],exclude: /node_modules/,include: path.resolve(__dirname, 'src')},]},plugins: [new MiniCssExtractPlugin({filename: 'css/[name].css'})]
}

压缩CSS文件

安装依赖

yarn add optimize-css-assets-webpack-plugin -D

在 webpack.config.js 中的 optimization 中增加配置

const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');module.exports = {//other code
    optimization: {minimizer: [new OptimizeCssAssetsWebpackPlugin()]}
}

支持react和mobx

安装react依赖

yarn add react react-dom 

安装mobx依赖

yarn add mobx mobx-react

支持typescript

安装依赖

yarn add typescript awesome-typescript-loader -D

安装react的类型依赖(否则会有命名空间和.d.ts相关报错)

yarn add @types/react @types/react-dom

在 webpack.config.js 中增加 loader 的配置(module.rules 数组中)。

module.exports = {//other code
    module: {rules: [{test: /\.(tsx|ts)?$/,loader: "awesome-typescript-loader",exclude: /node_modules/,include: path.resolve(__dirname, 'src')}]}
}

在根目录下添加tsconfig.json

{"compilerOptions": {"baseUrl": ".","outDir": "build/dist","module": "esnext","target": "es5","lib": ["es6", "dom"],"sourceMap": true,"allowJs": true,"jsx": "react","moduleResolution": "node","experimentalDecorators": true,"rootDir": "./","forceConsistentCasingInFileNames": true,"noImplicitReturns": true,"noImplicitThis": true,"noImplicitAny": false,"importHelpers": true,"strictNullChecks": true,"suppressImplicitAnyIndexErrors": true,"noUnusedLocals": false,"allowSyntheticDefaultImports": true},"exclude": ["node_modules","build","scripts","acceptance-tests","webpack","jest","src/setupTests.ts"]
}

在根目录下添加tslint.json

{"extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],"rules": {"no-empty-interface":false,"no-empty-block":false,"no-unused-expression":false,"object-literal-sort-keys":false,"no-empty":false,"semicolon": [false, "always"],"no-default-export": false,"member-access": true,"ordered-imports": false,"import-sources-order": "any","named-imports-order": "any","interface-over-type-literal":false,"jsx-no-lambda":false,"variable-name": [true,"ban-keywords","check-format","allow-leading-underscore","allow-trailing-underscore","allow-pascal-case","allow-snake-case"],"no-console": false,"no-angle-bracket-type-assertion": false,"jsx-no-string-ref":false,"prefer-for-of":false,"member-ordering":false,"only-arrow-functions":false,"object-literal-shorthand":false},"linterOptions": {"exclude": ["config/**/*.js","node_modules/**/*.ts","coverage/lcov-report/*.js"]},"strict": false
}

打包前先清空输出目录

安装依赖

yarn add clean-webpack-plugin -D

在 webpack.config.js 中增加 plugins 的配置

const {CleanWebpackPlugin} = require('clean-webpack-plugin');module.exports = {//other code
    plugins: [new CleanWebpackPlugin()]
}

( 注意3.0版本的clean-webpack-plugin有大改动,需要通过构造函数取出CleanWebpackPlugin再用 )

至此,webpack配置已经基本能满足react+less+typescript+mobx开发需求。

完整webpack.config.js和package.json文件

webpack.config.js文件

const path = require('path');
// 打包html的插件
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 压缩JS的插件
const UglifyWebpackPlugin = require('uglifyjs-webpack-plugin');
// 分离css文件
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// 打包前先清空输出目录
const {CleanWebpackPlugin} = require('clean-webpack-plugin');module.exports = {mode: 'development',// 入口 这里应用程序开始执行entry: path.resolve(__dirname, 'src/index.tsx'),// 出口
    output: {// 所有输出文件的目标路径path: path.resolve(__dirname, 'dist'),// 输出的文件名filename: 'bundle.js',// 输出解析文件的目录,url 相对于 HTML 页面, publicPath 上线时配置的是cdn的地址。// 静态资源最终访问路径 = output.publicPath + 资源loader或插件等配置路径publicPath: './'},devServer: {hot: true,port: 3000,open: true,contentBase: path.resolve(__dirname, 'dist'),publicPath: '/',// stats: 'none'
    },module: {rules: [/** 支持css* 通过使用不同的 style-loader 和 css-loader, 可以将 css 文件转换成JS文件类型。* */{test: /\.css/,// use: ['style-loader', 'css-loader'],
                use: [{loader: MiniCssExtractPlugin.loader},'css-loader'],exclude: /node_modules/,include: path.resolve(__dirname, 'src')},/** 支持编译less和sass* */{test: /.less/,// use: ['style-loader', 'css-loader', 'less-loader'],
                use: [{loader: MiniCssExtractPlugin.loader},'css-loader','less-loader'],exclude: /node_modules/,include: path.resolve(__dirname, 'src')},/** 支持加载图片* file-loader: 解决CSS等文件中的引入图片路径问题* url-loader: 当图片小于limit的时候会把图片Base64编码,大于limit参数的时候还是使用file-loader进行拷贝* */{test: /\.(gif|jpg|png|bmp|eot|woff|woff2|ttf|svg)/,use: [{loader: 'url-loader',options: {limit: 1,outputPath: 'images'}}]},/** 支持转义 ES6/ES7/JSX  ,支持react* ES6/ES7/JSX 转义需要 Babel 的依赖,支持装饰器。* */{test: /\.jsx?$/,use: [{loader: "babel-loader",options: {presets: ['@babel/preset-env', '@babel/react'],plugins: [["@babel/plugin-proposal-decorators",// "@babel/plugin-proposal-class-properties",
                                    {"legacy": true}]]}}],exclude: /node_modules/,include: path.resolve(__dirname, 'src')},/** 支持转义 支持typescript* ES6/ES7/JSX 转义需要 Babel 的依赖,支持装饰器。* */{test: /\.(tsx|ts)?$/,loader: "awesome-typescript-loader",exclude: /node_modules/,include: path.resolve(__dirname, 'src')}]},optimization: {minimizer: [new UglifyWebpackPlugin({parallel: 4})]},plugins: [new HtmlWebpackPlugin({template: path.resolve(__dirname, 'public/index.html')}),new MiniCssExtractPlugin({filename:'css/[name].css'}),new CleanWebpackPlugin()]
};

package.json文件

{"name": "webpack_cli","version": "1.0.0","main": "index.js","license": "MIT","scripts": {"start": "webpack-dev-server --mode development","build": "webpack --mode production"},"devDependencies": {"@babel/core": "^7.5.5","@babel/plugin-proposal-decorators": "^7.4.4","@babel/plugin-proposal-object-rest-spread": "^7.5.5","@babel/preset-env": "^7.5.5","@babel/preset-react": "^7.0.0","@types/react": "^16.9.2","@types/react-dom": "^16.8.5","awesome-typescript-loader": "^5.2.1","babel-loader": "^8.0.6","clean-webpack-plugin": "^3.0.0","css-loader": "^3.2.0","file-loader": "^4.2.0","html-webpack-plugin": "^3.2.0","less": "^3.10.1","less-loader": "^5.0.0","mini-css-extract-plugin": "^0.8.0","optimize-css-assets-webpack-plugin": "^5.0.3","style-loader": "^1.0.0","typescript": "^3.5.3","uglifyjs-webpack-plugin": "^2.2.0","url-loader": "^2.1.0","webpack": "^4.39.2","webpack-cli": "^3.3.7","webpack-dev-server": "^3.8.0"},"dependencies": {"mobx": "^5.13.0","mobx-react": "^6.1.3","react": "^16.9.0","react-dom": "^16.9.0"}
}

学习更多webpack配置请进入webpack官网 webpack官网链接

本文配置将持续升级优化

转载于:https://www.cnblogs.com/piaobodewu/p/11374475.html

从零配置webpack(react+less+typescript+mobx)相关推荐

  1. react 引用本地js_从零配置webpack 4+react脚手架(二)

    前言: 你可能也注意到了,html文件中的关于js的引用是我们手动写的,那假如我们改了输出路径或打包编译之后的文件名,那我们岂不是还要手动去修改html文件中的引用?我们怎么做到,像create-re ...

  2. react 文件 md5_从零配置webpack 4+react脚手架(二)

    前言: 你可能也注意到了,html文件中的关于js的引用是我们手动写的,那假如我们改了输出路径或打包编译之后的文件名,那我们岂不是还要手动去修改html文件中的引用?我们怎么做到,像create-re ...

  3. 从零搭建webpack的react开发/生产环境

    一.初始化项目 在命令行中敲入如下命令: mkdir Webpack-react && cd Webpack-react && npm init -y 然后你就可以在你 ...

  4. webpack打包压缩混淆_细说webpack系列 3. webpack-cli 零配置打包

    大家好!我是萝卜,webpack 4 带来了大量的更新,其中一个就是webpack 4 默认不需要配置文件,下面就带大家体验一下! 初始化项目 首先创建项目,创建一个名为webpack的文件夹,进入文 ...

  5. 细说webpack 3. webpack-cli 零配置打包

    大家好!我是萝卜,webpack 4 带来了大量的更新,其中一个就是webpack 4 默认不需要配置文件,下面就带大家体验一下! 初始化项目 首先创建项目,创建一个名为webpack的文件夹,进入文 ...

  6. 如何学习配置webpack(一)

    项目小白如何从0开始配置webpack 自己配置过webpack的人应该都知道,webpack真的好复杂,一开始做项目都是拿别人现成的做做小修改,但是别人的终究没有自己配的舒服.所以我打算写这篇文章, ...

  7. react打包后图片丢失_手搭一个 React,Typescript,Koa,GraphQL 环境

    本文系原创,转载请附带作者信息:yhlben 项目地址:https://github.com/yhlben/cdfang-spider 前言 在实际的开发过程中,从零开始初始化一个项目往往很麻烦,所以 ...

  8. 零配置构建工具:parcel

    更多内容欢迎来到博客:https://imjianjian.github.io 为什么学习parcel 17年12月6日,parcel发布了第一个正式版本,目前已经在GitHub上收获了17.7k+个 ...

  9. 详解Parcel:快速,零配置web应用打包工具

    Parcel有什么特别的,我为什么要关心它? 虽然webpack提供了非常多灵活的配置,但是与之带来的是复杂度的提升,而Parcel却非常的简洁.Parcel自己的口号也是非常直白:零配置. 为什么这 ...

最新文章

  1. TCP Cluster for mqtt 技术实施方案
  2. 系统发生 1219 错误。 提供的凭据与已存在的凭据集冲突。
  3. 通过eclipse调试MapReduce任务
  4. python下的橡皮线_python线性代数常用操作
  5. 利用Win32 Debug API打造自己的调试器Debugger
  6. 锻炼后应该做的4件事
  7. 判断JavaScript对象为null或者属性为空
  8. 精妙SQL语句【转】
  9. 在香蕉派 Banana Pi BPI-M1上使用 开源 OxOffice Impress
  10. log4j2 logger_简单一致的Log4j2 Logger命名
  11. Docker实践:Cannot connect to the Docker daemon.
  12. OpenCL向量相加
  13. vue 组件之间数据传递(七)
  14. FreeRTOS调度器挂起与解除
  15. 安装Cloudreve 新版V3(go版本)
  16. laydate 和 Vue 奇怪的清空问题
  17. 服务器遭受***后的处理过程
  18. 计算机i网络管理员证书四级,软考网络管理员试题练习(4)
  19. 五、鼎捷T100总账管理之总账报表及查询
  20. 电磁兼容学习-电磁干扰三要素

热门文章

  1. NYOJ33 - 蛇形填数
  2. matlab实验论文,毕业论文--基于MATLAB的光学实验仿真
  3. Unity3D基础31:脚本生命周期
  4. 2018北京ICPC D. Frog and Portal(构造)
  5. multimap多重关联容器
  6. 类别不平衡问题之评估指标
  7. 图像平均池化 利用pytorch对图像进行池化
  8. 聚合矩阵+中心化处理
  9. 静电场里非常有用的公式
  10. Win10 IoT Core 更改密码(PowerShell)