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

application-compiler

v1.0.2

Published

A Simple Compiler To Merge Multiple Files Into A Single File Application.

Downloads

5

Readme

Application Compiler: Single File Applications Re-Imagined

NPM version NPM downloads Language grade: JavaScript GitHub issues GitHub stars GitHub license

Motivation

This package aims to bring an "include" method to Node applications that performs similar to include implementations in other languages such as PHP. Unlike Node's built in module system, this compiler aims to allow for the usage of simple calls such as include('/routes/api/v1/login.js') in your code and produces a compiled javascript file with all include calls converted into their respective code simulating a direct include into global context.

Features

  • Simple-to-use API
  • Sub-File Support
  • Nested Infinite Include Loop Protection
  • Instantaneous Hot Reloading
  • Memory Efficient
  • Supports Windows, Linux & MacOS
  • Relative Error Traces

Installation

Application Compiler can be installed using node package manager (npm)

npm i application-compiler

Table Of Contents

Examples

Below are some examples making use of Compiler methods and properties.

Application Compiler With Automatic File Writing

const ApplicationCompiler = require('application-compiler');

// Create compiler instance for the root file which is index.js
const website_compiler = new ApplicationCompiler({
    file_path: './index.js'
});

// Initiate automatic file writing
website_compiler.write_to({
    path: './',
    file_name: 'compiled_exec.js', // Run this using a process manager such as PM2
    relative_errors: true, // We want custom error traces for faster debugging
});

// Bind an error logger
website_compiler.set_error_logger((path, error) => {
    // Send the error and path to your own internal systems
});

// Bind an event logger to log underlying events during development
website_compiler.set_logger((message) => {
    console.log(`[COMPILER] ${message}`); 
});

Application Compiler With Custom Processing

const ApplicationCompiler = require('application-compiler');

// Create compiler instance for the root file which is index.js
const website_compiler = new ApplicationCompiler({
    file_path: './index.js'
});

// Initiate a recalibration handler which will trigger custom processing on content changes
website_compiler.on_recalibration(() => {
    let compiled_code = website_compiler.compiled;
    // Do your own custom processing/compiled file writing here
});

// Bind an error logger
website_compiler.set_error_logger((path, error) => {
    // Send the error and path to your own internal systems
});

// Bind an event logger to log underlying events during development
website_compiler.set_logger((message) => {
    console.log(`[COMPILER] ${message}`); 
});

Compiler

Below is a breakdown of the Compiler class generated when creating a application compiler instance.

Constructor Options

  • file_path [String]: Path to the root/entry javascript file.
    • Example: ./master.js
    • Required for a Compiler Instance.
  • watcher_delay [Number]: Delay to enforce between FileWatcher updates in milliseconds.
    • Default: 250
  • include_tag [String]: Name of include method used during compilation.
    • Default: include
    • Example: include will convert all include(path) to their respective compiled code.

Compiler Properties

| Property | Type | Description | | :-------- | :------- | :------------------------- | | compiled | String | Returns compiled application code. | | chunks | Object | Contains nested objects which represent compiled code. | | pool | WatcherPool | Contains underlying WatcherPool instance. | | watchers | Object | Contains FileWatcher instances with their handlers. |

Compiler Methods

  • write_to(Object: options): Begins automatic file writing on content changes.
    • options: Automatic compilation options.
      • path[String]: Specifies where compiled file is written.
      • file_name[String]: Specifies the name of the compiled file.
        • Default: compiled_{root_file_name}.js
      • write_delay[Number]: Enforces delay between fast file writes in milliseconds.
        • Default: 250
      • relative_errors[Boolean]: Enables contextually relative Error traces for compile-time/syntax errors.
        • Default: true
        • Note Errors will be written directly into compiled file.
      • runtime_relative_errors[Boolean]: Enables contextually relative Error traces for run-time errors including uncaught promise exceptions.
        • Default: true
        • Note This will simply log the Error trace and exit the program.
        • Custom Handler: Use the following code anywhere in your application to handle relative error traces: require('application-compiler').log_relative_errors((String: error_trace) => { /* Your Code Here... */ })
        • Note that you must call process.exit(code) at the end of your code for any custom handling above to ensure you restart the application.
    • Note using this method can allow for fast development due to the automatic compilation.
  • on_recalibration(Function: handler): Triggered when a file content change is detected and code is recompiled.
    • Handler Example: () => {}
    • Note this can be used to do your own post processing/file writing on content changes.
  • set_error_handler(Function: handler): Sets error logger for all errors that occur in compiler.
    • Handler Example: (String: path, Error: error) => {}
      • path: The path of the file where the internal error occured.
      • error: The error object of the error that has occured.
    • Note: The usage of an error handler is recommended to log FileSystem errors.
  • set_logger(Function: logger): Sets logger for logging compiler events.
    • Logger Example: (String: message) => {}
      • message: Log message
    • Message Format: Event -> Hierarchy
      • Event: Type of action performed by compiler.
        • INITIALIZED: A new file has been loaded and is being watched.
        • DETECTED_CHANGES: A content change was detected triggering recalibration.
        • DESTROYED: This file has been destroyed and is no longer being watched.
      • Hierarchy: The hierarchy of inclusions for file separated by /
        • Example: root.js/routes.js/login.js where root.js is the file_path and event occured in login.js.
        • Note: Hierachy goes from root to specific inclusion.
  • destroy(): Destroys compiler instance along with underlying WatcherPool and nested file instances.

License

MIT