当前位置: 首页 > news >正文

大学生网站开发文档wordpress建站的好处

大学生网站开发文档,wordpress建站的好处,网站开发技术最新技术,渭南网站建设电话提升打包构建速度 一、HotModuleReplacement 为什么 开发时我们修改了其中一个模块代码#xff0c;Webpack 默认会将所有模块全部重新打包编译#xff0c;速度很慢。 所以我们需要做到修改某个模块代码#xff0c;就只有这个模块代码需要重新打包编译#xff0c;其他模…提升打包构建速度 一、HotModuleReplacement 为什么 开发时我们修改了其中一个模块代码Webpack 默认会将所有模块全部重新打包编译速度很慢。 所以我们需要做到修改某个模块代码就只有这个模块代码需要重新打包编译其他模块不变这样打包速度就能很快。 是什么 HotModuleReplacementHMR/热模块替换在程序运行中替换、添加或删除模块而无需重新加载整个页面。 怎么用 基本配置 module.exports {// 其他省略devServer: {host: localhost, // 启动服务器域名port: 3000, // 启动服务器端口号open: true, // 是否自动打开浏览器hot: true, // 开启HMR功能只能用于开发环境生产环境不需要了}, };此时 css 样式经过 style-loader 处理已经具备 HMR 功能了。 但是 js 还不行。 JS 配置 // main.js import count from ./js/count; import sum from ./js/sum; // 引入资源Webpack才会对其打包 import ./css/iconfont.css; import ./css/index.css; import ./less/index.less; import ./sass/index.sass; import ./sass/index.scss; import ./styl/index.styl;const result1 count(2, 1); console.log(result1); const result2 sum(1, 2, 3, 4); console.log(result2);// 判断是否支持HMR功能 if (module.hot) {module.hot.accept(./js/count.js, function (count) {const result1 count(2, 1);console.log(result1);});module.hot.accept(./js/sum.js, function (sum) {const result2 sum(1, 2, 3, 4);console.log(result2);}); }上面这样写会很麻烦所以实际开发我们会使用其他 loader 来解决。 比如vue-loader, react-hot-loader。 二、OneOf 为什么 打包时每个文件都会经过所有 loader 处理虽然因为 test 正则原因实际没有处理上但是都要过一遍。比较慢。 是什么 顾名思义就是只能匹配上一个 loader, 剩下的就不匹配了。 怎么用 const path require(path); const ESLintWebpackPlugin require(eslint-webpack-plugin); const HtmlWebpackPlugin require(html-webpack-plugin);module.exports {entry: ./src/main.js,output: {path: undefined, // 开发模式没有输出不需要指定输出目录filename: static/js/main.js, // 将 js 文件输出到 static/js 目录中// clean: true, // 开发模式没有输出不需要清空输出结果},module: {rules: [{oneOf: [{// 用来匹配 .css 结尾的文件test: /\.css$/,// use 数组里面 Loader 执行顺序是从右到左use: [style-loader, css-loader],},{test: /\.less$/,use: [style-loader, css-loader, less-loader],},{test: /\.s[ac]ss$/,use: [style-loader, css-loader, sass-loader],},{test: /\.styl$/,use: [style-loader, css-loader, stylus-loader],},{test: /\.(png|jpe?g|gif|webp)$/,type: asset,parser: {dataUrlCondition: {maxSize: 10 * 1024, // 小于10kb的图片会被base64处理},},generator: {// 将图片文件输出到 static/imgs 目录中// 将图片文件命名 [hash:8][ext][query]// [hash:8]: hash值取8位// [ext]: 使用之前的文件扩展名// [query]: 添加之前的query参数filename: static/imgs/[hash:8][ext][query],},},{test: /\.(ttf|woff2?)$/,type: asset/resource,generator: {filename: static/media/[hash:8][ext][query],},},{test: /\.js$/,exclude: /node_modules/, // 排除node_modules代码不编译loader: babel-loader,},],},],},plugins: [new ESLintWebpackPlugin({// 指定检查文件的根目录context: path.resolve(__dirname, ../src),}),new HtmlWebpackPlugin({// 以 public/index.html 为模板创建文件// 新的html文件有两个特点1. 内容和源文件一致 2. 自动引入打包生成的js等资源template: path.resolve(__dirname, ../public/index.html),}),],// 开发服务器devServer: {host: localhost, // 启动服务器域名port: 3000, // 启动服务器端口号open: true, // 是否自动打开浏览器hot: true, // 开启HMR功能},mode: development,devtool: cheap-module-source-map, };生产模式也是如此配置。 三、Include/Exclude 为什么 开发时我们需要使用第三方的库或插件所有文件都下载到 node_modules 中了。而这些文件是不需要编译可以直接使用的。 所以我们在对 js 文件处理时要排除 node_modules 下面的文件。 是什么 include 包含只处理 xxx 文件 exclude 排除除了 xxx 文件以外其他文件都处理 怎么用 const path require(path); const ESLintWebpackPlugin require(eslint-webpack-plugin); const HtmlWebpackPlugin require(html-webpack-plugin);module.exports {entry: ./src/main.js,output: {path: undefined, // 开发模式没有输出不需要指定输出目录filename: static/js/main.js, // 将 js 文件输出到 static/js 目录中// clean: true, // 开发模式没有输出不需要清空输出结果},module: {rules: [{oneOf: [{// 用来匹配 .css 结尾的文件test: /\.css$/,// use 数组里面 Loader 执行顺序是从右到左use: [style-loader, css-loader],},{test: /\.less$/,use: [style-loader, css-loader, less-loader],},{test: /\.s[ac]ss$/,use: [style-loader, css-loader, sass-loader],},{test: /\.styl$/,use: [style-loader, css-loader, stylus-loader],},{test: /\.(png|jpe?g|gif|webp)$/,type: asset,parser: {dataUrlCondition: {maxSize: 10 * 1024, // 小于10kb的图片会被base64处理},},generator: {// 将图片文件输出到 static/imgs 目录中// 将图片文件命名 [hash:8][ext][query]// [hash:8]: hash值取8位// [ext]: 使用之前的文件扩展名// [query]: 添加之前的query参数filename: static/imgs/[hash:8][ext][query],},},{test: /\.(ttf|woff2?)$/,type: asset/resource,generator: {filename: static/media/[hash:8][ext][query],},},{test: /\.js$/,// exclude: /node_modules/, // 排除node_modules代码不编译include: path.resolve(__dirname, ../src), // 也可以用包含loader: babel-loader,},],},],},plugins: [new ESLintWebpackPlugin({// 指定检查文件的根目录context: path.resolve(__dirname, ../src),exclude: node_modules, // 默认值}),new HtmlWebpackPlugin({// 以 public/index.html 为模板创建文件// 新的html文件有两个特点1. 内容和源文件一致 2. 自动引入打包生成的js等资源template: path.resolve(__dirname, ../public/index.html),}),],// 开发服务器devServer: {host: localhost, // 启动服务器域名port: 3000, // 启动服务器端口号open: true, // 是否自动打开浏览器hot: true, // 开启HMR功能},mode: development,devtool: cheap-module-source-map, };生产模式也是如此配置。 四、Cache 为什么 每次打包时 js 文件都要经过 Eslint 检查 和 Babel 编译速度比较慢。 我们可以缓存之前的 Eslint 检查 和 Babel 编译结果这样第二次打包时速度就会更快了。 是什么 对 Eslint 检查 和 Babel 编译结果进行缓存。 怎么用 const path require(path); const ESLintWebpackPlugin require(eslint-webpack-plugin); const HtmlWebpackPlugin require(html-webpack-plugin);module.exports {entry: ./src/main.js,output: {path: undefined, // 开发模式没有输出不需要指定输出目录filename: static/js/main.js, // 将 js 文件输出到 static/js 目录中// clean: true, // 开发模式没有输出不需要清空输出结果},module: {rules: [{oneOf: [{// 用来匹配 .css 结尾的文件test: /\.css$/,// use 数组里面 Loader 执行顺序是从右到左use: [style-loader, css-loader],},{test: /\.less$/,use: [style-loader, css-loader, less-loader],},{test: /\.s[ac]ss$/,use: [style-loader, css-loader, sass-loader],},{test: /\.styl$/,use: [style-loader, css-loader, stylus-loader],},{test: /\.(png|jpe?g|gif|webp)$/,type: asset,parser: {dataUrlCondition: {maxSize: 10 * 1024, // 小于10kb的图片会被base64处理},},generator: {// 将图片文件输出到 static/imgs 目录中// 将图片文件命名 [hash:8][ext][query]// [hash:8]: hash值取8位// [ext]: 使用之前的文件扩展名// [query]: 添加之前的query参数filename: static/imgs/[hash:8][ext][query],},},{test: /\.(ttf|woff2?)$/,type: asset/resource,generator: {filename: static/media/[hash:8][ext][query],},},{test: /\.js$/,// exclude: /node_modules/, // 排除node_modules代码不编译include: path.resolve(__dirname, ../src), // 也可以用包含loader: babel-loader,options: {cacheDirectory: true, // 开启babel编译缓存cacheCompression: false, // 缓存文件不要压缩},},],},],},plugins: [new ESLintWebpackPlugin({// 指定检查文件的根目录context: path.resolve(__dirname, ../src),exclude: node_modules, // 默认值cache: true, // 开启缓存// 缓存目录cacheLocation: path.resolve(__dirname,../node_modules/.cache/.eslintcache),}),new HtmlWebpackPlugin({// 以 public/index.html 为模板创建文件// 新的html文件有两个特点1. 内容和源文件一致 2. 自动引入打包生成的js等资源template: path.resolve(__dirname, ../public/index.html),}),],// 开发服务器devServer: {host: localhost, // 启动服务器域名port: 3000, // 启动服务器端口号open: true, // 是否自动打开浏览器hot: true, // 开启HMR功能},mode: development,devtool: cheap-module-source-map, };五、Thead 为什么 当项目越来越庞大时打包速度越来越慢甚至于需要一个下午才能打包出来代码。这个速度是比较慢的。 我们想要继续提升打包速度其实就是要提升 js 的打包速度因为其他文件都比较少。 而对 js 文件处理主要就是 eslint 、babel、Terser 三个工具所以我们要提升它们的运行速度。 我们可以开启多进程同时处理 js 文件这样速度就比之前的单进程打包更快了。 是什么 多进程打包开启电脑的多个进程同时干一件事速度更快。 需要注意请仅在特别耗时的操作中使用因为每个进程启动就有大约为 600ms 左右开销。 怎么用 我们启动进程的数量就是我们 CPU 的核数。 如何获取 CPU 的核数因为每个电脑都不一样。 // nodejs核心模块直接使用 const os require(os); // cpu核数 const threads os.cpus().length;下载包 npm i thread-loader -D使用 const os require(os); const path require(path); const ESLintWebpackPlugin require(eslint-webpack-plugin); const HtmlWebpackPlugin require(html-webpack-plugin); const MiniCssExtractPlugin require(mini-css-extract-plugin); const CssMinimizerPlugin require(css-minimizer-webpack-plugin); const TerserPlugin require(terser-webpack-plugin);// cpu核数 const threads os.cpus().length;// 获取处理样式的Loaders const getStyleLoaders (preProcessor) {return [MiniCssExtractPlugin.loader,css-loader,{loader: postcss-loader,options: {postcssOptions: {plugins: [postcss-preset-env, // 能解决大多数样式兼容性问题],},},},preProcessor,].filter(Boolean); };module.exports {entry: ./src/main.js,output: {path: path.resolve(__dirname, ../dist), // 生产模式需要输出filename: static/js/main.js, // 将 js 文件输出到 static/js 目录中clean: true,},module: {rules: [{oneOf: [{// 用来匹配 .css 结尾的文件test: /\.css$/,// use 数组里面 Loader 执行顺序是从右到左use: getStyleLoaders(),},{test: /\.less$/,use: getStyleLoaders(less-loader),},{test: /\.s[ac]ss$/,use: getStyleLoaders(sass-loader),},{test: /\.styl$/,use: getStyleLoaders(stylus-loader),},{test: /\.(png|jpe?g|gif|webp)$/,type: asset,parser: {dataUrlCondition: {maxSize: 10 * 1024, // 小于10kb的图片会被base64处理},},generator: {// 将图片文件输出到 static/imgs 目录中// 将图片文件命名 [hash:8][ext][query]// [hash:8]: hash值取8位// [ext]: 使用之前的文件扩展名// [query]: 添加之前的query参数filename: static/imgs/[hash:8][ext][query],},},{test: /\.(ttf|woff2?)$/,type: asset/resource,generator: {filename: static/media/[hash:8][ext][query],},},{test: /\.js$/,// exclude: /node_modules/, // 排除node_modules代码不编译include: path.resolve(__dirname, ../src), // 也可以用包含use: [{loader: thread-loader, // 开启多进程options: {workers: threads, // 数量},},{loader: babel-loader,options: {cacheDirectory: true, // 开启babel编译缓存},},],},],},],},plugins: [new ESLintWebpackPlugin({// 指定检查文件的根目录context: path.resolve(__dirname, ../src),exclude: node_modules, // 默认值cache: true, // 开启缓存// 缓存目录cacheLocation: path.resolve(__dirname,../node_modules/.cache/.eslintcache),threads, // 开启多进程}),new HtmlWebpackPlugin({// 以 public/index.html 为模板创建文件// 新的html文件有两个特点1. 内容和源文件一致 2. 自动引入打包生成的js等资源template: path.resolve(__dirname, ../public/index.html),}),// 提取css成单独文件new MiniCssExtractPlugin({// 定义输出文件名和目录filename: static/css/main.css,}),// css压缩// new CssMinimizerPlugin(),],optimization: {minimize: true,minimizer: [// css压缩也可以写到optimization.minimizer里面效果一样的new CssMinimizerPlugin(),// 当生产模式会默认开启TerserPlugin但是我们需要进行其他配置就要重新写了new TerserPlugin({parallel: threads // 开启多进程})],},// devServer: {// host: localhost, // 启动服务器域名// port: 3000, // 启动服务器端口号// open: true, // 是否自动打开浏览器// },mode: production,devtool: source-map, };我们目前打包的内容都很少所以因为启动进程开销原因使用多进程打包实际上会显著的让我们打包时间变得很长。
http://www.dnsts.com.cn/news/55707.html

