From f04d6e3b53df44e1cff77f3eca064d7e021c7fd6 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Thu, 21 Oct 2021 19:02:26 +0200 Subject: [PATCH] fix(kit): use native import for esm modules (#1327) --- packages/kit/src/module/install.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index 5f27b12d65..d0d7cf8dda 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -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 }