diff --git a/packages/uni-cli-shared/lib/preprocess/lib/preprocess.js b/packages/uni-cli-shared/lib/preprocess/lib/preprocess.js index b3eb9491de04611fd0b1f8bbcc94d2766ff8ba34..17f61a2cc24096e2d0ff609b358fcbbadcc01812 100755 --- a/packages/uni-cli-shared/lib/preprocess/lib/preprocess.js +++ b/packages/uni-cli-shared/lib/preprocess/lib/preprocess.js @@ -7,58 +7,54 @@ * Licensed under the Apache 2.0 license. */ -'use strict' +'use strict'; -exports.preprocess = preprocess -exports.preprocessFile = preprocessFile -exports.preprocessFileSync = preprocessFileSync +exports.preprocess = preprocess; +exports.preprocessFile = preprocessFile; +exports.preprocessFileSync = preprocessFileSync; -var path = require('path') - -var fs = require('fs') - -var os = require('os') - -var delim = require('./regexrules') - -var XRegExp = require('xregexp') +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) - }) + 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 + options = getOptionsForFile(srcFile, options); + context.src = srcFile; - var data = fs.readFileSync(srcFile) - var parsed = preprocess(data, context, options) - return fs.writeFileSync(destFile, parsed) + 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) + options = options || {}; + options.srcDir = options.srcDir || path.dirname(srcFile); + options.type = options.type || getExtension(srcFile); - return options + return options; } function getExtension(filename) { - var ext = path.extname(filename || '').split('.') - return ext[ext.length - 1] + var ext = path.extname(filename||'').split('.'); + return ext[ext.length - 1]; } function preprocess(src, context, typeOrOptions) { - src = src.toString() - context = context || process.env + src = src.toString(); + context = context || process.env; // default values var options = { @@ -66,440 +62,372 @@ function preprocess(src, context, typeOrOptions) { 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 (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 + 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) + context = copy(context); - return preprocessor(src, context, options) + return preprocessor(src, context, options); } function preprocessor(src, context, opts, noRestoreEol) { - src = normalizeEol(src) + src = normalizeEol(src); - var rv = src + var rv = src; - rv = replace( - rv, - opts.type.include, - processIncludeDirective.bind(null, false, context, opts) - ) + 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 - ) + 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 + return fileContents.contents; } - var extendedSource = preprocessor( - fileContents.contents, - extendedContext, - extendedOpts, - true - ).trim() + 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)) + include = include.replace(/^\n?|\n?$/g, ''); + return replace(extendedSource, opts.type.extendable, recurse(include)); } else { - return '' + 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(' ') + 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 + var contextVar = forParams[2]; + var arrString = getDeepPropFromObj(context, contextVar); + var eachArr; if (arrString.match(/\{(.*)\}/)) { - eachArr = JSON.parse(arrString) + 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') - }) + eachArr = arrString.slice(1, -1); + eachArr = eachArr.split(','); + eachArr = eachArr.map(function(arrEntry){ + return arrEntry.replace(/\s*(['"])(.*)\1\s*/, '$2'); + }); } else { - eachArr = arrString.split(',') + eachArr = arrString.split(','); } - var replaceToken = new RegExp(XRegExp.escape(forParams[0]), 'g') - var recursedInclude = recurse(include) + 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) - }, '') + return Object.keys(eachArr).reduce(function(stringBuilder, arrKey){ + var arrEntry = eachArr[arrKey]; + return stringBuilder + recursedInclude.replace(replaceToken, arrEntry); + }, ''); } else { - return '' + 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) - }) + 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 - ) { - var variant = startMatches[1] - var test = (startMatches[2] || '').trim() - switch (variant) { + 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': - return testPasses(test, context) - ? padContent(startMatches.input) + - recurse(include) + - padContent(endMatches.input) - : padContent(startMatches.input + include + endMatches.input) + 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) - ? padContent(startMatches.input) + - recurse(include) + - padContent(endMatches.input) - : padContent(startMatches.input + include + endMatches.input) - // case 'ifdef': - // return typeof getDeepPropFromObj(context, test) !== 'undefined' ? (padContent(startMatches.input) + recurse(include) + padContent(endMatches.input)) : padContent(startMatches.input + include + endMatches.input) // fixed by xxxxxx - // case 'ifndef': - // return typeof getDeepPropFromObj(context, test) === 'undefined' ? (padContent(startMatches.input) + recurse(include) + padContent(endMatches.input)) : padContent(startMatches.input + include + endMatches.input) // fixed by xxxxxx + 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 + '.') + throw new Error('Unknown if variant ' + variant + '.'); } - }) + }); } - rv = replace(rv, opts.type.echo, function(match, variable) { - variable = (variable || '').trim() + 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] - - return getDeepPropFromObj(context, (variable || '').trim()) - }) - - 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 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 '' + var fn = getDeepPropFromObj(context, name); + if (!fn || typeof fn !== 'function') return ''; - return fn.apply(context, params) - }) + return fn.apply(context, params); + }); - rv = replace( - rv, - opts.type['include-static'], - processIncludeDirective.bind(null, true, context, opts) - ) + rv = replace(rv, opts.type['include-static'], processIncludeDirective.bind(null, true, context, opts)); if (!noRestoreEol) { - rv = restoreEol(rv, opts.srcEol) + rv = restoreEol(rv, opts.srcEol); } - return rv -} -var splitRE = /\r?\n/g - -function padContent(content) { - return Array(content.split(splitRE).length).join('\n') + return rv; } function getEolType(source) { - var eol - var foundEolTypeCnt = 0 + var eol; + var foundEolTypeCnt = 0; if (source.indexOf('\r\n') >= 0) { - eol = '\r\n' - foundEolTypeCnt++ + eol = '\r\n'; + foundEolTypeCnt++; } if (/\r[^\n]/.test(source)) { - eol = '\r' - foundEolTypeCnt++ + eol = '\r'; + foundEolTypeCnt++; } if (/[^\r]\n/.test(source)) { - eol = '\n' - foundEolTypeCnt++ + eol = '\n'; + foundEolTypeCnt++; } if (eol == null || foundEolTypeCnt > 1) { - eol = os.EOL + eol = os.EOL; } - return 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) + source = source.replace(/(?:\r?\n)|\r/g, '\n' + indent); } else { - source = source.replace(/(?:\r\n)|\r/g, '\n') + source = source.replace(/(?:\r\n)|\r/g, '\n'); } - return source + return source; } function restoreEol(normalizedSource, originalEol) { if (originalEol !== '\n') { - normalizedSource = normalizedSource.replace(/\n/g, originalEol) + normalizedSource = normalizedSource.replace(/\n/g, originalEol); } - return normalizedSource + return normalizedSource; } function replace(rv, rule, processor) { - var isRegex = typeof rule === 'string' || rule instanceof RegExp - var isArray = Array.isArray(rule) + var isRegex = typeof rule === 'string' || rule instanceof RegExp; + var isArray = Array.isArray(rule); if (isRegex) { - rule = [new RegExp(rule, 'gmi')] + rule = [new RegExp(rule,'gmi')]; } else if (isArray) { - rule = rule.map(function(subRule) { - return new RegExp(subRule, 'gmi') - }) + rule = rule.map(function(subRule){ + return new RegExp(subRule,'gmi'); + }); } else { - throw new Error('Rule must be a String, a RegExp, or an Array.') + 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) + 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.') + 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') + 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) { + return matches.reduce(function (builder, match) { + switch(match.name) { case 'between': - builder += match.value - break + builder += match.value; + break; case 'left': - matchGroup.left = startRegex.exec(match.value) - break + matchGroup.left = startRegex.exec(match.value); + break; case 'match': - matchGroup.match = match.value - break + matchGroup.match = match.value; + break; case 'right': - matchGroup.right = endRegex.exec(match.value) - builder += processor( - matchGroup.left, - matchGroup.right, - matchGroup.match, - matchReplacePass - ) - break + matchGroup.right = endRegex.exec(match.value); + builder += processor(matchGroup.left, matchGroup.right, matchGroup.match, matchReplacePass); + break; } - return builder - }, '') + return builder; + }, ''); } - return matchReplacePass(rv) + 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 - ) + +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 + return linePrefix + fileContents.contents; } - var includedSource = fileContents.contents + var includedSource = fileContents.contents; if (isStatic) { - includedSource = fileContents.contents + includedSource = fileContents.contents; } else { - includedSource = preprocessor( - fileContents.contents, - includedContext, - includedOpts, - true - ) + includedSource = preprocessor(fileContents.contents, includedContext, includedOpts, true); } - includedSource = normalizeEol(includedSource, indent) + includedSource = normalizeEol(includedSource, indent); if (includedSource) { - return linePrefix + includedSource + return linePrefix + includedSource; } else { - return linePrefix + return linePrefix; } } function getTestTemplate(test) { - /* jshint evil:true */ - test = test || 'true' - test = test.trim() + /*jshint evil:true*/ + test = test || 'true'; + test = test.trim(); // force single equals replacement - test = test.replace(/([^=!])=([^=])/g, '$1==$2') - //fixed by xxxxxx + test = test.replace(/([^=!])=([^=])/g, '$1==$2'); + // fixed by xxxxxx test = test.replace(/-/g, '_') - /* eslint-disable no-new-func */ - return new Function( - 'context', - 'with (context||{}){ return ( ' + test + ' ); }' - ) + return new Function("context", "with (context||{}){ return ( " + test + " ); }"); } -function testPasses(test, context) { - var testFn = getTestTemplate(test) - try { - return testFn(context, getDeepPropFromObj) - } catch (e) {} - return false +function testPasses(test,context) { + var testFn = getTestTemplate(test); + return testFn(context, getDeepPropFromObj); } function getFileContents(path, failSilent, requesterPath) { try { - fs.statSync(path) + fs.statSync(path); } catch (e) { if (failSilent) { - return { - error: true, - contents: path + ' not found!' - } + 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) + 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() - } + 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 - }, {}) + 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('.') + 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]] + 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 - }) + propPath.some(function (pathSegment) { + obj = obj[pathSegment]; + return (obj == null); + }); - return obj + return obj; } +// fixed by xxxxxx +const splitRE = /\r?\n/g +function padContent(content) { + return Array(content.split(splitRE).length).join('\n') +} \ No newline at end of file diff --git a/packages/uni-cli-shared/lib/preprocess/lib/regexrules.js b/packages/uni-cli-shared/lib/preprocess/lib/regexrules.js index 01796404b29e904be7c41c9fc1b000264a33318a..2372654da03762e8682bc73bb8a41543f2aa809f 100755 --- a/packages/uni-cli-shared/lib/preprocess/lib/regexrules.js +++ b/packages/uni-cli-shared/lib/preprocess/lib/regexrules.js @@ -1,125 +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]*$' + 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: '|!>)', - exec: '|!>)', - include: '(.*)|!>)', - 'include-static': - '(.*)|!>)', - exclude: { - start: - '[ \t]*|!>)(?:[ \t]*\n+)?', - end: '[ \t]*|!>)(?:[ \t]*\n)?' + html : { + echo : "|!>)", + exec : "|!>)", + include : "(.*)|!>)", + 'include-static' : "(.*)|!>)", + exclude : { + start : "[ \t]*|!>)(?:[ \t]*\n+)?", + end : "[ \t]*|!>)(?:[ \t]*\n)?" }, - extend: { - start: - '[ \t]*|!>)(?:[ \t]*\n+)?', - end: '[ \t]*|!>)(?:[ \t]*\n)?' + extend : { + start : "[ \t]*|!>)(?:[ \t]*\n+)?", + end : "[ \t]*|!>)(?:[ \t]*\n)?" }, - extendable: '|!>)', - if: { - start: - '[ \t]*|!>)(?:[ \t]*\n+)?', - end: '[ \t]*|!>)(?:[ \t]*\n)?' + extendable : "|!>)", + if : { + start : "[ \t]*|!>)(?:[ \t]*\n+)?", + end : "[ \t]*|!>)(?:[ \t]*\n)?" }, - foreach: { - start: - '[ \t]*|!>)(?:[ \t]*\n+)?', - end: '[ \t]*|!>)(?:[ \t]*\n)?' + else : "[ \t]*|!>)(?:[ \t]*\n)?", + foreach : { + start : "[ \t]*|!>)(?:[ \t]*\n+)?", + end : "[ \t]*|!>)(?:[ \t]*\n)?" } }, - js: { - echo: [ - '/\\*[ \t]*#echo[ \t]+(.*?)[ \t]*\\*(?:\\*|/)', - '//[ \t]*#echo[ \t]+(.*?)[ \t]*$' + 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]*$' + 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]*$' + "^(.*)/\\*[ \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)?' + 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)?' + 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)?' + extendable : "[ \t]*(?://|/\\*)[ \t]*#extendable[ \t]*(?:\\*/)?", + if : { + start : "[ \t]*(?://|/\\*)[ \t]*#(ifndef|ifdef|if)[ \t]+([^\n*]*)(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?", + end : "[ \t]*(?://|/\\*)[ \t]*#endif[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?" }, - foreach: { - start: - '[ \t]*(?://|/\\*)[ \t]*#foreach[ \t]+([^\n*]*)(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?', - end: '[ \t]*(?://|/\\*)[ \t]*#endfor[ \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]*$', - 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?' + 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?' + 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?' + extendable : "^[ \t]*#+[ \t]*#extendable[ \t]*$", + if : { + start : "^[ \t]*#+[ \t]*#(ifndef|ifdef|if)[ \t]+(.*?)[ \t]*\n+", + end : "^[ \t]*#+[ \t]*#endif[ \t]*\n?" }, - foreach: { - start: '^[ \t]*#+[ \t]*@foreach[ \t]+(.*?)[ \t]*\n+', - end: '^[ \t]*#+[ \t]*@endfor[ \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.xml = module.exports.html; -module.exports.javascript = module.exports.js -module.exports.jsx = 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.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 +module.exports.bash = module.exports.coffee; +module.exports.shell = module.exports.coffee; +module.exports.sh = module.exports.coffee; diff --git a/packages/uni-cli-shared/lib/preprocess/package.json b/packages/uni-cli-shared/lib/preprocess/package.json index 7629227f9259a5820de6b81327d84d07f945a8b7..db50833e7358585798c856b339c1ca48c53face2 100755 --- a/packages/uni-cli-shared/lib/preprocess/package.json +++ b/packages/uni-cli-shared/lib/preprocess/package.json @@ -1,7 +1,7 @@ { "name": "preprocess", "description": "Preprocess directives in HTML, JavaScript, etc directives based off variable context", - "version": "3.1.0", + "version": "3.2.0", "homepage": "https://github.com/jsoverson/preprocess", "author": { "name": "Jarrod Overson", diff --git a/packages/vite-plugin-uni/src/load/main.ts b/packages/vite-plugin-uni/src/load/main.ts index 59d1400fdd4ca03b11d52f26085624ce48333f7a..68ab2a8334c50b7b660ea6f7cfad05152fee4fe2 100644 --- a/packages/vite-plugin-uni/src/load/main.ts +++ b/packages/vite-plugin-uni/src/load/main.ts @@ -19,8 +19,8 @@ export const main = { if (!pagesJsPath) { pagesJsPath = slash(path.resolve(options.inputDir, 'pages.json.js')) } - return `import '${pagesJsPath}'; -${fs.readFileSync(filename, 'utf-8').toString()} -` + return `import '${pagesJsPath}';${fs + .readFileSync(filename, 'utf-8') + .toString()}` }, }