在 IIFE 导出方法中调用方法

问题描述

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

预编译文件 (index.mjs)

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

var NakamaJS;

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

esbuild 任务

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

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

enter image description here

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