Calling a method inside an IIFE exported method

474 views Asked by At

i have a script which i want to expose a function using JS Modules this script is compiled using esbuild.

pre-transpiled file (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 task

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

exported code (i notice the function is inside an IIFE) enter image description here

code in the html:

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

error:

NakamaJS.InitNakama is not a function

the source of my project is here

1

There are 1 answers

0
constexpr On BEST ANSWER

The output format defaults to --format=iife but you can use --format=esm to output in ECMAScript module format instead, which will work with import. Docs are here: https://esbuild.github.io/api/#format-esm.