Search code examples
typescriptwebpack-2solace

Solace nodejs api integration with Webpack


I have an angular2 typescript webapp which is build with webpack. I am trying to integrate solclientjs into it, but I get the exception below

solclientjs-exports.js:34 Uncaught Error: Cannot find module "."
at webpackMissingModule (solclientjs-exports.js:34)
at load (solclientjs-exports.js:34)
at loadProduction (solclientjs-exports.js:44)
at new loader (solclientjs-exports.js:47)
at Object. (solclientjs-exports.js:72)
at webpack_require (bootstrap 79a17da…:52)
at Object. (home.component.ts:8)
at webpack_require (bootstrap 79a17da…:52)
at Object. (app.ts:17)
at webpack_require (bootstrap 79a17da…:52)

Can you please advise?


Solution

  • Using exports-loader will enable you to package the library with Webpack.

    Here's a sample webpack.config.js after exports-loader is used.

    const path = require('path');
    
    const solclientjs = path.resolve(__dirname, 'path/to/solclientjs-10.0.0/lib/solclient.js');
    
    module.exports = {
      resolve: {
        alias: {
          solclientjs$: solclientjs
        }
      },
      module: {
        rules: [
          {
            test: require.resolve(solclientjs),
            use: 'exports-loader?window.solace'
          }
        ]
      }
    }
    

    A couple of things to note:

    1. You need to use the browser version instead of the node version for Webpack. Packing the Node version of the library for use in the browser isn't supported and won't work.

      • The Node version uses a Node specific WebSocket library that won't work on browsers
      • The Node version doesn't include the full range of HTTP transports. For example, the browser version allows you to fallback to HTTP Comet transports. This is not available in the Node version.
      • The browser version includes browser-specific platform code to make things work better on specific browsers; none of this is present in the Node version.
    2. For compatibility reasons, the browser version of the library is shipped so that it can be directly included with a <script> tag in a file. For browsers, it's highly recommended that the library be loaded with it. Examples can be found here.