Zentyk
Zentyk

Reputation: 67

Calling a method inside an IIFE exported method

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

Upvotes: 0

Views: 518

Answers (1)

constexpr
constexpr

Reputation: 1041

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.

Upvotes: 2

Related Questions