初始化

This commit is contained in:
yziiy
2025-08-11 11:06:07 +08:00
parent 083bc37c00
commit 5607d11395
19772 changed files with 3108723 additions and 18 deletions

View File

@@ -0,0 +1,13 @@
Copyright 2012 OneHealth Solutions, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,410 @@
# preprocess
[![NPM][npm-image]][npm-url]
[![Linux Build Status][linux-ci-image]][linux-ci-url] [![Windows Build Status][windows-ci-image]][windows-ci-url] [![Coverage Status][coverage-image]][coverage-url] [![dependencies][deps-image]][deps-url] [![dev-dependencies][dev-deps-image]][dev-deps-url]
Preprocess HTML, JavaScript, and other files with directives based off custom or ENV configuration
## Configuration
Install via npm:
```bash
$ npm install --save preprocess
```
## What does it look like?
```html
<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>
```
```js
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
```html
<body>
<!-- @exclude -->
<header>You're on dev!</header>
<!-- @endexclude -->
</body>
```
After build
```html
<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)
```html
<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
```html
<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
```html
<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:
```html
<!-- @if true !>
<p>Process was run!</p>
<!-- @endif -->
```
### JavaScript, CSS, C, Java Syntax
Extended syntax below, but will work without specifying a test
```js
normalFunction();
//@exclude
superExpensiveDebugFunction()
//@endexclude
anotherFunction('/* @echo USERNAME */');
```
Built with a NODE_ENV of production :
```js
normalFunction();
anotherFunction('jsoverson');
```
Like HTML, you can have conditional blocks that are hidden by default by ending the directive with a `**` instead of `*/`
```js
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
```css
body {
/* @if NODE_ENV=='development' */
background-color: red;
/* @endif */
}
// @include util.css
```
(CSS preprocessing supports single line comment style directives)
### Shell, PHP
```bash
#!/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`, `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](#context) 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](#options) 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](#context) of `preprocess()` function.
#### options
See `options` [attribute description](#options) 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
```js
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.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.
[npm-image]: https://nodei.co/npm/preprocess.png?downloads=true
[npm-url]: https://www.npmjs.com/package/preprocess
[linux-ci-image]: https://img.shields.io/travis/jsoverson/preprocess/master.svg?style=flat-square&label=Linux%20build
[linux-ci-url]: https://travis-ci.org/jsoverson/preprocess
[windows-ci-image]: https://img.shields.io/appveyor/ci/BendingBender/preprocess/master.svg?style=flat-square&label=Windows%20build
[windows-ci-url]: https://ci.appveyor.com/project/BendingBender/preprocess
[deps-image]: https://img.shields.io/david/jsoverson/preprocess.svg?style=flat-square
[deps-url]: https://david-dm.org/jsoverson/preprocess
[dev-deps-image]: https://img.shields.io/david/dev/jsoverson/preprocess.svg?style=flat-square
[dev-deps-url]: https://david-dm.org/jsoverson/preprocess#info=devDependencies
[coverage-image]: https://img.shields.io/coveralls/jsoverson/preprocess/master.svg?style=flat-square
[coverage-url]: https://coveralls.io/r/jsoverson/preprocess?branch=master

View File

