rollup-plugin-re
Advanced tools
Comparing version 1.0.4 to 1.0.5
/*! | ||
* rollup-plugin-re v1.0.4 | ||
* rollup-plugin-re v1.0.5 | ||
* (c) 2017 jetiny 86287344@qq.com | ||
@@ -15,2 +15,110 @@ * Release under the MIT License. | ||
/* | ||
{ | ||
defines: { | ||
IS_MOCK: true, | ||
} | ||
} | ||
*/ | ||
function parseDefines (defines, patterns) { | ||
if (isObject(defines)) { | ||
for (var defineName in defines) { | ||
if (!defines[defineName]) { // remove define blocks | ||
patterns.push({ | ||
test: makeDefineRegexp(defineName), | ||
replace: '' | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
/* | ||
{ | ||
replaces: { | ||
Host: `'localhost'`, // replace | ||
} | ||
} | ||
*/ | ||
function parseReplaces (replaces, patterns) { | ||
if (isObject(replaces)) { | ||
for (var replaceName in replaces) { | ||
if (replaces[replaceName]) { | ||
patterns.push({ | ||
test: replaceName, | ||
replace: replaces[replaceName] | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
/* | ||
{ | ||
patterns: [ | ||
{ | ||
include: 'String|Regexp', | ||
exclude: 'String|Regexp', | ||
match: 'String|Regexp|Function', | ||
test: 'String|RegExp', | ||
replace: 'String|Function', | ||
file: 'String', | ||
text: 'String', | ||
transform: 'Function' | ||
} | ||
] | ||
} | ||
*/ | ||
function parsePatterns (patterns, contents) { | ||
patterns.forEach(function (it) { | ||
if (it._pass) { | ||
return contents.push(it) | ||
} | ||
// filter | ||
it.filter = rollupPluginutils.createFilter(it.include, it.exclude); | ||
// match | ||
if (isFunction(it.match)) { | ||
it.matcher = it.match; | ||
} else if (isRegExp(it.match)) { | ||
it.matcher = it.match.test.bind(it.match); | ||
} else if (isString(it.match)) { | ||
it.matcher = rollupPluginutils.createFilter(it.match); | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true; | ||
} else if (isString(it.test)) { | ||
it.testIsString = true; | ||
} | ||
// replace | ||
if (isString(it.replace)) { | ||
it.replaceIsString = true; | ||
} else if (isFunction(it.replace)) { | ||
it.replaceIsFunction = true; | ||
} | ||
// content by file | ||
if (isString(it.file)) { | ||
it.replaceContent = function (res) { | ||
var file = path.resolve(res.id, '../', it.file); | ||
try { | ||
res.content = fs.readFileSync(file).toString(); | ||
} catch (err) { | ||
throw new Error('[rollup-plugin-re] can not readFile: ' + file) | ||
} | ||
}; | ||
} | ||
// text | ||
if (isString(it.text)) { | ||
it.replaceContent = function (res) { | ||
res.content = it.text; | ||
}; | ||
} | ||
contents.push(it); | ||
}); | ||
} | ||
function replace (options) { | ||
@@ -21,52 +129,7 @@ if ( options === void 0 ) options = {}; | ||
var contents = []; | ||
var patterns = options.patterns || (options.patterns = []); | ||
parseDefines(options.defines, patterns); | ||
parseReplaces(options.replaces, patterns); | ||
parsePatterns(patterns, contents); | ||
var patterns = options.patterns; | ||
if (Array.isArray(patterns)) { | ||
patterns.forEach(function (it) { | ||
if (it._pass) { | ||
return contents.push(it) | ||
} | ||
// filter | ||
it.filter = rollupPluginutils.createFilter(it.include, it.exclude); | ||
// match | ||
if (typeof it.match === 'function') { | ||
it.matcher = it.match; | ||
} else if (isRegExp(it.match)) { | ||
it.matcher = it.match.test.bind(it.match); | ||
} else if (isString(it.match)) { | ||
it.matcher = rollupPluginutils.createFilter(it.match); | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true; | ||
} else if (isString(it.test)) { | ||
it.testIsString = true; | ||
} | ||
// replace | ||
if (isString(it.replace)) { | ||
it.replaceIsString = true; | ||
} else if (typeof it.replace === 'function') { | ||
it.replaceIsFunction = true; | ||
} | ||
// content by file | ||
if (isString(it.file)) { | ||
it.replaceContent = function (res) { | ||
var file = path.resolve(res.id, '../', it.file); | ||
try { | ||
res.content = fs.readFileSync(file).toString(); | ||
} catch (err) { | ||
throw new Error('[rollup-plugin-re] can not readFile: ' + file) | ||
} | ||
}; | ||
} | ||
// text | ||
if (isString(it.text)) { | ||
it.replaceContent = function (res) { | ||
res.content = it.text; | ||
}; | ||
} | ||
contents.push(it); | ||
}); | ||
} | ||
return { | ||
@@ -105,3 +168,3 @@ name: 're', | ||
// transform | ||
if (pattern.transform) { | ||
if (isFunction(pattern.transform)) { | ||
var newCode = pattern.transform(code, id); | ||
@@ -167,2 +230,8 @@ if (isString(newCode) && newCode !== code) { | ||
var source = /\/\/\s#if\sIS_DEFINE(.*)([\s\S]*?)\/\/\s#endif/g.source; | ||
function makeDefineRegexp (text) { | ||
return new RegExp(source.replace('IS_DEFINE', text), 'g') | ||
} | ||
function isRegExp (re) { | ||
@@ -176,2 +245,10 @@ return Object.prototype.toString.call(re) === '[object RegExp]' | ||
function isFunction (val) { | ||
return typeof val === 'function' | ||
} | ||
function isObject (val) { | ||
return val !== null && Object.prototype.toString.call(val) === '[object Object]' | ||
} | ||
module.exports = replace; |
/*! | ||
* rollup-plugin-re v1.0.4 | ||
* rollup-plugin-re v1.0.5 | ||
* (c) 2017 jetiny 86287344@qq.com | ||
@@ -11,2 +11,110 @@ * Release under the MIT License. | ||
/* | ||
{ | ||
defines: { | ||
IS_MOCK: true, | ||
} | ||
} | ||
*/ | ||
function parseDefines (defines, patterns) { | ||
if (isObject(defines)) { | ||
for (var defineName in defines) { | ||
if (!defines[defineName]) { // remove define blocks | ||
patterns.push({ | ||
test: makeDefineRegexp(defineName), | ||
replace: '' | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
/* | ||
{ | ||
replaces: { | ||
Host: `'localhost'`, // replace | ||
} | ||
} | ||
*/ | ||
function parseReplaces (replaces, patterns) { | ||
if (isObject(replaces)) { | ||
for (var replaceName in replaces) { | ||
if (replaces[replaceName]) { | ||
patterns.push({ | ||
test: replaceName, | ||
replace: replaces[replaceName] | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
/* | ||
{ | ||
patterns: [ | ||
{ | ||
include: 'String|Regexp', | ||
exclude: 'String|Regexp', | ||
match: 'String|Regexp|Function', | ||
test: 'String|RegExp', | ||
replace: 'String|Function', | ||
file: 'String', | ||
text: 'String', | ||
transform: 'Function' | ||
} | ||
] | ||
} | ||
*/ | ||
function parsePatterns (patterns, contents) { | ||
patterns.forEach(function (it) { | ||
if (it._pass) { | ||
return contents.push(it) | ||
} | ||
// filter | ||
it.filter = createFilter(it.include, it.exclude); | ||
// match | ||
if (isFunction(it.match)) { | ||
it.matcher = it.match; | ||
} else if (isRegExp(it.match)) { | ||
it.matcher = it.match.test.bind(it.match); | ||
} else if (isString(it.match)) { | ||
it.matcher = createFilter(it.match); | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true; | ||
} else if (isString(it.test)) { | ||
it.testIsString = true; | ||
} | ||
// replace | ||
if (isString(it.replace)) { | ||
it.replaceIsString = true; | ||
} else if (isFunction(it.replace)) { | ||
it.replaceIsFunction = true; | ||
} | ||
// content by file | ||
if (isString(it.file)) { | ||
it.replaceContent = function (res) { | ||
var file = resolve(res.id, '../', it.file); | ||
try { | ||
res.content = fs.readFileSync(file).toString(); | ||
} catch (err) { | ||
throw new Error('[rollup-plugin-re] can not readFile: ' + file) | ||
} | ||
}; | ||
} | ||
// text | ||
if (isString(it.text)) { | ||
it.replaceContent = function (res) { | ||
res.content = it.text; | ||
}; | ||
} | ||
contents.push(it); | ||
}); | ||
} | ||
function replace (options) { | ||
@@ -17,52 +125,7 @@ if ( options === void 0 ) options = {}; | ||
var contents = []; | ||
var patterns = options.patterns || (options.patterns = []); | ||
parseDefines(options.defines, patterns); | ||
parseReplaces(options.replaces, patterns); | ||
parsePatterns(patterns, contents); | ||
var patterns = options.patterns; | ||
if (Array.isArray(patterns)) { | ||
patterns.forEach(function (it) { | ||
if (it._pass) { | ||
return contents.push(it) | ||
} | ||
// filter | ||
it.filter = createFilter(it.include, it.exclude); | ||
// match | ||
if (typeof it.match === 'function') { | ||
it.matcher = it.match; | ||
} else if (isRegExp(it.match)) { | ||
it.matcher = it.match.test.bind(it.match); | ||
} else if (isString(it.match)) { | ||
it.matcher = createFilter(it.match); | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true; | ||
} else if (isString(it.test)) { | ||
it.testIsString = true; | ||
} | ||
// replace | ||
if (isString(it.replace)) { | ||
it.replaceIsString = true; | ||
} else if (typeof it.replace === 'function') { | ||
it.replaceIsFunction = true; | ||
} | ||
// content by file | ||
if (isString(it.file)) { | ||
it.replaceContent = function (res) { | ||
var file = resolve(res.id, '../', it.file); | ||
try { | ||
res.content = fs.readFileSync(file).toString(); | ||
} catch (err) { | ||
throw new Error('[rollup-plugin-re] can not readFile: ' + file) | ||
} | ||
}; | ||
} | ||
// text | ||
if (isString(it.text)) { | ||
it.replaceContent = function (res) { | ||
res.content = it.text; | ||
}; | ||
} | ||
contents.push(it); | ||
}); | ||
} | ||
return { | ||
@@ -101,3 +164,3 @@ name: 're', | ||
// transform | ||
if (pattern.transform) { | ||
if (isFunction(pattern.transform)) { | ||
var newCode = pattern.transform(code, id); | ||
@@ -163,2 +226,8 @@ if (isString(newCode) && newCode !== code) { | ||
var source = /\/\/\s#if\sIS_DEFINE(.*)([\s\S]*?)\/\/\s#endif/g.source; | ||
function makeDefineRegexp (text) { | ||
return new RegExp(source.replace('IS_DEFINE', text), 'g') | ||
} | ||
function isRegExp (re) { | ||
@@ -172,2 +241,10 @@ return Object.prototype.toString.call(re) === '[object RegExp]' | ||
function isFunction (val) { | ||
return typeof val === 'function' | ||
} | ||
function isObject (val) { | ||
return val !== null && Object.prototype.toString.call(val) === '[object Object]' | ||
} | ||
export default replace; |
{ | ||
"name": "rollup-plugin-re", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "rollup replace plugin", | ||
@@ -5,0 +5,0 @@ "main": "dist/rollup-plugin-re.cjs.js", |
@@ -42,2 +42,58 @@ [![Build Status](https://travis-ci.org/jetiny/rollup-plugin-re.svg?branch=master)](https://travis-ci.org/jetiny/rollup-plugin-re) | ||
## Define macro pre-processor | ||
use `defines` options to remove macro blocks | ||
### Options | ||
```javascript | ||
{ | ||
defines: { | ||
IS_SKIP: false, | ||
IS_REMOVE: true, | ||
} | ||
} | ||
``` | ||
### input | ||
```javascript | ||
// #if IS_SKIP | ||
console.log('!Skip!') | ||
// #endif | ||
// #if IS_REMOVE | ||
console.log('!Remove!') | ||
// #endif | ||
``` | ||
### output | ||
```javascript | ||
// #if IS_SKIP | ||
console.log('!Skip!') | ||
// #endif | ||
``` | ||
## Replace | ||
use `replaces` options to quick replace text | ||
### Options | ||
```javascript | ||
{ | ||
replaces: { | ||
$version: "1.0.1" | ||
} | ||
} | ||
``` | ||
### input | ||
```javascript | ||
console.log('$version') | ||
``` | ||
### output | ||
```javascript | ||
console.log('1.0.1') | ||
``` | ||
## Options | ||
@@ -55,2 +111,9 @@ | ||
exclude: 'node_modules/**', | ||
defines: { | ||
IS_SKIP: false, | ||
IS_REMOVE: true, | ||
}, | ||
replaces: { | ||
$version: "1.0.1" | ||
}, | ||
patterns: [ | ||
@@ -57,0 +120,0 @@ { |
171
src/index.js
@@ -6,55 +6,118 @@ import { createFilter } from 'rollup-pluginutils' | ||
export default function replace (options = {}) { | ||
const filter = createFilter(options.include, options.exclude) | ||
let contents = [] | ||
/* | ||
{ | ||
defines: { | ||
IS_MOCK: true, | ||
} | ||
} | ||
*/ | ||
function parseDefines (defines, patterns) { | ||
if (isObject(defines)) { | ||
for (let defineName in defines) { | ||
if (!defines[defineName]) { // remove define blocks | ||
patterns.push({ | ||
test: makeDefineRegexp(defineName), | ||
replace: '' | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
const patterns = options.patterns | ||
if (Array.isArray(patterns)) { | ||
patterns.forEach((it) => { | ||
if (it._pass) { | ||
return contents.push(it) | ||
/* | ||
{ | ||
replaces: { | ||
Host: `'localhost'`, // replace | ||
} | ||
} | ||
*/ | ||
function parseReplaces (replaces, patterns) { | ||
if (isObject(replaces)) { | ||
for (let replaceName in replaces) { | ||
if (replaces[replaceName]) { | ||
patterns.push({ | ||
test: replaceName, | ||
replace: replaces[replaceName] | ||
}) | ||
} | ||
// filter | ||
it.filter = createFilter(it.include, it.exclude) | ||
// match | ||
if (typeof it.match === 'function') { | ||
it.matcher = it.match | ||
} else if (isRegExp(it.match)) { | ||
it.matcher = it.match.test.bind(it.match) | ||
} else if (isString(it.match)) { | ||
it.matcher = createFilter(it.match) | ||
} | ||
} | ||
} | ||
/* | ||
{ | ||
patterns: [ | ||
{ | ||
include: 'String|Regexp', | ||
exclude: 'String|Regexp', | ||
match: 'String|Regexp|Function', | ||
test: 'String|RegExp', | ||
replace: 'String|Function', | ||
file: 'String', | ||
text: 'String', | ||
transform: 'Function' | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true | ||
} else if (isString(it.test)) { | ||
it.testIsString = true | ||
} | ||
// replace | ||
if (isString(it.replace)) { | ||
it.replaceIsString = true | ||
} else if (typeof it.replace === 'function') { | ||
it.replaceIsFunction = true | ||
} | ||
// content by file | ||
if (isString(it.file)) { | ||
it.replaceContent = (res) => { | ||
let file = resolve(res.id, '../', it.file) | ||
try { | ||
res.content = fs.readFileSync(file).toString() | ||
} catch (err) { | ||
throw new Error('[rollup-plugin-re] can not readFile: ' + file) | ||
} | ||
] | ||
} | ||
*/ | ||
function parsePatterns (patterns, contents) { | ||
patterns.forEach((it) => { | ||
if (it._pass) { | ||
return contents.push(it) | ||
} | ||
// filter | ||
it.filter = createFilter(it.include, it.exclude) | ||
// match | ||
if (isFunction(it.match)) { | ||
it.matcher = it.match | ||
} else if (isRegExp(it.match)) { | ||
it.matcher = it.match.test.bind(it.match) | ||
} else if (isString(it.match)) { | ||
it.matcher = createFilter(it.match) | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true | ||
} else if (isString(it.test)) { | ||
it.testIsString = true | ||
} | ||
// replace | ||
if (isString(it.replace)) { | ||
it.replaceIsString = true | ||
} else if (isFunction(it.replace)) { | ||
it.replaceIsFunction = true | ||
} | ||
// content by file | ||
if (isString(it.file)) { | ||
it.replaceContent = (res) => { | ||
let file = resolve(res.id, '../', it.file) | ||
try { | ||
res.content = fs.readFileSync(file).toString() | ||
} catch (err) { | ||
throw new Error('[rollup-plugin-re] can not readFile: ' + file) | ||
} | ||
} | ||
// text | ||
if (isString(it.text)) { | ||
it.replaceContent = (res) => { | ||
res.content = it.text | ||
} | ||
} | ||
// text | ||
if (isString(it.text)) { | ||
it.replaceContent = (res) => { | ||
res.content = it.text | ||
} | ||
contents.push(it) | ||
}) | ||
} | ||
} | ||
contents.push(it) | ||
}) | ||
} | ||
export default function replace (options = {}) { | ||
const filter = createFilter(options.include, options.exclude) | ||
let contents = [] | ||
let patterns = options.patterns || (options.patterns = []) | ||
parseDefines(options.defines, patterns) | ||
parseReplaces(options.replaces, patterns) | ||
parsePatterns(patterns, contents) | ||
return { | ||
@@ -93,3 +156,3 @@ name: 're', | ||
// transform | ||
if (pattern.transform) { | ||
if (isFunction(pattern.transform)) { | ||
let newCode = pattern.transform(code, id) | ||
@@ -155,2 +218,8 @@ if (isString(newCode) && newCode !== code) { | ||
let source = /\/\/\s#if\sIS_DEFINE(.*)([\s\S]*?)\/\/\s#endif/g.source | ||
function makeDefineRegexp (text) { | ||
return new RegExp(source.replace('IS_DEFINE', text), 'g') | ||
} | ||
function isRegExp (re) { | ||
@@ -163,1 +232,9 @@ return Object.prototype.toString.call(re) === '[object RegExp]' | ||
} | ||
function isFunction (val) { | ||
return typeof val === 'function' | ||
} | ||
function isObject (val) { | ||
return val !== null && Object.prototype.toString.call(val) === '[object Object]' | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25512
699
143