【问题标题】:(!) Unused external imports. 'reduce' imported from external module 'lodash' but never used(!)未使用的外部进口。 'reduce' 从外部模块 'lodash' 导入但从未使用过
【发布时间】:2018-07-27 01:05:48
【问题描述】:

我正在使用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 config,它一直运行良好,直到现在。

我错过了什么吗?

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

【问题讨论】:

    标签: javascript import lodash node-modules rollupjs


    【解决方案1】:

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

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

    【讨论】:

    • 我曾想过使用babel-plugin-lodash,但最终决定使用const { reduce } = require("lodash")。 @th3n3wguy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2021-03-07
    • 2018-10-17
    相关资源
    最近更新 更多