reactjs - NextJS中的摇晃树包括所有的node_modules包,即使没有全部使用它

标签 reactjs typescript npm lodash next.js

以下是Next JS中我的包装尺寸的屏幕截图。我要指出的是react-color下的node_modules组件。我以这种方式导入它们:
import { GithubPickerProps, GithubPicker, AlphaPicker } from 'react-color';
但是您会看到它包含了我不使用的所有东西,例如photoshop.jssketch.js等。

我如何不将我不使用的东西与摇树捆绑在一起?

我确实注意到import { debounce } from 'lodash';导入了所有lodash,但是import debounce from 'lodash/debounce';减少了200kB的封装大小。

enter image description here

最佳答案

为了使摇树工作正常,react-color应该在module中添加package.json属性,该属性将指向lib的esm版本。

由于没有它,您将需要直接导入。

前:

import React from 'react'
import SketchPicker  from 'react-color'

class Component extends React.Component {

  render() {
    return <SketchPicker />
  }
}

enter image description here

后:

import React from 'react'
import SketchPicker  from 'react-color/lib/Sketch'

class Component extends React.Component {

  render() {
    return <SketchPicker />
  }
}

enter image description here

关于reactjs - NextJS中的摇晃树包括所有的node_modules包,即使没有全部使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60700036/

相关文章:

javascript - 如何传入 Tesseract 选项进行页面分割?

javascript - 我如何使用 browserify 安装不在 npm 上的库?

javascript - 为什么传递给构造函数的数组与类不同步?

javascript - 类型错误 : Cannot read property 'name' of undefined react error

javascript - 安装 axios 后出现“找不到模块”babel 错误

node.js - 检查最新的 npm 包版本是否具有相同的主要版本

node.js - 我如何在 CasperJS 中需要 MongoDB?

reactjs - 如何从 reducer 返回一个数组?

javascript - 无法使用 typescript 创建 react 应用程序

angular - 订阅时不触发 rxjs pipeline tap/map