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

common-js

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

common-js

module.exports and module.import for browsers too

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

CommonJS + module.import() build status

This module aim is to bring both CommonJS like module behavior on Web browsers, and a promise based module.import(path) to both browsers and NodeJS.

Yes, it resolves paths relatively to the current one!

Browser Example

<!doctype html>
<html>
  <script
    id="common-js"
    data-main="/js-browser/main.js"
    src="common.js"
  ></script>
</html>

Having a single top level script is all it takes to be able to load asynchronously any other file or module.

The main entry point /js-browser/main.js will resolve relative paths from /js-browser/ folder.

Its loaded modules will resolve their own paths from where they've been loaded, and so on. The same goes for NodeJS.

// /js-browser/main.js loading /js-browser/test.js
module.import('./test').then(function (test) {
  test('Hello CommonJS!');
  // will output:
  // Hello CommonJS!
  // from /js-browser/test.js
});

// the /js-browser/test.js content
module.exports = function (message) {
  alert(message + '\nfrom ' + module.filename);
};

Load multiple modules at once

Promise.all([
  module.import('./a'),
  module.import('//cdn.something.com/cool.js'),
  module.import('../sw.js'),
  module.import('/root/too.js')
]).then(function (modules) {
  const [a, cool, sw, too] = modules;
});

F.A.Q

  • Does it load every time? It uses a cache, like NodeJS does. If you load same module twice, even from different relative paths, it'll use the cached one.
  • Why on the module? There are scripts, script type module, importScripts, a dynamic import proposal, you name it ... this one actually works and it's backward compatible with modules that don't care about this solution existing.
  • Why not ES2015 modules? Because those, so far, never truly solved anything. Actually, ES6 modules created more problems due inability to require modules at runtime and/or on the browser.
  • Is there a CDN I can use to test? There is always one for npm modules. https://unpkg.com/common-js@latest would do.
  • Is this using eval? No, it's using Function, which is not the same, and it does exactly what a script loading your code would do. Actually, it does something better. It makes the module run on strict by default and it avoids global scope pollution, like a proper module system.

License

Copyright (C) 2017 by Andrea Giammarchi - @WebReflection

Keywords

FAQs

Package last updated on 20 Jan 2017

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