• Stars
    star
    363
  • Rank 117,374 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 12 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration

preprocess

NPM

Linux Build Status Windows Build Status Coverage Status dependencies dev-dependencies

Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration

Configuration

Install via npm:

$ npm install --save preprocess

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

API

preprocess(source[, context[, options]]) -> preprocessedSource

Preprocesses a source provided as a string and returns the preprocessed source.

source

Type: String (mandatory)

The source to preprocess.

context

Type: Object Default: process.env

The context that contains the variables that are used in the source. For @extend variants and @include the additional context property src is available inside of files to be included that contains the current file name. This property is also available in the context of the source file if one of the preprocessFile*() API variants are used.

options

Type: Object

The options object allows to pass additional options to preprocess. Available options are:

options.fileNotFoundSilentFail

Type: Boolean Default: false

When using @include variants and @extend, preprocess will by default throw an exception in case an included file can't be found. Set this option to true to instruct preprocess to fail silently and instead of throwing to write a message inside of the preprocessed file that an included file could not be found.

options.srcDir

Type: String Default: process.cwd()

The directory where to look for files included via @include variants and @extend.

options.srcEol

Type: String Default: EOL of source string or os.EOL if source string contains multiple different or no EOLs.

The end of line (EOL) character to use for the preprocessed result. May be one of:

  • \r\n - Windows
  • \n - Linux/OSX/Unix
  • \r - legacy Mac
options.type

Type: String Default: html

The syntax type of source string to preprocess. There are 3 main syntax variants:

  • html, aliases: xml
  • js, aliases: javascript, jsx, json, c, cc, cpp, cs, csharp, java, less, sass, scss, css, php, ts, tsx, peg, pegjs, jade, styl
  • coffee, aliases: bash, shell, sh

preprocessFile(srcFile, destFile[, context[, callback[, options]]])

Preprocesses a sourceFile and saves the result to destFile. Simple wrapper around fs.readFile() and fs.writeFile().

srcFile

Type: String (mandatory)

The path to the source file to preprocess.

destFile

Type: String (mandatory)

The path to the destination file where the preprocessed result shall be saved.

context

See context attribute description of preprocess() function.

callback

Type: function(err)

The callback function that is called upon error or completion. Receives an error if something goes wrong as first parameter.

options

See options attribute description of preprocess() function. Differs only in that the default srcDir value is set to the path of the provided source file instead of process.cwd() and the default type is derived from source file extension.

preprocessFileSync(srcFile, destFile[, context[, options]])

Preprocesses a sourceFile and saves the result to destFile. Simple wrapper around fs.readFileSync() and fs.writeFileSync().

srcFile

Type: String (mandatory)

The path to the source file to preprocess.

destFile

Type: String (mandatory)

The path to the destination file where the preprocessed result shall be saved.

context

See context attribute description of preprocess() function.

options

See options attribute description of preprocess() function. Differs only in that the default srcDir value is set to the path of the provided source file instead of process.cwd() and the default type is derived from source file extension.

Usage Examples

var pp = require('preprocess');

var text = 'Hi, I am <!-- @echo USERNAME -->';

pp.preprocess(text);
// -> Hi, I am jsoverson

pp.preprocess(text, {USERNAME : "Bob"});
// -> Hi, I am Bob

// specify the format to use for the directives as the third parameter
pp.preprocess(text, {USERNAME : "Bob"}, {type: 'html'});
// -> Hi, I am Bob

// Preprocess files asynchronously
pp.preprocessFile(src, dest, context, callback, options);

// Preprocess files synchronously
pp.preprocessFileSync(src, dest, context, options);

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using jshint

