Giter Site home page Giter Site logo

amarcruz / jscc-brunch Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 1.0 21 KB

Adds conditional compilation and compile-time variable replacement support to brunch

License: MIT License

JavaScript 100.00%
brunch brunch-plugin conditional comments compilation javascript es6 preprocessor sourcemap typescript

jscc-brunch's Introduction

jscc-brunch

npm Version License

Adds conditional compilation and compile-time variable replacement support to brunch.

jscc-brunch is not a transpiler, it is a wrapper of jscc, a tiny and powerful, language agnostic file preprocessor that uses JavaScript to transform text based on expressions at compile time.

With jscc, you have:

  • Conditional inclusion/exclusion of blocks, based on compile-time variables*
  • Compile-time variables with all the power of JavaScript expressions
  • Replacement of variables in the sources, by its value at compile-time
  • Sourcemap support, useful for JavaScript sources.
  • TypeScript v3 definitions

IMPORTANT:

From v2.8.3, the generation of source map is disabled by default to solve issues with many plugins that does not supports chained source maps. With sourceMaps disabled, jscc will only keep the correct line numbers.

Please read more about this in Using Source Maps.

Install

Install the plugin via npm with npm i jscc-brunch -D.

or, do manual install:

  • Add "jscc-brunch": "~x.y.z" to package.json of your brunch app.
  • If you want to use git version of plugin, use the GitHub URI "jscc-brunch": "aMarCruz/jscc-brunch".

Usage

In brunch the order matters and jscc is a preprocessor, so please put it before compilers in the devDependencies of your package.json.

Set the options in your brunch-config file:

  ...
  plugins: {
    jscc: {
      values: {
        _DEBUG: 1,
        _MYAPP: 'My App' }
    }
  }
  ...

Use it:

/*#if _DEBUG
const mylib = require('mylib-debug');
//#else */
const mylib = require('mylib');
//#endif

mylib.log('Starting $_MYAPP v$_VERSION...');

output:

const mylib = require('mylib-debug');

mylib.log('Starting My App v1.0.0...');

That's it.

* jscc has two predefined memvars: _FILE and _VERSION, in addition to giving access to the environment variables through the nodejs proccess.env object.

See Syntax in the jscc wiki for information about templating.

Options

Plain JavaScript object, with all properties optional.

Name Type Description
escapeQuotes string String with the type of quotes to escape in the output of strings: 'single', 'double' or 'both'. This has no default.
prefixes string | RegExp |
Array<string|RegExp>
The start of a directive. That is the characters before the '#', usually the start of comments.
Default '//', '/*', '<!--' (with one optional space after them).
values object Plain object defining the variables used by jscc at compile-time.
ignore anymatch Specify which files in your project should not be processed.
Default `/^(bower_components
pattern RegExp Regular expression that matches the file paths you want to process.
Default `/.(js
sourceMaps boolean Enable the generation of sourcemap (if sourceMaps:true in your brunch config).
Default false
sourceMapFor anymatch Files for which sourcemap must be generated if sourceMaps:true.
Default JavaScript/TypeScript files.

Using Source Maps

You can enable the generation of sourcemap if your are not using JS compilers or your compiler can merge sourcemaps*:

  ...
  plugins: {
    jscc: {
      sourceMaps: true,        // enable sourcemap generation in jscc
      sourceMapFor: /\.js$/,   // allows sourcemap for .js files only
      values: {
        _DEBUG: 1,
        _MYAPP: 'My App'
      }
    }
  }
  ...

Even with sourceMaps: true, sourcemap generation is limited to JavaScript/TypeScript files. You can change this with anymatchs through the sourceMapFor option.

* The jscc plugin does support merging sourcemaps.

Documentation

You can read in the jscc Wiki about:

What's New

  • Date and RegExp types outputs its stringified value (same behavior of the strings).
  • The pattern option defaults to .js, .jsx, .ts and .tsx extensions.
  • New option escapeQuotes.
  • Optimized ouput of property values, now you can use more than one property.
  • Using jscc v1.1.0

Support my Work

I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time and effort so, if you like my work, please consider...

Of course, feedback, PRs, and stars are also welcome 🙃

Thanks for your support!

License

The MIT License (MIT)

Copyright (c) 2016-2018 Alberto Martínez

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.