kabiroberai / node-swift

Create Node modules in Swift
MIT License
492 stars 16 forks source link

Webpack loader plugin for importing .swift files directly #8

Open KishanBagaria opened 2 years ago

KishanBagaria commented 2 years ago

This would be a nice to have:

example.swift:

import Foundation
import NodeAPI

@main struct MyExample: NodeModule {
    let exports: NodeValueConvertible
    init() throws {
        exports = [
            "getTime": try NodeFunction { _ in
                Date().timeIntervalSince1970.rounded(.down)
            }
        ]
    }
}

index.js:

const { getTime } = require("./example.swift");
console.log(`Seconds since 1970: ${getTime()}`);
// Seconds since 1970: 1647672663

webpack.config.js:

module.exports = {
  module: {
    rules: [
      {
        test: /\.swift$/i,
        use: ["node-swift-loader"],
      },
    ],
  },
};