Giter Club home page Giter Club logo

electron-typescript-native's Introduction

A native Electron module using Typescript

Summary

Install and run on Windows

Install NodeJS.

From an elevated PowerShell or CMD.exe (run as Administrator), run

$ npm install --global --production windows-build-tools

to install Python and Visual C++ Build Environment.

After that, enter in the project folder and launch

$ cd native_module && npm install && npm run build
$ cd .. && cd electron_sample && npm install && npm run build
$ npm run start

After the start task, the native string world will appear inside the DOM of an Electron window.

Documentation

The application is split in two parts:

  • electron_sample: a sample application running in Electron using a Typescript renderer.
  • native_module: a sample Typescript module that integrates a native feature.

In order to display the sample string generated by the module in the Electron application, we need to inject it into the application itself.

electron_sample application

The application main treat is to generate an Electron application and to include the native_module.

The assets file are already moved in the dist folder and webpack is configured to build the bundle inside that folder.

The application package.json just includes the local path in his dependencies.

"dependencies": {
    "native_module": "file:../native_module"
}

devDependencies are included to compile and run correctly the application.

In our scripts we have:

  • start task, to launch the application.
  • postinstall task, to rebuild Electron including the native module.
  • build task, to compile and pack Typescript and Javascript files.

native_module module

First of all, let's consider the tsconfig.json file. Using:

"declaration": true,
"outDir": "dist"

the Typescript compiler will use dist as the build directory and will generate declarations.

In the package.json

"main": "dist/index.js",
"types": "dist/index.d.ts"

indicates the correct path for the main file and typings.

The scripts used in this module are

"build": "npm run build-native && npm run tsc",
"tsc": "tsc",
"build-native": "node-gyp rebuild"
  • tsc: compiles the Typescript source code.
  • build-native: compiles the C++ code.
  • build: executes both.

In order to develop using a native library, we need nan and node-gyp. @types/node and typescript are useful to compile Typescript.

"dependencies": {
    "nan": "^2.14.0"
},
"devDependencies": {
    "@types/node": "^12.0.0",
    "node-gyp": "^4.0.0",
    "typescript": "^3.4.5"
}

In binding.gyp we include nan binaries

"include_dirs": [
    "<!(node -e \"require('nan')\")"
]

In Typescript index.ts is wrapped the exported hello function used in the Electron application.

electron-typescript-native's People

Contributors

ruj89 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.