npm audit镜像

修补命令的作用

  1. # 扫描项目漏洞把不安全的依赖项自动更新到兼容性版本
  2. npm audit fix
  3. # 在不修改 node_modules 的情况下执行 audit fix,仍然会更改 pkglock
  4. npm audit fix --package-lock-only
  5. # 跳过更新 devDependencies
  6. npm audit fix --only=prod
  7. # 强制执行 audit fix 安装最新的依赖项(toplevel)
  8. npm audit fix --force
  9. # 单纯的获取 audit fix 会做的事,并以 json 格式输出。
  10. npm audit fix --dry-run --json
  11. # 获取详情
  12. npm audit
  13. # 以 JSON 格式打印报告
  14. npm audit --json

.umirc.js配置

www.cnblogs.com:添加配置式路由——添加路由信息到路由表

  1. .umirc.js // umi 配置,同 config/config.js,二选一

#umi配置细节

在.umi.js里配置:

  1. export default {
  2. plugins: [
  3. ['umi-plugin-react', {
  4. dva: {
  5. immer: true,
  6. },
  7. antd: true,
  8. routes: {
  9. exclude: [/models\//],
  10. },
  11. polyfills: ['ie9'],
  12. locale: {},
  13. library: 'react',
  14. dynamicImport: {
  15. webpackChunkName: true,
  16. loadingComponent: './components/Loading.js',
  17. },
  18. dll: {
  19. exclude: [],
  20. },
  21. hardSource: true,
  22. pwa: true,
  23. hd: true,
  24. fastClick: true,
  25. title: 'default title',
  26. chunks: ['vendor', 'umi'],
  27. scripts: [
  28. { src: 'http://cdn/a.js' },
  29. { src: '<%= PUBLIC_PATH %>a.js' },
  30. { content: `alert('a');` },
  31. ],
  32. headScripts: [],
  33. metas: [
  34. { charset: 'utf-8' },
  35. ],
  36. links: [
  37. { rel: 'stylesheet', href: 'http://cdn/a.css' },
  38. ],
  39. }],
  40. ],
  41. };

#配置项

所有功能默认关闭,有真值配置才会开启。

#dva
  • 类型:Object

基于 umi-plugin-dva 实现,功能详见 和 dva 一起用
配置项包含:

  • immer,是否启用 dva-immer
  • dynamicImport,是否启用按需加载,配置项同 #dynamicImport,并且如果在 #dynamicImport 有配置,配置项会继承到 dva 中
  • hmr是否启用 dva 的 hmr

WARNING
如果项目中有 dva 依赖,则优先使用项目中的依赖。

#antd
  • 类型:Boolean

启用后自动配置 babel-plugin-import 实现 antd, antd-mobile 和 antd-pro 的按需编译,并且内置 antd, antd-mobile 依赖,无需手动在项目中安装。
WARNING
如果项目中有 antd 或者 antd-mobile 依赖,则优先使用项目中的依赖。

#routes
  • 类型:Object

基于 umi-plugin-routes 实现,用于批量修改路由。
配置项包含:

  • exclude,值为Array(RegExp),用于忽略某些路由,比如使用 dva 后,通常需要忽略 models、components、services 等目录
  • upgrate, 值为Function,用于更新路由

    #polyfills (已废弃)
  • 类型:Array(String)

    请改用 config.targets

基于 umi-plugin-polyfills 实现,用于加各种补丁。
目前支持配置[‘ie9’]、[‘ie10’]、[‘ie11’] ,实现一键兼容。

#locale
  • 类型:Object

基于 umi-plugin-localereact-intl 实现,用于解决 i18n 问题。
配置项包含:

  • default :’zh-CN’,// default zh-CN
  • baseNavigator:true// default true, when it is true, will use navigator.language overwrite default
  • antd:true // use antd, default is true

    #library
  • 类型:String

可能切换底层库为 preact 或 react。

#dynamicImport
  • 类型:Object

实现路由级的动态加载(code splitting),可按需指定哪一级的按需加载。
配置项包含:

  • webpackChunkName,是否通过 webpackChunkName 实现有意义的异步文件名
  • loadingComponent,指定加载时的组件路径
  • level,指定按需加载的路由等级

    #dll
  • 类型:Object

通过 webpack 的 dll 插件预打包一份 dll 文件来达到二次启动提速的目的。
配置项包含:

  • include
  • exclude

    #hardSource
  • 类型:Boolean

通过 hard-source-webpack-plugin 开启 webpack 缓存,二次启动时间减少 80%。推荐非 windows 电脑使用,windows 下由于大文件 IO 比较慢,可自行决定是否启用。

#pwa
  • 类型:Object

开启 PWA 相关功能,包括:

  • 生成manifest.json,对于 WebManifest 中引用的icons图标,建议放在项目根目录public/文件夹下,最终会被直接拷贝到构建目录中
  • 在PRODUCTION 模式下生成 Service Worker

配置项包含:

  • manifestOption类型:Object,包含如下属性:
    • srcPath manifest 的文件路径,类型:String,默认值为src/mainfest.json(如果src不存在,为项目根目录)
  • workboxPluginMode Workbox 模式,类型:String,默认值为GeneraSW即生成全新 Service Worker ;也可选填injectManifest即向已有 Service Worker 注入代码,适合需要配置复杂缓存规则的场景
  • qorkboxOption Workbox配置对象,其中部分重要属性如下:
    • swSrc类型:String,默认值为src/manifest.json ,只有选择了InjectManisest 模式才需要配置
    • swDest类型:String,最终输出的文件名,默认值为service-worker.js 或者等于swSrc中的文件名
    • importWorkboxFrom类型:String,默认从 Google CDN 加载 Workbox 代码,可选值’local’ 适合国内无法访问的环境

更多关于 Workbox 的使用可以参考官方文档
一个完整示例如下:

  1. // .umirc.js or config/config.js
  2. export default {
  3. pwa: {
  4. manifestOptions: {
  5. srcPath: 'path/to/manifest.webmanifest'
  6. },
  7. workboxPluginMode: 'InjectManifest',
  8. workboxOptions: {
  9. importWorkboxFrom: 'local',
  10. swSrc: 'path/to/service-worker.js',
  11. swDest: 'my-dest-sw.js'
  12. }
  13. }
  14. }

当 Service Worker 发生更新以及网络断开时,会触发相应的 CustomEvent。 例如当 Service Worker 完成更新时,通常应用会引导用户手动刷新页面,在组件中可以监听 sw.updated 事件:

  1. window.addEventListener('sw.updated', () => {
  2. // 弹出提示,引导用户刷新页面
  3. });

另外,当网络环境发生改变时,也可以给予用户显式反馈:

  1. window.addEventListener('sw.offline', () => {
  2. // 置灰某些组件
  3. });

#hd
  • 类型:Boolean

开启高清方案。

#fastClick
  • 类型:Boolean

启用 fastClick。

#title
  • 类型:String/Object

开启 title 插件,设置 HTML title:
配置项包含:

  • defaultTitle:‘默认标题’,// 必填,当配置项为 String 时直接配置项作为 defaultTitle
  • format:’{parent}{separator}{current]’ ,// default {parent}{separator}{current}, title format
  • separator, // default ‘ - ‘
  • useLocale:true,// default false,是否使用locale做多语言支持。如果选true,则会通过配置的title属性去找locales/*.js找对应文字

当 title 插件开启后你可以在 routes 配置或者 pages 下的页面组件中配置 title。
比如使用配置式路由的时候如下配置:

  1. // .umirc.js or config/config.js
  2. export default {
  3. routes: [{
  4. path: '/testpage',
  5. component: './testpage',
  6. title: 'test page',
  7. }],
  8. }

使用约定式路由的时候则直接在页面组件中配置:

  1. /**
  2. * title: test page
  3. */
  4. export default () => {
  5. return <div>testpage</div>;
  6. }

在约定式路由里,注释必须写在文件头,否则将不被识别

#自定义模板document.ejs

如果你使用了自定的src/pages/document.ejs,你需要在里面加入入,以确保title.defaultTitle能正常被注入到生成的index.html的里

#chunks
  • 类型:Array(String)

默认是 [‘umi’],可修改,比如做了 vendors 依赖提取之后,会需要在 umi.js 之前加载 vendors.js
比如如下配置:

  1. // .umirc.js or config/config.js
  2. export default {
  3. chainWebpack: function (config, { webpack }) {
  4. config.merge({
  5. optimization: {
  6. minimize: true,
  7. splitChunks: {
  8. chunks: 'all',
  9. minSize: 30000,
  10. minChunks: 3,
  11. automaticNameDelimiter: '.',
  12. cacheGroups: {
  13. vendor: {
  14. name: 'vendors',
  15. test({ resource }) {
  16. return /[\\/]node_modules[\\/]/.test(resource);
  17. },
  18. priority: 10,
  19. },
  20. },
  21. },
  22. }
  23. });
  24. },
  25. plugins: [
  26. ['umi-plugin-react', {
  27. chunks: ['vendors', 'umi']
  28. }]
  29. }

#scripts
  • 类型:Array(Object)

放在里,在 umi.js 之后,可使用 <%= PUBLIC_PATH %> 指向 publicPath

#headScripts
  • 类型:Array(Object)

放在 里,在 umi.js 之前,可使用 <%= PUBLIC_PATH %> 指向 publicPath

#metas
  • 类型:Array(Object)

    #links
  • 类型:Array(Object)

可使用 <%= PUBLIC_PATH %> 指向 publicPath

connent导致未定义

TypeError: Cannot read property ‘routerData’ of undefined
由于content连接是modal和component,程序中没有modal,编译找不到!!!
添加了modal,编译成功

  1. export default connect(({ menu: menuModel }) => ({
  2. routerData: menuModel.routerData,
  3. }))(AuthComponent);

umi整体布局混乱

antd是前端组件库,由于在.umirc.js中设置为false,未能引用组件,造成的布局混乱—-antd:true

  1. export default {
  2. treeShaking: true,
  3. plugins: [
  4. // ref: https://umijs.org/plugin/umi-plugin-react.html
  5. ['umi-plugin-react', {
  6. antd: true,
  7. dva: true,
  8. dynamicImport: false,
  9. title: 'proumi',
  10. dll: false,
  11. routes: {
  12. exclude: [
  13. /components\//,
  14. ],
  15. },
  16. }],
  17. ],}

LocalProvide国际化

ant.design/components/locale-provider-cn:LocalProvide国际化

locales国际化模版引用

umijs.org/plugin/umi-plugin-react:国际化模版引用
问题1、UI页面无法将 id正常显示为中文
问题2、引用的Dashboard无法显示
在locales中找到了两个的内容,尝试如何引用却失败
umijs.org/plugin/umi-plugin-react:国际化模版中找到了答案,引用了locales,还是无法显示,然后将dva修改后,成功了

  1. // ref: https://umijs.org/config/
  2. export default {
  3. treeShaking: true,
  4. plugins: [
  5. // ref: https://umijs.org/plugin/umi-plugin-react.html dva:true dynamixImport:true
  6. ['umi-plugin-react', {
  7. antd: true,
  8. dva: {
  9. immer: true,
  10. },
  11. dynamicImport: {
  12. webpackChunkName: true,
  13. loadingComponent: './components/Loading.js',
  14. },
  15. title: 'proumi',
  16. dll: {
  17. exclude: [],
  18. },
  19. locale: {},
  20. library: 'react',
  21. hardSource: true,
  22. pwa: true,
  23. hd: true,
  24. fastClick: true,
  25. title: 'default title',
  26. routes: {
  27. exclude: [
  28. /components\//,
  29. ],
  30. },
  31. }],
  32. ],
  33. }

hd:高清显示

umijs.org/plugin/umi-plugin-react:开启高清
楼上的国际化模版引用成功,但是比较大,是因为使用了hd,删除后得到成功,
(疑问:尝试了 hd:false 还是编译错误)
疑问解决:和删除结果一样

.concat传值为空

.concat是fotmatMessage的方法,fotmatMessage是在locale中引用的,在.umirc.js或config.js中引用locale得到了解决

vsCode设置问题

jianshu.com/p:VSCode中”experimentalDecorators”设置无效
.jianshu.com/p/:VS Code experimentalDecorators 问题