nim-loader

0.3.3 • Public • Published

nim-loader

A Webpack loader for the Nim language.

How do I use it?

First of all, make sure that you have Nim installed.
Then install nim-loader:

npm install nim-loader

Next, configure your webpack.config.js to use this loader:

module: {
  ...
  rules: [
    ...
    {
      test: /\.nim$/,
      use: [
        {
          loader: 'nim-loader',
          options: {
            flags: ['--nim-compiler-flags']
          }
        }
      ]
    }
    ...
  ]
  ...
}

Of course, options is optional. If you don't want advanced behavior you can use the terse syntax:

module: {
  ...
  rules: [
    ...
    {
      test: /\.nim$/,
      loader: 'nim-loader'
    }
    ...
  ]
  ...
}

Finally, import your Nim file from your Javascript code.

import nimModule from './path/to/nim/file/module.nim'
nimModules.exportedFunction("With arguments")

Caveats

While Nim has native concepts of exporting functions and modules, these do not translate to the Javascript compile target. Instead, you are encouraged to use CommonJS module exports. This requires that your Nim code "imports" the module object. Doing so looks like this:

import jsffi
var module {. importc, nodecl .}: JsObject
...
module.exports = exportObject

where exportObject is of type JsObject. Note that modules.exports is already initialized to an empty object, so you can also do something like:

...
module.exports.someFunction = functionImpl
module.exports.someValue = nimValue
...

to register your exports one-by-one.

Examples

If you're still confused, hopefully the examples can help.

Package Sidebar

Install

npm i nim-loader

Weekly Downloads

2

Version

0.3.3

License

MIT

Unpacked Size

5.05 kB

Total Files

5

Last publish

Collaborators

  • labguns