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.3.0
  • npm
  • Socket score

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

als-require

als-require is a user-friendly utility designed to facilitate the use of the require function in web browsers and to create bundles for CommonJS modules. It simplifies the process of module management in browser environments, allowing for seamless integration and deployment of CommonJS-based code.

Features

  • Dynamic Module Loading: Load JavaScript modules dynamically based on runtime conditions.
  • Cyclic Dependency Detection: Automatically detects and handles cyclic dependencies within modules.
  • Error Handling: Provides detailed error information for debugging loading issues.
  • Flexibility: Supports various module formats and can be easily integrated into different JavaScript environments.

Installation

To install als-require, use npm:

npm install als-require

Usage

als-require includes two main parts:

  1. Browser's script for handling modules
  2. Bundle builder for browser

Dynamic Module Loading in Browsers

The browser's script called getModule, is a function which fetching all module's chain and then laughing all the chain to get the result. The fetch is async, that's why getModule returns promise. All module's results, saved in getModule.modules object with relative path as a key and module result as a value.

Example:

Let's say, we have moduleA.js which requiring another modules and exporting variable someExport and we want to use this module in browser. Here is the code:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Dynamic Module Loading</title>
</head>
<body>
   <script src="node_modules/als-require/require.js"></script>
   <script>
      getModule('./moduleA.js').then(someExport => {
         window.someExport = someExport;
         console.log(getModule.modules) // will return object with all modules and their results
      });
   </script>
</body>
</html>

In example above, first we include als-require script which adding getModule function and then, get the module.

Creating and Using Bundles

In this scenario, als-require is used to generate a bundle that consolidates all the required modules into a single file. This bundle can then be used in the browser, reducing the number of HTTP requests and streamlining the module loading process.

Example for Generating a Bundle:

const Modules = require('als-require');
const modules = new Modules()
modules.require('./moduleA','moduleAVarname')
modules.require('./moduleB','moduleBVarname')

modules.scripts // the object with {relativePath:{exports,content}}

// Now you can save the script as file
require('fs').writeFileSync('test.js', modules.script);

// Or return it directly 
app.get('/bundle.js', (req, res) => {
   res.send(modules.script);
});

On Browser:

<script src="/bundle.js"></script>
<script>
   console.log(moduleAVarname,moduleBVarname)
   console.log(getModule.modules)
</script>

The bundle is self-sufficient and will include getModule with getModule.modules which will include all bundled modules.

Keywords

FAQs

Package last updated on 22 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