在 IIFE 导出的方法中调用方法

Calling a method inside an IIFE exported method

我有一个脚本,我想使用 JS 模块公开一个函数这个脚本是使用 esbuild 编译的。

预编译文件(index.mjs)

const NakamaWrapper = require("./nakama").default

var NakamaJS;

export default function InitNakama(host, port, useSSL) {
    NakamaJS = new NakamaWrapper(host, port, useSSL);
    NakamaJS.initiate();
}

esbuild 任务

"build-dev": "./node_modules/.bin/esbuild ./src/index.mjs --bundle --sourcemap --target=es2015 --outfile=./dist/dev/pc-nakama.js",

导出代码(我注意到函数在 IIFE 中)

html中的代码:

import * as NakamaJS from "./pc-nakama.js"; 
NakamaJS.InitNakama("192.168.100.50", 7350, false);

错误:

NakamaJS.InitNakama is not a function

the source of my project is here

输出格式默认为 --format=iife,但您可以使用 --format=esm 以 ECMAScript 模块格式输出,这将适用于 import。文档在这里:https://esbuild.github.io/api/#format-esm.