javascript - (!) 未使用的外部导入。 'reduce' 从外部模块 'lodash' 导入但从未使用过

标签 javascript import lodash node-modules rollupjs

我正在使用 rollup 构建我的,并且我依赖于 lodash

但是当我运行 rollup 来捆绑我的代码 时,我收到了这个警告。

(!) Unused external imports
reduce imported from external module 'lodash' but never used

我的代码示例如下:

import { reduce } from "lodash"

export function someutilityfunction(args) {
    return reduce(args,() => {
        // do somthing
    }, {}) // A generic use case of reduce function
}

捆绑工作正常。

我什至尝试过使用

import * as _ from "lodash"

lodash-es 而不是 lodash

但没有成功

这是我的rollup.config.js

import resolve      from 'rollup-plugin-node-resolve'
import babel        from 'rollup-plugin-babel'
import filesize     from 'rollup-plugin-filesize'
import typescript   from 'rollup-plugin-typescript2'
import commonjs     from 'rollup-plugin-commonjs'
import uglify       from 'rollup-plugin-uglify'

let production = (process.env.NODE_ENV == "production")

export default {
    input: 'src/index.ts',
    output: {
        file: 'lib/index.js',
        format: 'cjs',
        name: 'my-library',
        sourcemap: true
    },
    external: [
        'rxjs',
        'axios',
        'lodash'
    ],
    plugins: [
        resolve(),
        typescript({
            tsconfigOverride: {
                compilerOptions: {
                    declaration: true,
                    moduleResolution: "node",
                    allowSyntheticDefaultImports: true
                }
            },
            // verbosity: 3,
            clean: true,
            rollupCommonJSResolveHack: true,
            abortOnError: false,
            typescript: require('typescript'),
        }),
        commonjs(),   
        babel({
            exclude: 'node_modules/**'
        }),
        production && uglify(),
        filesize()
    ],
    watch: {
        include: 'src/**'
    }
  };

我以前使用过这个 汇总配置,直到现在它运行良好。

我错过了什么吗?

而且我知道问题的标题可以更通用。随时改进帖子。

最佳答案

看起来 Rollup 和 Lodash 中的 tree-shaking 存在一个已知问题(D3 也有类似的问题):

https://github.com/rollup/rollup/issues/691

关于javascript - (!) 未使用的外部导入。 'reduce' 从外部模块 'lodash' 导入但从未使用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48821400/

相关文章:

javascript - 本地存储读取次数过多

用于测量浏览器性能以打开或关闭动画的 Javascript

javascript - NPM模块导入时抛出错误

javascript - _.invoke 方法在 Lodash 中如何工作?

javascript - 提取具有最大属性值的数组元素

javascript - 如何使用$.ajax从API接收JSON

javascript - 如何使用 jquery 和 animate.css 切换最大化/最小化?

css - CSS 中@import 和 link 的区别

Python 导入错误 : 'module' object has no attribute 'x'

javascript - Lodash 的 _.debounce() 在 Vue.js 中不起作用