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

endif-loader

v1.0.0

Published

endif-loader realized Condition compiler, it can Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration

Downloads

7

Readme

endif-loader

endif-loader realized Condition compiler, it can Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration

Configuration

Install via npm:

$ npm install --save endif-loader

Usage Examples

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /target-file.js$/,
        use: [
          {
            loader: `enif-loader`,
            options: {
              context: {
                DEV: process.env.NODE_ENV === 'development'
              }
            }
          },
        ],
      },
    ],
  },
};

What does it look like?

<head>
  <title>Your App</title>

  <!-- #if NODE_ENV='production' -->
  <script src="some/production/lib/like/analytics.js"></script>
  <!-- #endif -->

</head>
<body>
  <!-- #ifdef DEBUG -->
  <h1>Debugging mode - <!-- #echo RELEASE_TAG --> </h1>
  <!-- #endif -->
  <p>
  <!-- #include welcome_message.txt -->
  </p>
</body>
var configValue = '/* #echo FOO */' || 'default value';

// #ifdef DEBUG
someDebuggingCall()
// #endif

Directive syntax

Basic example

The most basic usage is for files that only have two states, non-processed and processed. In this case, your #exclude directives are removed after preprocessing

<body>
    <!-- #exclude -->
    <header>You're on dev!</header>
    <!-- #endexclude -->
</body>

After build

<body>
</body>

All directives

  • #if VAR='value' / #endif This will include the enclosed block if your test passes
  • #ifdef VAR / #endif This will include the enclosed block if VAR is defined (typeof !== 'undefined')
  • #ifndef VAR / #endif This will include the enclosed block if VAR is not defined (typeof === 'undefined')
  • #include This will include the source from an external file. If the included source ends with a newline then the following line will be space indented to the level the #include was found.
  • #include-static Works the same way as #include but doesn't process the included file recursively. Is useful if a large file has to be included and the recursive processing is not necessary or would otherwise take too long.
  • #extend file.html / #endextend This will use the source from the external file indicated with the #extend tag to wrap the enclosed block.
  • #extendable This tag is used to indicate the location in a file referenced using #extend where the block enclosed by #extend will be populated.
  • #exclude / #endexclude This will remove the enclosed block upon processing
  • #echo VAR This will include the environment variable VAR into your source
  • #foreach $VAR in ARR / #endfor This will repeat the enclosed block for each value in the Array or Object in ARR. Each value in ARR can be interpolated into the resulting content with $VAR.
  • #exec FUNCTION([param1, param2...]) This will execute the environment FUNCTION with its parameters and echo the result into your source. The parameter could be a string or a reference to another environment variable.

Extended html Syntax

This is useful for more fine grained control of your files over multiple environment configurations. You have access to simple tests of any variable within the context (or ENV, if not supplied)

<body>
    <!-- #if NODE_ENV!='production' -->
    <header>You're on dev!</header>
    <!-- #endif -->

    <!-- #if NODE_ENV='production' -->
    <script src="some/production/javascript.js"></script>
    <!-- #endif -->

    <script>
    var fingerprint = '<!-- #echo COMMIT_HASH -->' || 'DEFAULT';
    </script>

    <script src="<!-- #exec static_path('another/production/javascript.js') -->"></script>
</body>

With a NODE_ENV set to production and 0xDEADBEEF in COMMIT_HASH this will be built to look like

<body>
    <script src="some/production/javascript.js"></script>

    <script>
    var fingerprint = '0xDEADBEEF' || 'DEFAULT';
    </script>

    <script src="http://cdn2.my.domain.com/another/javascript.js"></script>
</body>

With NODE_ENV not set or set to dev and nothing in COMMIT_HASH, the built file will be

<body>
    <header>You're on dev!</header>

    <script>
    var fingerprint = '' || 'DEFAULT';
    </script>

    <script src="http://localhost/myapp/statics/another/javascript.js"></script>
</body>

You can also have conditional blocks that are hidden by default by using the fictional !> end tag instead of --> after your condition:

<!-- #if true !>
<p>Process was run!</p>
<!-- #endif -->

JavaScript, CSS, C, Java Syntax

Extended syntax below, but will work without specifying a test

normalFunction();
//#exclude
superExpensiveDebugFunction()
//#endexclude

anotherFunction('/* @echo USERNAME */');

Built with a NODE_ENV of production :

normalFunction();

anotherFunction('jsoverson');

Like HTML, you can have conditional blocks that are hidden by default by ending the directive with a ** instead of */

angular.module('myModule', ['dep1'
    , 'dep2'
    /* #if NODE_ENV='production' **
    , 'prod_dep'
    /* #endif */
    /* #exclude **
    , 'debug_dep'
    /* #endexclude */
]);

Note: Hidden by default blocks only work with block comments (/* */) but not with line comments (//).

CSS example

body {
/* #if NODE_ENV=='development' */
  background-color: red;
/* #endif */

}
// #include util.css

(CSS preprocessing supports single line comment style directives)

Shell, PHP

#!/bin/bash

# #include util.sh

License

Licensed under the Apache 2.0 license.