@theopenweb/import-require-js

1.0.0 • Public • Published

Description

Synchronous require using es6 imports.

Functionality

  • Allows for "require" while being synchronous. This matches how common js works in Node.js.
  • polyfilling module.exports and exports.
  • Uses es6 imports so can be used in non-built modern and no combining of import and require allowed environments.
  • Works on various URL formats: node_modules named, relative.

Usage

npm install @theopenweb/import-require-js

module:

// import require from "./index.js"
// import require from 'node_modules/@theopenweb/import-require-js/index.js'
import require from '@theopenweb/import-require-js'

script:

  • Remove export line at end of javascript file.
<script src="./index.js"></script>

Example

npx http-server ./
# Go to http://localhost:8080

Example

Other libraries, etc

Requirejs is often used for implementing common js require functionality in browsers. However, it is "async" only: Requirejs is async. Synchronous handling only works on already loaded modules.

Synchronous handling can be handled as shown in that link. This is similar to how it is implemented in this library.

Improvements

This library has only been made to support what is needed. There are some features that should be added but has not been yet. If you find something that should be added, please create a detailed issue.

Areas that could be impoved:

  • Allowing for async handling, especially in nested libraries.
  • Support for other library URL resolution(bower_component, etc.), local files, etc.
  • Not include dependencies? Currently mustache(used for example) and require(might become needed).

Regards.

Package Sidebar

Install

npm i @theopenweb/import-require-js

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

8.29 kB

Total Files

5

Last publish

Collaborators

  • theopenweb_jp