vite-plugin / vite-plugin-dynamic-import

Enhance Vite builtin dynamic import
https://www.npmjs.com/package/vite-plugin-dynamic-import
MIT License
193 stars 11 forks source link

Ts import type incorrect I am not sure what cause #21

Closed zq0904 closed 2 years ago

zq0904 commented 2 years ago
  // package.json
  "vite": "^3.0.0",
  "vite-plugin-dynamic-import": "^1.0.0",
// vite.config.ts
import dynamicImport from 'vite-plugin-dynamic-import'
plugins: [
    // @ts-expect-error
    dynamicImport.default(), // Does not match the document, and the TS prompt is incorrect
]

I don't know. Possible cause?

  1. Is it just the wrong type of export?
  2. allowSyntheticDefaultImports: true // Has nothing to do with this configuration
  3. package.json "type": "module", // Does it matter?
caoxiemeihao commented 2 years ago

ESM import

// vite.config.ts
import dynamicImport from 'vite-plugin-dynamic-import'
plugins: [
    // @ts-expect-error
-    dynamicImport.default(), // Does not match the document, and the TS prompt is incorrect
+    dynamicImport(),
]

CJS require

// vite.config.ts
const dynamicImport = require('vite-plugin-dynamic-import').default;

plugins: [
    dynamicImport(),
]
zq0904 commented 2 years ago

ESM 导入

// vite.config.ts
import dynamicImport from 'vite-plugin-dynamic-import'
plugins: [
    // @ts-expect-error
-    dynamicImport.default(), // Does not match the document, and the TS prompt is incorrect
+    dynamicImport(),
]

CJS 要求

// vite.config.ts
const dynamicImport = require('vite-plugin-dynamic-import').default;

plugins: [
    dynamicImport(),
]

An error is reported when esm is introduced in this version // package.json "Vite" : "^ 3.0.0", "Vite - plugin - dynamic - import" : "^ 1.0.0",

dynamicImport is not a function

That's why I had to use it // @ts-expect-error dynamicImport.default(),