npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@evg656e/requirify

v0.0.10

Published

Node-style require for browser-like JavaScript environments

Downloads

12

Readme

requirify

Node-style require for browser-like and QML JavaScript environments.

Why?

Super fast (in terms of setup) prototyping. No need to build assets, no need to rebuild QML apps. When ready, fallback to Webpack (or whatever tool) builds.

Install

npm install @evg656e/requirify --save-dev

Usage

You can find some code examples in https://github.com/evg656e/broadcast-example and this project test folder.

Browsers with local files

Enable local file support:

  • Chrome: run chrome --allow-file-access-from-files
  • Firefox: go to about:config, set security.fileuri.strict_origin_policy to false (you will probably want to create separate profile for this)

Add link to requirify to HTML page:

<!--  Change paths appropriate to your project structure. -->
<script src="./node_modules/@evg656e/requirify/dist/require.js"></script>

Now you can access Node-like modules within your page:

var Button = require('./lib/button');

var btn = new Button('Go!');
btn.on('clicked', function(target) {
    console.log('Button clicked:', target.text);
});
btn.click();

Where lib/button.js is:

var Widget = require('./widget');

function Button(text, parent) {
    Widget.call(this, parent);
    this.text = text;
}

Button.prototype = Object.create(Widget.prototype);
Button.prototype.constructor = Button;

Button.prototype.click = function() {
    this.emit('clicked', this);
};

module.exports = Button;

And lib/widget.js is:

var EventEmitter = require('events'); // https://www.npmjs.com/package/events 

function Widget(parent) {
    EventEmitter.call(this);
    this.parent = parent;
    this.visible = false;
}

Widget.prototype = Object.create(EventEmitter.prototype);
Widget.prototype.constructor = Widget;

Widget.prototype.show = function() {
    if (!this.visible) {
        this.visible = !this.visible;
        this.emit('visibilityChanged', this);
    }
};

module.exports = Widget;

QML with qmlscene

Install Qt.

Open Qt Creator, choose New Project -> Qt Quick UI Prototype.

In Projects tree Add New... -> Qt -> JS File:

Qt.include('./node_modules/@evg656e/requirify/dist/require.js'); // change paths to appropriate for you project structure

var Button = require('./lib/button');

In QML file:

import QtQuick 2.8
import QtQuick.Window 2.2
import './lib.js' as Lib // <-- import JS resource, see http://doc.qt.io/qt-5/qtqml-javascript-imports.html

Window {
    id: window
    // ...
    Component.onCompleted: {
        var btn = new Lib.Button('Go!');
        btn.on('clicked', function(target) {
            console.log('Button clicked:', target.text);
        });
        btn.click();
        // you can access QML items from JS, e.g.:
        var btn2 = new Lib.Button('Click me', window); // <-- you can use QML Window properties and methods within JS code
    }
}

Node.js HTTP server

To make work require with server-side generated HTML pages, handle require-path header in your HTTP server:

const http = require('http');
const url = require('url');
const fs = require('fs');

http.createServer(function(req, res) {
    // add this block (don't use this in production!)
    const requirePath = req.headers['require-path'];
    if (requirePath) {
        res.writeHead(200);
        fs.createReadStream(requirePath).on('error', () => {
            res.writeHead(404)
            res.end()
        }).pipe(res);
    }
    // ...
}).listen(8080);

Now JS-files will be send from HTTP server (including modules from node_modules like events). See https://github.com/evg656e/broadcast-example for details.

ES6 and Babel

You can prototype with ES6 modules, using babel-standalone for Browsers and QML, and babel-register for Node.js.

Enable ES6 in HTML JS:

var Babel = require('./node_modules/babel-standalone');
require.extensions['.js'] = function(content) {
    return Babel.transform(content, { presets: ['es2015'] }).code;
};
var Foo = require('./lib/foo').default; // <- foo contains ES6 code and uses ES6 modules (import/export)

Enable ES6 in QML JS:

Qt.include('./node_modules/@evg656e/requirify/dist/require.js');
var Babel = require('./node_modules/@evg656e/requirify/dist/babel.qml'); // <- use patched version of babel-standalone from requirify, standard version won't work because of QML JS engine bugs
require.extensions['.js'] = function(content) {
   return Babel.transform(content, { presets: ['es2015'] }).code;
};
var Foo = require('./lib/foo').default;

Enable ES6 in Node:

require('babel-register');
const Foo = require('./lib/foo').default;

See https://babeljs.io/docs/usage/babel-register/ for details.