相关文章:

  • 哪些网站做任务可以赚钱网站建设九步走
  • 用动态和静态设计一个网站打开网站是空白页面
  • 网站建设运营费用网站建设服装市场分析报告
  • cms搭建网站山东省住房和城乡建设厅电话号码
  • 上海网站设计服务商如何推广自己的网站和产品
  • yii2 网站开发福建网站建设公司排名
  • 上海网站建设网页设计电商网站设计原则
  • 自己怎么做彩票网站wordpress改变语言
  • 淄博网站开发网泰快昆山规建设局网站
  • 电脑商城网站模板wordpress新闻轮播制作
  • 淘宝网站建设策划案原创视频素材哪里弄
  • 企业的网站内容管理系统做网站流量点击分析的软件
  • 东莞网站设计出名 乐云践新wordpress标签订阅插件
  • js怎么做网站网站免费正能量直接进入小说
  • 沈阳seo网站推广优化iis ip访问网站
  • 做网站有哪些公司前端兼职平台的行业前景
  • 计算机论文网站优化团队
  • 网站域名等级网站建设合作加盟
  • 网上做调查赚钱的网站有哪些市场营销是做什么的
  • 网站如何用微信支付桂林两江四湖象山景区讲解导游词
  • 内容网站 如何做采集湛洪波.jsp网站开发详解
  • 网站名称写什么网站设计首页
  • 文化传媒 网站设计wordpress登记打印
  • 垂直网站建设方案宁波做网站的企业
  • 网站改版协议建设企业查询网站
  • 移动网站建站西昌做网站
  • 手机网站jquery底部导航菜单辽阳网站建设辽阳
  • 网站报价内容乐陵森林酒店家具
  • wordpress创建多站点鹤壁集团网站建设
  • 免费注册个人网站合肥百度 网站建设