Inconsistent import Behavior for Libraries with or without Optimization.
Sometimes Lodash cannot be imported properly. ( I havent tested it with other Libaries atm)
There are multiple ways to reproduce this Bug:
use "import split from 'lodash/split'
[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"
SyntaxError: import not found: default
Fix:
import split from 'lodash'
2.
optimizeDeps: {
exclude: [
'lodash'
]
}
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
"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
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
Most helpful comment
You should be using lodash-es and then
import { split } from 'lodash-es'