All Projects → brrd → electron-require

brrd / electron-require

Licence: MIT license
Simplified require in electron applications

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to electron-require

tradeship
Automatically imports missing JavaScript dependencies and removes unused ones.
Stars: ✭ 42 (+61.54%)
Mutual labels:  require
requireg
Resolve and require local & global modules in node.js like a boss
Stars: ✭ 45 (+73.08%)
Mutual labels:  require
pkg-require
require node files relative to your package directory
Stars: ✭ 22 (-15.38%)
Mutual labels:  require
lint-deps
Lint for unused or missing dependencies in your node.js projects. Customize with plugins or configuration.
Stars: ✭ 48 (+84.62%)
Mutual labels:  require
node-advanced
Node Advanced Courseware
Stars: ✭ 80 (+207.69%)
Mutual labels:  require
require-polyfill
Make `require` work in browsers, at runtime. No code bundling needed!
Stars: ✭ 37 (+42.31%)
Mutual labels:  require
konan
find all require/import calls by walking the AST
Stars: ✭ 48 (+84.62%)
Mutual labels:  require
WDS-Required-Plugins
Make certain plugins required so that they cannot be (easily) deactivated. | Gatekeeper: @aubreypwd
Stars: ✭ 77 (+196.15%)
Mutual labels:  require
module-invalidate
Invalidate node.js modules loaded through require()
Stars: ✭ 19 (-26.92%)
Mutual labels:  require
lazy-cache
Cache requires to be lazy-loaded when needed. Uses node's own require system with tried and true, plain-vanilla JavaScript getters.
Stars: ✭ 51 (+96.15%)
Mutual labels:  require

electron-require

Simplified require in electron applications

electron-require is a super basic, no dependency convenience wrapper around require for your electron applications. You can use it to avoid using complex require paths in your application.

Installation

$ npm install --save electron-require

Then include it in your code:

const rq = require('electron-require');

Usage

rq()

rq('./module.js') imports module.js from the current process directory (it is actually an alias to require.main.require('./module.js')).

rq.electron()

rq.electron('module') is the same than require('electron')['module'], except that it resolves into require('electron').remote['module'] when module is not found, if used in the renderer process.

rq.remote()

rq.remote('module') is the same than require('electron').remote.require('module'), except that it resolves into rq.main('module') when used in the main process.

Aliases

You can add your own custom alias with rq.set(key, path).

Once rq.set('myAlias', 'my/path') is called, rq.myAlias('./module.js') will try to load my/path/module.js.

Example 1:

rq.set('local', 'local');

// Import [application root]/local/my-local-module.js into myLocalModule
const myLocalModule = rq.local('./my-local-module.js');

Example 2:

let userData = electron.app.getPath('userData');
rq.set('plugin', userData + '/plugins');

// Import [userdata]/plugins/my-plugin.js into myPlugin
const myPlugin = rq.plugin('/my-plugin.js');

Template strings

You can use template string in the path passed to .set():

  • %{app} resolves to app.getAppPath()
  • %{anyOtherName} resolves to app.getPath('anyOtherName')

So we can write example 2 in a simpler way:

rq.set('plugin', '%{userData}/plugins');

// Import [userdata]/plugins/my-plugin.js into myPlugin
const myPlugin = rq.plugin('/my-plugin.js');

Read more about this in the app module documentation

Multiple alias

rq.set can also be used with an object:

rq.set({
	'local': 'local',
    'plugin': '%{userData}/plugins'
});

Custom aliases defined in package.json

In most cases you will want to use the same custom aliases for the whole project. You can define custom aliases by adding an electron-require key to your app package.json file:

"electron-require": {
    "first": "path/to/first/alias",
	"second": "path/to/second/alias"
}

Default aliases

Default aliases are the following:

{
    "root": "",
    "renderer": "app/renderer",
    "main": "app/main",
    "browser": "app/main"
}

It actually assumes that your app is organized in the following way:

.
├── app
│   ├── main
│   │   └── [main process modules]
│   └── renderer
│       └── [renderer process modules]
└── package.json

But you can of course override theses default values by using rq.set() or by adding an electron-require entry in your package.json.

License

The MIT License (MIT)
Copyright (c) 2016 Thomas Brouard

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].