【问题标题】:webpack & typescript: partial lodash import with es5 as target not workingwebpack & typescript:以 es5 为目标的部分 lodash 导入不起作用
【发布时间】:2017-11-13 20:23:05
【问题描述】:

我试图让 lodash 的部分导入在 webpack / typescript / react 环境中工作,以减少包大小。我正在使用lodash@4.17.4webpack@2.4.1

我知道我不应该像这样导入 lodash,因为它会导入整个 lodash 库:

import * as lodash from 'lodash';
 lodash.clone({});

所以我尝试了这个:

import { clone, create } from 'lodash';

但这仍然会继续导入整个库,而不仅仅是我需要的函数(bundlesize 不会改变)。 所以我读了一下,看到了这个:

import clone = require('lodash/clone');

这确实有效,只导入了我需要的函数,并且我的 bundlesize 小了几百 KB。但不幸的是,打字稿给了我以下错误(仅在构建捆绑包时显示错误,在运行 watch / webDev-Server 时一切正常):

Import assignment cannot be used when targeting ECMAScript 2015 modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.

这是因为我的打字稿配置中有"target":"es5" 吗?如果是这样,我怎样才能以 es5 模块为目标实现部分导入?

我的打字稿配置如下所示:

{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es5",
    "jsx": "react",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "declaration": false,
    "noImplicitAny": false,
    "noImplicitReturns": false,
    "removeComments": true,
    "strictNullChecks": false,
    "outDir": "build",
    "lib": [
      "es6",
      "es7",
      "dom"
    ]
  },
  "exclude": [
    "dist",
    "build",
    "node_modules",
    "bower_components"
  ]
}

有什么想法吗?

编辑: 当我只做常规要求时:

const clone = require('lodash/clone');

一切都编译没有错误(监视和构建)。这是导入最新版本 lodash 的正确方法吗?好像错了

【问题讨论】:

  • 你用的是什么版本的lodash?
  • 我正在使用lodash@4.17.4

标签: typescript import webpack bundle


【解决方案1】:

那是我导入 lodash

import { clone } from 'lodash/clone'

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-08-27
  • 2016-12-24
  • 2017-04-13
  • 2018-01-06
  • 1970-01-01
  • 2017-07-22
  • 2021-08-23
  • 1970-01-01
相关资源
最近更新 更多