Vite: Inconsistent import Behavior when using Lodash

Created on 9 Jun 2020  路  2Comments  路  Source: vitejs/vite

Describe the bug

Inconsistent import Behavior for Libraries with or without Optimization.
Sometimes Lodash cannot be imported properly. ( I havent tested it with other Libaries atm)

Reproduction

There are multiple ways to reproduce this Bug:



    • Default Vite Config

use "import split from 'lodash/split'
  • terminal will output:
[vite] Avoid deep import "lodash/split" (imported by /@/main.js)   
because "lodash" has been pre-optimized by vite into a single file.
Prefer importing directly from the module entry:

  import { ... } from "lodash" 
  • console will output:
SyntaxError: import not found: default

Fix:

import split from 'lodash'
  • then its not tree shaked

2.

  • Disable Deep Optimization like this in the Config
    optimizeDeps: {
        exclude: [
            'lodash'
        ]
    }
  • Old Terminal Output will be gone
  • Console Output will be the same

This Issue doesnt occur in the vite.config.js itself, if lodash is imported there like in example 1.
If lodash-es is used instead it outputs the same as above from main.js,
But if it is used in vite.config.js, then this error occurs:

import baseToString from './_baseToString.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module

System Info

"lodash": "^4.17.15",
"lodash-es": "^4.17.15",
"vue": "^3.0.0-beta.14"
"vite": "^0.20.5",
"@vue/compiler-sfc": "^3.0.0-beta.14"

Windows 10 64 Bit 2004
Node: 12.16.1
NPM: 6.13.4

pending triage

Most helpful comment

You should be using lodash-es and then import { split } from 'lodash-es'

All 2 comments

You should be using lodash-es and then import { split } from 'lodash-es'

How should i import lodash in packages that are used both in the server and client?

I cannot import from lodash-es because nodejs still does not support ESM

I should not use lodash because of bigger bindle size

import from lodash/function was the best solution IMHO

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haikyuu picture haikyuu  路  3Comments

Hoverhuang-er picture Hoverhuang-er  路  3Comments

ashubham picture ashubham  路  3Comments

maxxcs picture maxxcs  路  3Comments

tbgse picture tbgse  路  4Comments