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

macro-compiler

v1.1.1

Published

Write efficient code for nodejs and browser without weird tricks

Downloads

10

Readme

Macro Compiler

Separate explicitly portions of code that will be executed only on a specific target.

Example

  • File: src-folder/index.ts
//! if target == Browser
// import { Buffer } from "./my-custom-browser-buffer";

//!start if target == NodeJs
import { Buffer } from "node:buffer";
import { RTCPeerConnection } from "wrtc";
//!end

...
  • File: src-folder/my-custom-browser-buffer.ts
//!file if target == Browser

...

Once we run the compiler (npx macro-compiler src-folder). The nodejs and browser code will be generated.

  • Generated File: NodeJs/src/index.ts
//!start if target == NodeJs
import { Buffer } from "node:buffer";
import { RTCPeerConnection } from "wrtc";
//!end

...
  • Generated File: Browser/src/index.ts
//! if target == Browser
import { Buffer } from "./my-custom-browser-buffer";

...
  • Generated File: Browser/src/my-custom-browser-buffer.ts
//!file if target == Browser

...

How it works

The code will be divided into various sections The sections are defined explicitly with macros. The purpose is to separate portions of code that will be executed only on a specific target.

Macros

They are simple comments that define what code will be deleted or conserved depending on the target. The macros can select lines, blocks or files.

Select a line

Defined by //!, it will only select the next line.

//! if target == NodeJs
console.log("Code that will run on NodeJs");
console.log("Code that will run every where");

If we concatenate multiple blocks, the IDE can think that we are makeing a Duplicate identifier error. To prevent that you can comment the selected line. The macro-compiler will uncomment the code if the target is selected.

Select a block

Selcets the code that is in between (//!start or /*!) and (//!end or */).

The /*! */ macro should only be used on imports because it is going to disable the syntax highlighting. It is recommended to write the code on a different file (see Select a File).

//!start if target == NodeJs
import { deflate, inflate } from "node:zlib";
import { Buffer } from "node:buffer";
//!end

/*! if target == Browser
import { deflate, inflate } from "./browser-zlib";
import { Buffer } from "./browser-buffer";
*/

Select a file

Defined by //!file on the beginning of the file. It will remove or copy the file depending on the target.

//!file if target == Browser

console.log("A file for the browser");

...

export const Buffer = MyWebBuffer;

CLI

  • Usage: macro-compiler [source directory/file] [optional-flags]

  • Example: macro-compiler src-folder -t NodeJs dist/nodejs/src Browser dist/browser/src

| Flag | Description | Default | | ---------------- | ------------------------------------------------ | --------------------------------------- | | -t, --target | Set the compilation targets | NodeJs NodeJs/src Browser Browser/src | | -r, --rootDir | Specify the root folder | current-directory | | -c, --clean | Delete the target directories before compilation | true | |