I'm trying to use SFS JS API in a project with Webpack and Typescript, however I don't know how to set it up correctly. I haven't found any NPM packages related to SFS (are you planning to create one?), so I tried to include the .js file manually. My config is as follows:
webpack.config.js
Code: Select all
module.exports = {
// ...
resolve: {
extensions: ['.ts', '.js'],
alias: {
// ...
sfs2x: path.join(__dirname, 'lib/smart-fox-server/sfs2x-api-1.7.6.js')
}
},
// ...
module: {
rules: [
// ...
{ test: /sfs2x-api-1\.7\.6\.js$/, loader: 'expose-loader?SFS2X' }
]
},
app.ts
Code: Select all
import 'sfs2x';
some.other.file.ts
Code: Select all
this.sfs = new SFS2X.SmartFox(config);
Unfortunately, when I run the code, there is an error:
Code: Select all
Uncaught TypeError: SFS2X.SmartFox is not a constructor
This is the result from inspecting the SFS2X global variable in console:
Code: Select all
function (arrayBuffer,byteOffset,endianness){this._byteOffset=byteOffset||0;if(arrayBuffer instanceof ArrayBuffer){this.buffer=arrayBuffer}else if(typeof arrayBuffer=="object"){this.dataView=arrayBuffer;if(byteOffset){this._byteOffset+=byteOffset}}else{this.buffer=new ArrayBuffer(arrayBuffer||1)}this.position=0;this.endianness=endianness==null?DataStream.LITTLE_ENDIAN:endianness}
If the rule in module.rules is commented out, the module doesn't load at all (SFS2X is undefined).