Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

als-require

Package Overview
Dependencies
Maintainers
0
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

als-require

A utility for using CommonJS require in the browser and creating bundles.

  • 0.4.0
  • npm
  • Socket score

Version published
Weekly downloads
25
increased by150%
Maintainers
0
Weekly downloads
 
Created
Source

als-require

als-require is a lightweight utility that enables the importation and modification of CommonJS modules before execution in both browser and Node.js environments.

Capabilities of als-require:

  • Utilize CommonJS modules in the browser, complete with all dependencies and access to module functionalities.
  • Employ a unified context object across all modules.
  • Modify the code of each module before it is executed.

Where it can be particularly useful:

  • Enables the use of the same codebase on both server and browser, facilitating seamless integration.
  • Allows for on-the-fly code rendering without the need for building a separate bundle.

Installation

To install als-require, use npm:

npm install als-require

Importing

Import in nodejs:

const Require = require('als-require')
const module = Require.getModule('./some/path')

Import in browser:

<script src="/node_modules/als-require/require.js"></script>
<script>
   Require.getModule('./some/path')
   .then(module => {
      // ...
   })
</script>

Usage

als-require has two files for NodeJS and browser which has same structure and api. Each file includes Require class with folowing structure:

class Require {
   static getModule(path, context) {}
   static contents = {}
   constructor(path, context = {}) {
      this.modules = {}
      this.contents = {}
      this.path = path
      this.context = context
   }
   getContent(path = this.path, from = Require.relativePath) {}
   build(modules = this.modules) {}
}

The structure above describes only properties and methods for usage, without additional properties and methods which used as private methods.

The constructor gets two arguments: path and context.

  • path (String): relative path to module for require
  • context (Object): shared object which will be available in all modules

Here explanation what each method and property used for:

  • Require.getModule - quick way to get contents and build them in one step
    • returns new instance of Requires with ready contents, modules and result
    • The method is sync for NodeJS and async for browser
  • Require.contents - includes modules contents and their children list and used as cache
  • require.getContent() - used for reading module file's contents
    • Adding content to Require.contents and to require.contents
    • The browser version is async and NodeJS version is sync
  • require.build() - builds all modules results
    • require.modules - includes all module's results
    • require.result - has the main module result (export)

Node.Js

const Require = require('als-require')
const path = './relative/path/to/module'
const context = {} // shared object for all modules empty object by default
const mod = new Require(path,context)
mod.getContent() // reading all modules
for(const path in mod.contents) {
   mod.contents[path] = mod.contents[path] // modify if needed
}

mod.build() // build the result
mod.result // includes the result of main module
mod.modules // object with all module`s results {relativePath:moduleResult,...}

If you want to use node modules libraries, you need to specify relative path.

Browser

<script src="/node_modules/als-require/require.js"></script>
<script>
   const path = './relative/path/to/module'
   const context = {} // shared object for all modules empty object by default
   const mod = new Require(path,context)
   const promise = mod.getContent() // fetching all modules
   promise.then(mod => {
      for(const path in mod.contents) {
         mod.contents[path] = mod.contents[path] // modify if needed
      }
   
      mod.build() // build the result
      mod.result // includes the result of main module
      mod.modules // object with all module`s results {relativePath:moduleResult,...}
   })
</script>

Permitted module path

Require class not requiring node_modules packages. All node packages returns null as result. You can include node module by requiring it's full path. Here is example:

const somePackage = require('some-package');
const somePackage1 = require('./node_modules/some-package/index.js');

module.exports = {somePackage,somePackage1}

In case above somePackage is null but somePackage1 should be the package.

Context Usage

The context object is a shared space accessible by all modules loaded by als-require. This allows modules to read and write shared data, enabling more interactive and dynamic module behaviors.

Make sure you are using the context for static value (like constants and common variables and functions) and not dynamic, cause it available to all require's results.

Error Handling

als-require throwing errors in case of cyclic dependencies and if some module throwing error.

For the case of module's error, Require adds to stack modules paths which has called.

Keywords

FAQs

Package last updated on 26 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc