umi配置gzip

配置项目

1.项目安装plugin

yarn add compression-webpack-plugin -D

2.config/config.js文件中添加配置

 const CompressionWebpackPlugin = require('compression-webpack-plugin');
const prodGzipList = ['js', 'css'];

chainWebpack: config => {
  if (process.env.NODE_ENV === 'production') {  // 生产模式开启
    config.plugin('compression-webpack-plugin').use(
      new CompressionWebpackPlugin({
        // filename: 文件名称,这里我们不设置,让它保持和未压缩的文件同一个名称
        algorithm: 'gzip', // 指定生成gzip格式
        test: new RegExp('\\.(' + prodGzipList.join('|') + ')$'), // 匹配哪些格式文件需要压缩
        threshold: 10240, //对超过10k的数据进行压缩
        minRatio: 0.6 // 压缩比例,值为0 ~ 1
      })
    );
  }
}

3 打包
打包后dist目录下会出现压缩过的文件
在这里插入图片描述

部署

部署服务器这里用的是nginx
把dist下的资源部署上去后访问服务器后访问会发现服务器当然没有返回压缩过的版本
在这里插入图片描述

此时需要在nginx配置文件下添加一行配置,配置完了后会自动去寻找压缩过的资源

gzip_static on;

结果:
在这里插入图片描述

拿到了压缩过后的版本了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
首先,你需要在你的 Umi 项目中安装 UEditor 的依赖包。然后,你需要在你的 Umi 配置文件中添加 UEditor 的相关配置,以便能够在你的项目中使用它。 以下是具体步骤: 1. 安装依赖包 在你的 Umi 项目根目录下运行以下命令,安装 UEditor 的依赖包: ``` npm install ueditor --save ``` 2. 添加 UEditor 的配置文件 在你的 Umi 项目中,你可以将 UEditor 的配置文件放在 `public` 目录下,例如 `public/ueditor.config.js`,然后在该文件中添加以下内容: ```javascript window.UEDITOR_CONFIG = { UEDITOR_HOME_URL: '/static/ueditor/', serverUrl: '/api/ueditor/', toolbars: [ [ 'source', '|', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'strikethrough', '|', 'forecolor', 'backcolor', '|', 'insertorderedlist', 'insertunorderedlist', '|', 'superscript', 'subscript', 'blockquote', 'pasteplain', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'link', 'unlink', 'anchor', '|', 'insertimage', 'emotion', 'scrawl', 'insertvideo', '|', 'fullscreen', 'autotypeset', 'removeformat', 'formatmatch', '|', 'inserttable', 'deletetable', 'insertparagraphbeforetable', '|', 'help' ] ], autoHeightEnabled: true, autoFloatEnabled: true, initialFrameHeight: 320, initialFrameWidth: 640, zIndex: 20000 }; ``` 该配置文件中包含了 UEditor 的一些基本配置,比如编辑器的宽度和高度、工具栏的按钮、是否自动浮动等。 3. 在 Umi 配置文件中添加相关配置 在你的 Umi 配置文件中,你需要添加以下配置: ```javascript export default { // ... chainWebpack(config) { config.module .rule('ueditor') .test(/\.js$/) .include.add(path.join(__dirname, 'node_modules/ueditor')) .end() .use('babel') .loader('babel-loader') .end() .use('ueditor') .loader('ueditor-loader') .options({ ueditorPath: '/static/ueditor/', ueditorConfigPath: '/ueditor.config.js' }) .end() }, // ... }; ``` 该配置中,我们首先通过 `chainWebpack` 方法扩展了 Webpack 的配置。然后,我们添加了一个名为 `ueditor` 的模块规则,该规则用于加载 UEditor 的代码。在该规则中,我们使用了 `ueditor-loader` 来加载 UEditor 的代码,同时还传递了一些参数,比如 UEditor 的路径和配置文件的路径。 4. 在页面中使用 UEditor 在你的页面中,你可以通过以下方式来使用 UEditor: ```javascript import React, { useRef, useEffect } from 'react'; import Editor from 'ueditor-react'; import 'ueditor-react/dist/ueditor.css'; const MyEditor = () => { const ueditorRef = useRef(null); useEffect(() => { if (ueditorRef.current) { ueditorRef.current.focus() } }, []); return ( <Editor ref={ueditorRef} ueditorPath="/static/ueditor/" configPath="/ueditor.config.js" onChange={console.log} /> ); }; export default MyEditor; ``` 在上面的代码中,我们使用了 `ueditor-react` 这个库来集成 UEditor。通过 `ueditorRef` 引用,我们可以在 `useEffect` 钩子中调用 `focus` 方法来让编辑器获取焦点。在 `Editor` 组件中,我们传递了一些参数,比如 UEditor 的路径和配置文件的路径,以及 `onChange` 回调函数。 希望以上内容对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值