fix(kit): use native import for esm modules (#1327)

This commit is contained in:
pooya parsa 2021-10-21 19:02:26 +02:00 committed by GitHub
parent b332462b2a
commit f04d6e3b53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import { resolveModule, requireModule } from '../utils/cjs'
import { resolveModule, requireModule, importModule } from '../utils/cjs'
import { resolveAlias } from '../utils/resolve'
import type { LegacyNuxtModule, NuxtModule, ModuleMeta, ModuleInstallOptions, ModuleOptions, ModuleSrc } from '../types/module'
import type { Nuxt } from '../types/nuxt'
@ -32,7 +32,9 @@ export async function installModule (nuxt: Nuxt, installOpts: ModuleInstallOptio
let handler: LegacyNuxtModule
if (typeof src === 'string') {
const _src = resolveModule(resolveAlias(src, nuxt.options.alias), { paths: nuxt.options.modulesDir })
handler = requireModule(_src)
// TODO: also check with type: 'module' in closest `package.json`
const isESM = _src.endsWith('.mjs') || meta.isESM
handler = isESM ? await importModule(_src) : requireModule(_src)
if (!meta.name) {
meta.name = src
}