@@ -0,0 +1,438 @@
/*
* preprocess
* https://github.com/onehealth/preprocess
*
* Copyright (c) 2012 OneHealth Solutions, Inc.
* Written by Jarrod Overson - http://jarrodoverson.com/
* Licensed under the Apache 2.0 license.
*/
'use strict';
exports.preprocess = preprocess;
exports.preprocessFile = preprocessFile;
exports.preprocessFileSync = preprocessFileSync;
var path = require('path'),
fs = require('fs'),
os = require('os'),
delim = require('./regexrules'),
XRegExp = require('xregexp');
function preprocessFile(srcFile, destFile, context, callback, options) {
options = getOptionsForFile(srcFile, options);
context.src = srcFile;
fs.readFile(srcFile, function (err, data) {
if (err) return callback(err, data);
var parsed = preprocess(data, context, options);
fs.writeFile(destFile, parsed, callback);
});
}
function preprocessFileSync(srcFile, destFile, context, options) {
options = getOptionsForFile(srcFile, options);
context.src = srcFile;
var data = fs.readFileSync(srcFile);
var parsed = preprocess(data, context, options);
return fs.writeFileSync(destFile, parsed);
}
function getOptionsForFile(srcFile, options) {
options = options || {};
options.srcDir = options.srcDir || path.dirname(srcFile);
options.type = options.type || getExtension(srcFile);
return options;
}
function getExtension(filename) {
var ext = path.extname(filename||'').split('.');
return ext[ext.length - 1];
}
function preprocess(src, context, typeOrOptions) {
src = src.toString();
context = context || process.env;
// default values
var options = {
fileNotFoundSilentFail: false,
srcDir: process.cwd(),
srcEol: getEolType(src),
type: delim['html']
};
// needed for backward compatibility with 2.x.x series
if (typeof typeOrOptions === 'string') {
typeOrOptions = {
type: typeOrOptions
};
}
// needed for backward compatibility with 2.x.x series
if (typeof context.srcDir === "string") {
typeOrOptions = typeOrOptions || {};
typeOrOptions.srcDir = context.srcDir;
}
if (typeOrOptions && typeof typeOrOptions === 'object') {
options.srcDir = typeOrOptions.srcDir || options.srcDir;
options.fileNotFoundSilentFail = typeOrOptions.fileNotFoundSilentFail || options.fileNotFoundSilentFail;
options.srcEol = typeOrOptions.srcEol || options.srcEol;
options.type = delim[typeOrOptions.type] || options.type;
}
context = copy(context);
return preprocessor(src, context, options);
}
function preprocessor(src, context, opts, noRestoreEol) {
src = normalizeEol(src);
var rv = src;
rv = replace(rv, opts.type.include, processIncludeDirective.bind(null, false, context, opts));
if (opts.type.extend) {
rv = replaceRecursive(rv, opts.type.extend, function(startMatches, endMatches, include, recurse) {
var file = (startMatches[1] || '').trim();
var extendedContext = copy(context);
var extendedOpts = copy(opts);
extendedContext.src = path.join(opts.srcDir, file);
extendedOpts.srcDir = path.dirname(extendedContext.src);
var fileContents = getFileContents(extendedContext.src, opts.fileNotFoundSilentFail, context.src);
if (fileContents.error) {
return fileContents.contents;
}
var extendedSource = preprocessor(fileContents.contents, extendedContext, extendedOpts, true).trim();
if (extendedSource) {
include = include.replace(/^\n?|\n?$/g, '');
return replace(extendedSource, opts.type.extendable, recurse(include));
} else {
return '';
}
});
}
if (opts.type.foreach) {
rv = replaceRecursive(rv, opts.type.foreach, function(startMatches, endMatches, include, recurse) {
var variable = (startMatches[1] || '').trim();
var forParams = variable.split(' ');
if (forParams.length === 3) {
var contextVar = forParams[2];
var arrString = getDeepPropFromObj(context, contextVar);
var eachArr;
if (arrString.match(/\{(.*)\}/)) {
eachArr = JSON.parse(arrString);
} else if (arrString.match(/\[(.*)\]/)) {
eachArr = arrString.slice(1, -1);
eachArr = eachArr.split(',');
eachArr = eachArr.map(function(arrEntry){
return arrEntry.replace(/\s*(['"])(.*)\1\s*/, '$2');
});
} else {
eachArr = arrString.split(',');
}
var replaceToken = new RegExp(XRegExp.escape(forParams[0]), 'g');
var recursedInclude = recurse(include);
return Object.keys(eachArr).reduce(function(stringBuilder, arrKey){
var arrEntry = eachArr[arrKey];
return stringBuilder + recursedInclude.replace(replaceToken, arrEntry);
}, '');
} else {
return '';
}
});
}
if (opts.type.exclude) {
rv = replaceRecursive(rv, opts.type.exclude, function(startMatches, endMatches, include, recurse){
var test = (startMatches[1] || '').trim();
return testPasses(test,context) ? '' : recurse(include);
});
}
if (opts.type.if) {
rv = replaceRecursive(rv, opts.type.if, function (startMatches, endMatches, include, recurse) {
// I need to recurse first, so I don't catch "inner" else-directives
var recursed = recurse(include);
// look for the first else-directive
var matches = opts.type.else && recursed.match(new RegExp(opts.type.else));
var match = (matches || [""])[0];
var index = match ? recursed.indexOf(match) : recursed.length;
var ifBlock = recursed.substring(0, index);
var elseBlock = recursed.substring(index + match.length); // empty string if no else-directive
var variant = startMatches[1];
var test = (startMatches[2] || '').trim();
// fixed by xxxxxx
var startLine = padContent(startMatches.input)
var endLine = padContent(endMatches.input)
switch(variant) {
case 'if':
case 'ifdef': // fixed by xxxxxx
return testPasses(test, context) ? (startLine + ifBlock + endLine) : (startLine + padContent(ifBlock) + padContent(match || '') + elseBlock + endLine);
case 'ifndef':
return !testPasses(test, context) ? (startLine + ifBlock + endLine) : (startLine + padContent(ifBlock) + padContent(match || '') + elseBlock + endLine);
// case 'ifdef':
// return typeof getDeepPropFromObj(context, test) !== 'undefined' ? ifBlock : elseBlock;
// case 'ifndef':
// return typeof getDeepPropFromObj(context, test) === 'undefined' ? ifBlock : elseBlock;
default:
throw new Error('Unknown if variant ' + variant + '.');
}
});
}
rv = replace(rv, opts.type.echo, function (match, variable) {
variable = (variable || '').trim();
// if we are surrounded by quotes, echo as a string
var stringMatch = variable.match(/^(['"])(.*)\1$/);
if (stringMatch) return stringMatch[2];
var arrString = getDeepPropFromObj(context, variable);
return typeof arrString !== 'undefined' ? arrString : '';
});
rv = replace(rv, opts.type.exec, function (match, name, value) {
name = (name || '').trim();
value = value || '';
var params = value.split(',');
var stringRegex = /^['"](.*)['"]$/;
params = params.map(function(param){
param = param.trim();
if (stringRegex.test(param)) { // handle string parameter
return param.replace(stringRegex, '$1');
} else { // handle variable parameter
return getDeepPropFromObj(context, param);
}
});
var fn = getDeepPropFromObj(context, name);
if (!fn || typeof fn !== 'function') return '';
return fn.apply(context, params);
});
rv = replace(rv, opts.type['include-static'], processIncludeDirective.bind(null, true, context, opts));
if (!noRestoreEol) {
rv = restoreEol(rv, opts.srcEol);
}
return rv;
}
function getEolType(source) {
var eol;
var foundEolTypeCnt = 0;
if (source.indexOf('\r\n') >= 0) {
eol = '\r\n';
foundEolTypeCnt++;
}
if (/\r[^\n]/.test(source)) {
eol = '\r';
foundEolTypeCnt++;
}
if (/[^\r]\n/.test(source)) {
eol = '\n';
foundEolTypeCnt++;
}
if (eol == null || foundEolTypeCnt > 1) {
eol = os.EOL;
}
return eol;
}
function normalizeEol(source, indent) {
// only process any kind of EOL if indentation has to be added, otherwise replace only non \n EOLs
if (indent) {
source = source.replace(/(?:\r?\n)|\r/g, '\n' + indent);
} else {
source = source.replace(/(?:\r\n)|\r/g, '\n');
}
return source;
}
function restoreEol(normalizedSource, originalEol) {
if (originalEol !== '\n') {
normalizedSource = normalizedSource.replace(/\n/g, originalEol);
}
return normalizedSource;
}
function replace(rv, rule, processor) {
var isRegex = typeof rule === 'string' || rule instanceof RegExp;
var isArray = Array.isArray(rule);
if (isRegex) {
rule = [new RegExp(rule,'gmi')];
} else if (isArray) {
rule = rule.map(function(subRule){
return new RegExp(subRule,'gmi');
});
} else {
throw new Error('Rule must be a String, a RegExp, or an Array.');
}
return rule.reduce(function(rv, rule){
return rv.replace(rule, processor);
}, rv);
}
function replaceRecursive(rv, rule, processor) {
if(!rule.start || !rule.end) {
throw new Error('Recursive rule must have start and end.');
}
var startRegex = new RegExp(rule.start, 'mi');
var endRegex = new RegExp(rule.end, 'mi');
function matchReplacePass(content) {
var matches = XRegExp.matchRecursive(content, rule.start, rule.end, 'gmi', {
valueNames: ['between', 'left', 'match', 'right']
});
var matchGroup = {
left: null,
match: null,
right: null
};
return matches.reduce(function (builder, match) {
switch(match.name) {
case 'between':
builder += match.value;
break;
case 'left':
matchGroup.left = startRegex.exec(match.value);
break;
case 'match':
matchGroup.match = match.value;
break;
case 'right':
matchGroup.right = endRegex.exec(match.value);
builder += processor(matchGroup.left, matchGroup.right, matchGroup.match, matchReplacePass);
break;
}
return builder;
}, '');
}
return matchReplacePass(rv);
}
function processIncludeDirective(isStatic, context, opts, match, linePrefix, file) {
file = (file || '').trim();
var indent = linePrefix.replace(/\S/g, ' ');
var includedContext = copy(context);
var includedOpts = copy(opts);
includedContext.src = path.join(opts.srcDir,file);
includedOpts.srcDir = path.dirname(includedContext.src);
var fileContents = getFileContents(includedContext.src, opts.fileNotFoundSilentFail, context.src);
if (fileContents.error) {
return linePrefix + fileContents.contents;
}
var includedSource = fileContents.contents;
if (isStatic) {
includedSource = fileContents.contents;
} else {
includedSource = preprocessor(fileContents.contents, includedContext, includedOpts, true);
}
includedSource = normalizeEol(includedSource, indent);
if (includedSource) {
return linePrefix + includedSource;
} else {
return linePrefix;
}
}
function getTestTemplate(test) {
/*jshint evil:true*/
test = test || 'true';
test = test.trim();
// force single equals replacement
// fixed by xxxxxx 不替换,会影响 >= 等判断
// test = test.replace(/([^=!])=([^=])/g, '$1==$2');
// fixed by xxxxxx
test = test.replace(/-/g, '_')
return new Function("context", "with (context||{}){ return ( " + test + " ); }");
}
// fixed by xxxxxx
function testPasses(test, context) {
var testFn = getTestTemplate(test)
try {
return testFn(context, getDeepPropFromObj)
} catch (e) {}
return false
}
function getFileContents(path, failSilent, requesterPath) {
try {
fs.statSync(path);
} catch (e) {
if (failSilent) {
return {error: true, contents: path + ' not found!'};
} else {
var errMsg = path;
errMsg = requesterPath ? errMsg + ' requested from ' + requesterPath : errMsg;
errMsg += ' not found!';
throw new Error(errMsg);
}
}
return {error: false, contents: fs.readFileSync(path).toString()};
}
function copy(obj) {
return Object.keys(obj).reduce(function (copyObj, objKey) {
copyObj[objKey] = obj[objKey];
return copyObj;
}, {});
}
function getDeepPropFromObj(obj, propPath) {
propPath.replace(/\[([^\]+?])\]/g, '.$1');
propPath = propPath.split('.');
// fast path, no need to loop if structurePath contains only a single segment
if (propPath.length === 1) {
return obj[propPath[0]];
}
// loop only as long as possible (no exceptions for null/undefined property access)
propPath.some(function (pathSegment) {
obj = obj[pathSegment];
return (obj == null);
});
return obj;
}
// fixed by xxxxxx
const splitRE = /\r?\n/g
function padContent(content) {
return Array(content.split(splitRE).length).join('\n')
}

View File

@@ -0,0 +1,121 @@
module.exports = {
simple : {
echo : "^#echo[ \t]+(.*?)[ \t]*$",
exec : "^#exec[ \t]+(\\S+)[ \t]*\\((.*)\\)[ \t]*$",
include : "^(.*)#include(?!-)[ \t]+(.*?)[ \t]*$", // allow prefix characters to specify the indent level of included file
'include-static' : "^(.*)#include-static[ \t]+(.*?)[ \t]*$"
},
html : {
echo : "<!--[ \t]*#echo[ \t]+(.*?)[ \t]*(?:-->|!>)",
exec : "<!--[ \t]*#exec[ \t]+(\\S+)[ \t]*\\((.*)\\)[ \t]*(?:-->|!>)",
include : "(.*)<!--[ \t]*#include(?!-)[ \t]+(.*?)[ \t]*(?:-->|!>)",
'include-static' : "(.*)<!--[ \t]*#include-static[ \t]+(.*?)[ \t]*(?:-->|!>)",
exclude : {
start : "[ \t]*<!--[ \t]*#exclude(?:[ \t]+(.*?))?[ \t]*(?:-->|!>)(?:[ \t]*\n+)?",
end : "[ \t]*<!--[ \t]*#endexclude[ \t]*(?:-->|!>)(?:[ \t]*\n)?"
},
extend : {
start : "[ \t]*<!--[ \t]*#extend(?!able)[ \t]+(.*?)[ \t]*(?:-->|!>)(?:[ \t]*\n+)?",
end : "[ \t]*<!--[ \t]*#endextend[ \t]*(?:-->|!>)(?:[ \t]*\n)?"
},
extendable : "<!--[ \t]*#extendable[ \t]*(?:-->|!>)",
if : {
start : "[ \t]*<!--[ \t]*#(ifndef|ifdef|if)[ \t]+(.*?)[ \t]*(?:-->|!>)(?:[ \t]*\n+)?",
end : "[ \t]*<!(?:--)?[ \t]*#endif[ \t]*(?:-->|!>)(?:[ \t]*\n)?"
},
else : "[ \t]*<!(?:--)?[ \t]*#else[ \t]*(?:-->|!>)(?:[ \t]*\n)?",
foreach : {
start : "[ \t]*<!--[ \t]*#foreach[ \t]+(.*?)[ \t]*(?:-->|!>)(?:[ \t]*\n+)?",
end : "[ \t]*<!(?:--)?[ \t]*#endfor[ \t]*(?:-->|!>)(?:[ \t]*\n)?"
}
},
js : {
echo : [
"/\\*[ \t]*#echo[ \t]+(.*?)[ \t]*\\*(?:\\*|/)",
"//[ \t]*#echo[ \t]+(.*?)[ \t]*$"
],
exec : "(?://|/\\*)[ \t]*#exec[ \t]+(\\S+?)[ \t]*\\((.*)\\)[ \t]*(?:\\*(?:\\*|/))?",
include : [
"^(.*)/\\*[ \t]*#include(?!-)[ \t]+(.*?)[ \t]*\\*(?:\\*|/)",
"^(.*)//[ \t]*#include(?!-)[ \t]+(.*?)[ \t]*$"
],
'include-static': [
"^(.*)/\\*[ \t]*#include-static[ \t]+(.*?)[ \t]*\\*(?:\\*|/)",
"^(.*)//[ \t]*#include-static[ \t]+(.*?)[ \t]*$"
],
exclude : {
start : "[ \t]*(?://|/\\*)[ \t]*#exclude(?:[ \t]+([^\n*]*))?[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?",
end : "[ \t]*(?://|/\\*)[ \t]*#endexclude[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?"
},
extend : {
start : "[ \t]*(?://|/\\*)[ \t]*#extend(?!able)[ \t]+([^\n*]*)(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?",
end : "[ \t]*(?://|/\\*)[ \t]*#endextend[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?"
},
extendable : "[ \t]*(?://|/\\*)[ \t]*#extendable[ \t]*(?:\\*/)?",
if : {
start : "[ \t]*(?://|/\\*)[ \t]*#(ifndef|ifdef|if)[ \t]+([^\n*]*)(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?",
end : "[ \t]*(?://|/\\*)[ \t]*#endif[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?"
},
else : "[ \t]*(?://|/\\*)[ \t]*#else[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?",
foreach : {
start : "[ \t]*(?://|/\\*)[ \t]*#foreach[ \t]+([^\n*]*)(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?",
end : "[ \t]*(?://|/\\*)[ \t]*#endfor[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?"
}
},
coffee : {
echo : [
"###+[ \t]*#echo[ \t]+(.*?)[ \t]###",
"#+[ \t]*#echo[ \t]+(.*?)[ \t]*$"
],
exec : "#+[ \t]*#exec[ \t]+(\\S+)[ \t]*\\((.*)\\)[ \t]*$",
include : "^(.*?)#+[ \t]*#include(?!-)[ \t]+(.*?)[ \t]*$",
'include-static' : "^(.*?)#+[ \t]*#include-static[ \t]+(.*?)[ \t]*$",
exclude : {
start : "^[ \t]*#+[ \t]*#exclude(?:[ \t]+(.*?))?[ \t]*\n+",
end : "^[ \t]*#+[ \t]*#endexclude[ \t]*\n?"
},
extend : {
start : "^[ \t]*#+[ \t]*#extend(?!able)[ \t]+(.*?)\n+",
end : "^[ \t]*#+[ \t]*#endextend[ \t]*\n?"
},
extendable : "^[ \t]*#+[ \t]*#extendable[ \t]*$",
if : {
start : "^[ \t]*#+[ \t]*#(ifndef|ifdef|if)[ \t]+(.*?)[ \t]*\n+",
end : "^[ \t]*#+[ \t]*#endif[ \t]*\n?"
},
else : "^[ \t]*#+[ \t]*#else[ \t]*\n?",
foreach : {
start : "^[ \t]*#+[ \t]*#foreach[ \t]+(.*?)[ \t]*\n+",
end : "^[ \t]*#+[ \t]*#endfor[ \t]*\n?"
}
}
};
module.exports.xml = module.exports.html;
module.exports.javascript = module.exports.js;
module.exports.jsx = module.exports.js;
module.exports.json = module.exports.js;
module.exports.c = module.exports.js;
module.exports.cc = module.exports.js;
module.exports.cpp = module.exports.js;
module.exports.cs = module.exports.js;
module.exports.csharp = module.exports.js;
module.exports.java = module.exports.js;
module.exports.less = module.exports.js;
module.exports.sass = module.exports.js;
module.exports.scss = module.exports.js;
module.exports.css = module.exports.js;
module.exports.php = module.exports.js;
module.exports.ts = module.exports.js;
module.exports.tsx = module.exports.js;
module.exports.peg = module.exports.js;
module.exports.pegjs = module.exports.js;
module.exports.jade = module.exports.js;
module.exports.styl = module.exports.js;
module.exports.go = module.exports.js;
module.exports.bash = module.exports.coffee;
module.exports.shell = module.exports.coffee;
module.exports.sh = module.exports.coffee;

View File

@@ -0,0 +1,68 @@
{
"name": "preprocess",
"description": "Preprocess directives in HTML, JavaScript, etc directives based off variable context",
"version": "3.2.0",
"homepage": "https://github.com/jsoverson/preprocess",
"author": {
"name": "Jarrod Overson",
"email": "jsoverson@gmail.com",
"url": "http://jarrodoverson.com/"
},
"repository": {
"type": "git",
"url": "git://github.com/jsoverson/preprocess.git"
},
"bugs": {
"url": "https://github.com/jsoverson/preprocess/issues"
},
"licenses": [
{
"type": "Apache 2.0",
"url": "https://github.com/jsoverson/preprocess/blob/master/LICENSE"
}
],
"main": "lib/preprocess.js",
"engines": {
"node": ">= 0.10.0"
},
"scripts": {
"test": "grunt test",
"ci": "grunt ci"
},
"dependencies": {
"xregexp": "3.1.0"
},
"devDependencies": {
"chai": "^3.5.0",
"chai-spies": "^0.7.0",
"grunt": "^0.4.5",
"grunt-benchmark": "^0.3.0",
"grunt-cli": "^0.1.13",
"grunt-contrib-clean": "^1.0.0",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-jshint": "^1.0.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-coveralls": "^1.0.0",
"grunt-deps-ok": "^0.9.0",
"grunt-mocha-istanbul": "^3.0.1",
"grunt-mocha-test": "^0.12.7",
"istanbul": "^0.4.2",
"load-grunt-tasks": "^3.4.0",
"mocha": "^2.4.5",
"time-grunt": "^1.3.0",
"travis-cov": "^0.2.5"
},
"keywords": [
"directive",
"ENV",
"environment",
"ifdef",
"ifndef",
"echo",
"include",
"exclude",
"process",
"preprocess",
"pragma"
]
}