Release History

  • 3.2.0

    • Fix npm incompatibility on CI (@pioug)
    • Run CI with LTS and latest Node versions (@pioug)
    • Support if-else statement (@hu9o, #121)
    • Add JSON extension as alias for js (@jirikrepl, #111)
  • 3.1.0

    • Added .jsx file extension as an alias for js (@BendingBender, #79)
    • Added .tsx file extension as an alias for js (@rosendi, #100)
    • Bumped XRegExp to v3
  • 3.0.1/2 Fixes for backward compatibility and regex cleanups (thanks to @anseki for suggestions, #77)

  • 3.0.0

    Breaking changes:

    • If a file requested by @include or @extend can not be found, preprocess will now throw by default with a possibility to opt in to the legacy behavior via the fileNotFoundSilentFail option (@BendingBender, #35).
    • Fixed multiple issues with newlines (@BendingBender, #8), this may result in output that differs from earlier versions.
    • The srcDir option was moved to the options object and now defaults to process.cwd instead of throwing by default (@BendingBender, #68)

    New functionality:

    • All block directives (ones that have a start and an end token, like @if/@endif) are now processed recursively (@Frizi, #61)
    • Added hidden by default configuration blocks for js (@mallowigi, #40) and html (@Frizi, #66)

    Fixes:

    • fixed @exec in files included via @include and @extend (@BendingBender, #58)
    • changed @extend and @exclude html regex so that directives may appear more than once in one line (@BendingBender, #36)
    • fixed multiple issues with coffescript syntax (@BendingBender, #39)
    • fixed @if and @foreach to not require trailing whitespace (@BendingBender, #74)
  • 2.3.1 Fixed @echo and @exec directives to allow - and * characters, fixed @exec with multiple params (@BendingBender, #21, #45, #51, #54).

  • 2.3.0 Added support for @include-static (@BendingBender)

  • 2.2.0 Added support for @foreach and @extend (@orionstein)

  • 2.1.1 Added support for .styl files via js regex (@nsonnad)

  • 2.1.0 Added automatic support for numerous formats, merged @exec, hidden by default html tags, added simple directives

  • 2.0.0 Added ability to echo strings, added conditional comments, removed lodash, merged 17, 13, 15, 16

  • 1.2.0 Added processing for hash-style comments (@marsch). Added more file aliases.

  • 1.1.0 Added deep inclusion, fixed sequential ifs

  • 1.0.1 Fixed multiple inline echo statements

  • 1.0.0 Pulled from grunt-preprocess to stand alone

License

Copyright Jarrod Overson

Written by Jarrod Overson

Licensed under the Apache 2.0 license.

More Repositories

1

node-to-rust

Rust
1,372
star
2

grunt-preprocess

Preprocess files based off environment configuration
JavaScript
294
star
3

JavaScript-Particle-System

2d JavaScript Particle System with gravity field simulation
JavaScript
242
star
4

grunt-env

Grunt task to automate environment configuration for future tasks
JavaScript
224
star
5

grunt-plato

Generate static analysis reports with plato through grunt
JavaScript
206
star
6

hackium

TypeScript
175
star
7

shift-refactor

A suite of utilities to query and modify JavaScript source
TypeScript
131
star
8

grunt-open

Open urls and files from a grunt task
JavaScript
112
star
9

jquery.pulse.js

jQuery Pulse Animation Plugin
CSS
82
star
10

puppeteer-interceptor

Easy request interception/response modification for puppeteer
TypeScript
51
star
11

rcl

A websocket bridge for debug logging from browser to server
JavaScript
44
star
12

grunt-contrib-jasmine-example

Example application to help get you started using jasmine through grunt
JavaScript
42
star
13

grunt-strip

Strip JavaScript nodes (like console.*) out of your source code
JavaScript
42
star
14

deobfuscating-js-streams

JavaScript
27
star
15

gitfaq

organized git faq
SCSS
26
star
16

workshop-reverse-engineering

JavaScript
23
star
17

shift-interpreter

TypeScript
22
star
18

jsconsole

JavaScript console/repl styled after chrome devtools.
JavaScript
22
star
19

RetwisJS

Port of Redis's Twitter clone, retwis for node.js
19
star
20

puppeteer-extra-interceptor

Puppeteer-extra plugin for puppeteer-interceptor
TypeScript
18
star
21

es6repl

reusable babel repl
JavaScript
15
star
22

Backbone.Augment

Simple plugin/addon functionality for Backbone Views, Models, and Collections.
JavaScript
15
star
23

puppeteer-extensionbridge

Expose the Chrome extension API to puppeteer
TypeScript
13
star
24

javascript-deobfuscation-AMA

General questions about deobfuscating JavaScript
13
star
25

grunt-jasmine-runner-example

Example setup for grunt-jasmine-runner
JavaScript
12
star
26

badjs.org

CSS
12
star
27

shift-query-cli

Command line tool to query AST nodes in JavaScript source files.
JavaScript
11
star
28

combolist-generator

JavaScript
10
star
29

texture.js

Basic implementation of normal mapped textures in javascript
JavaScript
10
star
30

stream-unzip

A streaming unzip library for rust projects
Rust
9
star
31

html5hub-particlesystem

Particle System demo for html5hub
JavaScript
8
star
32

js-identifiers

JavaScript
8
star
33

karma-commonjs-example

Example repo showing commonjs tests running on command line as well as in karma
JavaScript
7
star
34

shift-visualizer

Visualizer for the Shift AST format
JavaScript
5
star
35

string-format

Sprintf inspired string format method.
JavaScript
5
star
36

typescript-boilerplate

Boilerplate TypeScript project with mocha, eslint, prettier, and Visual Studio Code preconfigured
TypeScript
4
star
37

refactor-plugin-common

TypeScript
4
star
38

shift-identifiers

Find similar identifiers across a source file
JavaScript
4
star
39

marionette.augment.inlineregions

JavaScript
4
star
40

grunt-require-dir

Task to bundle up a directory of AMD modules into one accessor module
JavaScript
3
star
41

fromgit

TypeScript
3
star
42

shift-complexity

Complexity analysis for JavaScript source code.
JavaScript
3
star
43

liquid-json

A library to parse structured JSON with embedded liquid templates
Rust
3
star
44

apex-wip

TypeScript
3
star
45

hackium-plugin-visiblecursor

JavaScript
3
star
46

box2d-intro-amd

Seth Ladd's excellent box2d intro migrated to a modular structure with requirejs.
JavaScript
3
star
47

gh-cloneall

Utility to clone all github repos for a user or organization
JavaScript
3
star
48

concentric-circle-illusion

Visualization on html canvas of the classic optical illusion http://i.imgur.com/p2yeT.jpg
JavaScript
3
star
49

AMD_RequireJS-slides

Talk given to SanDiegoJS on Jan 10, 2012
JavaScript
2
star
50

believable

TypeScript
2
star
51

randomfarts.com

HTML
2
star
52

AMD-urlParams

A query string parser in AMD format for require.js, curl.js, etc
JavaScript
2
star
53

auto-cli-replay

Automatically create CLI GIFs with simulated typing, dynamic interactivity, and more.
Shell
2
star
54

git-spread

Run git commands across multiple directories
Shell
2
star
55

es6-repl

web component wrapping the es6repl
JavaScript
2
star
56

interpreter-context-jsdom

TypeScript
2
star
57

widl2json

WIDL to JSON converter
JavaScript
2
star
58

hackium-plugin-preserve-native

TypeScript
2
star
59

chrome-devtools-slides

Slides for sandiego.js July talk
JavaScript
2
star
60

jsperf-common

List of good jsperf benchmarks for common situations
1
star
61

browserusage.org

Site to simply display worldwide browser usage stats.
CSS
1
star
62

wacl

Rust
1
star
63

perl-Parallel-Forking-Manager

Thread manager for Moose objects with role 'Forking'
Perl
1
star
64

gcp-simple-secrets

TypeScript
1
star
65

cordova-flashcard-app

Objective-C
1
star
66

perl-MooseX-Role-Forking

Forking/Threading role for Perl Moose
Perl
1
star
67

refactor-plugin-unsafe

TypeScript
1
star
68

lockyourdamncomputer.com

CSS
1
star
69

phaser-mario

JavaScript
1
star
70

python-game

Python
1
star
71

example-cli

Rust
1
star
72

gh-action-test

TypeScript
1
star
73

dotfiles

Shell
1
star
74

jsoverson.github.com

stub
1
star
75

fromgit-test

1
star
76

mp-tuning

Rust
1
star
77

allfaqs

HTML
1
star
78

wapc-rust-template

Makefile
1
star
79

apex-template

TypeScript
1
star
80

seeded-random-utils

TypeScript
1
star
81

wit-example

Rust
1
star
82

d3-starter

basic repo that includes what's necessary to experiment with d3
CSS
1
star