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