0.0.3 • Published 4 years ago
@dokkanwiki/conditional_js v0.0.3
Conditional JavaScript
Use a C++ style pre-processor to conditionally remove JavaScript code.
Includes a Webpack loader.
Features
- Supports the following preprocessor types:
#define,#undef#if,#ifdef,#ifndef#else,#elif,#elifdef,#elifndef#error,#endif
- Conditional statements are pure javascript
- Allows a global definition context
- Supports
isolate-vmfor executing conditions in a secure enviroment - Supports nested conditions
- Can be used through the provided Webpack loader or as a standalone class
- Supports multi-level sourcemap generation
Conditionals
#define <IDENTIFIER> <VALUE?>: add a definition for just one file. Does not nest into any imported/required files. If avalueis not provided, theidentifierwill be set totrue. Thevaluecan be a javascript expression.#undef <IDENTIFIER>: remove a definition. Global definitions that are undefined are removed for that file only.#if <EXPRESSION>: Start a block that will be removed ifexpressionis falsy.#ifdef <IDENTIFIER>: Start a block that will be removed if theidentifieris not defined (no matter the value).#ifndef <IDENTIFIER>: Start a block that will be removed if theidentifieris defined (no matter the value).#else: Start a sub-block that will be removed if another prior block is active.#elif <EXPRESSION>: Start a sub-block that will be removed if theexpressionis falsy or a prior block is active.#elifdef <IDENTIFIER>: Start a sub-block that will be removed if theidentifieris not defined (no matter the value) or a prior block is active.#elifndef <IDENTIFIER>: Start a sub-block that will be removed if theidentifieris defined (no matter the value) or a prior block is active.#error <MESSAGE>: Throw an error withmessageif allowed to execute or is in an active block.#endif: Close a block
Installation
Install with a node package manager:
npm install @dokkanwiki/conditional_js
# or
yarn add @dokkanwiki/conditional_js
# or
pnpm add @dokkanwiki/conditional_jsWebpack Loader Usage/Examples
Webpack loader documentation: https://webpack.js.org/loaders/
Within your webpack configuration, you'll need to add @dokkanwiki/conditional_js to the rules that are processing your JavaScript.
Process javascript files directly:
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: '@dokkanwiki/conditional_js',
options: {
definitions: {
DEBUG: true,
NODE_ENV: process.env.NODE_ENV
}
}
}
}
]
}Process before Babel:
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', { targets: "defaults" }]
]
}
},
{
loader: '@dokkanwiki/conditional_js',
options: {
DEBUG: process.env.NODE_ENV === 'development'
}
}
]
}
]
}Options
parser: Defaults to@style macros (@if,@ifdef, etc).- Provide a string to determine flavor:
'@'or'#' - Alternatively provide an object to define your own parsing method. Provide the following properties:
file_detect: Regex string orRegExpclass instance- Used to detect if a file should be processed
define,if,ifdef,ifndef,elif,else,elifdef,elifndef,endif,undef,error- Regex string,
RegExpclass instance, or a function that takes a string and returns eitherundefinedfor no match or an array of object with the following properties:valuestring of matched group,indexposition of matched group item in the string. See src/options.ts for additional information.
- Regex string,
- Provide a string to determine flavor:
action: Defaults toremove.- Control what happens to inactive conditional blocks.
- Options are:
removeorcomment
definitions: Object (key => value) of global definitions to apply to all files.sandbox: Defaults totrue- Run conditional statements in
isolate-vmsandbox - Provide
trueto run in sandbox with default options. - NOT RECOMMENDED Provide
falseto run conditional statements witheval(). Provide an object with the following properties:
memory_limit: Memory limit in megabytes. Defaults to 128MB.timeout: Timeout for individual condition in milliseconds. Defaults to 250ms.
- Run conditional statements in
API Reference
ConditionalJsProcessor
new ConditionalJsProcessor(options)process(source, context)source: file content asstringcontext: Optionalobjectwith the following properties:file_name:stringsource_map:stringorobject
- Returns promise resolving to
objectwith the following properties:transformed_sourcetransformed_mapprocessor:ConditionalJsProcessorinstance
release()- Clean up
isolated-vmexecutor. No-op if not using a sandbox.
- Clean up
Examples
// @if NODE_ENV === 'production'
// @undef PERFORMANCE_STATS
// @elif NODE_ENV === 'development'
// @define PERFORMANCE_STATS
// @else
// @error NODE_ENV must be either 'production' or 'development'
// @endif
const render = function () {
// @ifdef PERFORMANCE_STATS
const t0 = performance.now();
// @endif
requestAnimationFrame(render);
renderer.render(scene, camera);
// @ifdef PERFORMANCE_STATS
const t1 = performance.now();
console.log(`Call to render() took ${t1 - t0} milliseconds.`);
// @endif
};