rollup-plugin-re
Advanced tools
Comparing version 1.0.2 to 1.0.3
/*! | ||
* rollup-plugin-re v1.0.2 | ||
* rollup-plugin-re v1.0.3 | ||
* (c) 2017 jetiny 86287344@qq.com | ||
@@ -12,3 +12,9 @@ * Release under the MIT License. | ||
var MagicString = _interopDefault(require('magic-string')); | ||
var path = require('path'); | ||
var fs = _interopDefault(require('fs')); | ||
function isRegExp (re) { | ||
return Object.prototype.toString.call(re) === '[object RegExp]' | ||
} | ||
function replace (options) { | ||
@@ -23,3 +29,17 @@ if ( options === void 0 ) options = {}; | ||
patterns.forEach(function (it) { | ||
if (Object.prototype.toString.call(it.test) === '[object RegExp]') { | ||
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 (typeof it.match === 'string') { | ||
it.matcher = rollupPluginutils.createFilter(it.match); | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true; | ||
@@ -29,2 +49,3 @@ } else if (typeof it.test === 'string') { | ||
} | ||
// replace | ||
if (typeof it.replace === 'string') { | ||
@@ -35,3 +56,19 @@ it.replaceIsString = true; | ||
} | ||
it.filter = rollupPluginutils.createFilter(it.include, it.exclude); | ||
// content by file | ||
if (typeof it.file === 'string') { | ||
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 (typeof it.text === 'string') { | ||
it.replaceContent = function (res) { | ||
res.content = it.text; | ||
}; | ||
} | ||
contents.push(it); | ||
@@ -56,5 +93,29 @@ }); | ||
} | ||
if (pattern.match && !pattern.match.test(id)) { | ||
if (pattern.matcher && !pattern.matcher(id)) { | ||
return | ||
} | ||
// replace content | ||
if (pattern.replaceContent) { | ||
var res = { | ||
id: id, | ||
code: code, | ||
magicString: magicString | ||
}; | ||
pattern.replaceContent(res); | ||
if (res.content && res.content !== code) { | ||
hasReplacements = true; | ||
magicString = new MagicString(res.content); | ||
code = res.content; | ||
} | ||
} | ||
// transform | ||
if (pattern.transform) { | ||
var newCode = pattern.transform(code, id); | ||
if (newCode !== code) { | ||
hasReplacements = true; | ||
magicString = new MagicString(newCode); | ||
code = newCode; | ||
} | ||
} | ||
// test & replace | ||
if (pattern.testIsRegexp) { | ||
@@ -61,0 +122,0 @@ var match = pattern.test.exec(code); |
/*! | ||
* rollup-plugin-re v1.0.2 | ||
* rollup-plugin-re v1.0.3 | ||
* (c) 2017 jetiny 86287344@qq.com | ||
@@ -8,3 +8,9 @@ * Release under the MIT License. | ||
import MagicString from 'magic-string'; | ||
import { resolve } from 'path'; | ||
import fs from 'fs'; | ||
function isRegExp (re) { | ||
return Object.prototype.toString.call(re) === '[object RegExp]' | ||
} | ||
function replace (options) { | ||
@@ -19,3 +25,17 @@ if ( options === void 0 ) options = {}; | ||
patterns.forEach(function (it) { | ||
if (Object.prototype.toString.call(it.test) === '[object RegExp]') { | ||
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 (typeof it.match === 'string') { | ||
it.matcher = createFilter(it.match); | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true; | ||
@@ -25,2 +45,3 @@ } else if (typeof it.test === 'string') { | ||
} | ||
// replace | ||
if (typeof it.replace === 'string') { | ||
@@ -31,3 +52,19 @@ it.replaceIsString = true; | ||
} | ||
it.filter = createFilter(it.include, it.exclude); | ||
// content by file | ||
if (typeof it.file === 'string') { | ||
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 (typeof it.text === 'string') { | ||
it.replaceContent = function (res) { | ||
res.content = it.text; | ||
}; | ||
} | ||
contents.push(it); | ||
@@ -52,5 +89,29 @@ }); | ||
} | ||
if (pattern.match && !pattern.match.test(id)) { | ||
if (pattern.matcher && !pattern.matcher(id)) { | ||
return | ||
} | ||
// replace content | ||
if (pattern.replaceContent) { | ||
var res = { | ||
id: id, | ||
code: code, | ||
magicString: magicString | ||
}; | ||
pattern.replaceContent(res); | ||
if (res.content && res.content !== code) { | ||
hasReplacements = true; | ||
magicString = new MagicString(res.content); | ||
code = res.content; | ||
} | ||
} | ||
// transform | ||
if (pattern.transform) { | ||
var newCode = pattern.transform(code, id); | ||
if (newCode !== code) { | ||
hasReplacements = true; | ||
magicString = new MagicString(newCode); | ||
code = newCode; | ||
} | ||
} | ||
// test & replace | ||
if (pattern.testIsRegexp) { | ||
@@ -57,0 +118,0 @@ var match = pattern.test.exec(code); |
{ | ||
"name": "rollup-plugin-re", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "rollup replace plugin", | ||
@@ -5,0 +5,0 @@ "main": "dist/rollup-plugin-re.cjs.js", |
@@ -5,3 +5,3 @@ [![Build Status](https://travis-ci.org/jetiny/rollup-plugin-re.svg?branch=master)](https://travis-ci.org/jetiny/rollup-plugin-re) | ||
Power rollup replace plugin with RegExp supported. | ||
Power rollup content transform plugin. | ||
@@ -65,5 +65,17 @@ ## Installation | ||
replace: '', | ||
}, | ||
// replace whole file content | ||
{ | ||
text: 'exports = "content"', // replace content with given text | ||
}, | ||
{ | ||
file: './replace.js', // replace with given relative file | ||
}, | ||
{ | ||
transform (code, id) { // replace by function | ||
return `'use strict';\n${code}` | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
``` |
import { createFilter } from 'rollup-pluginutils' | ||
import MagicString from 'magic-string' | ||
import {resolve} from 'path' | ||
import fs from 'fs' | ||
function isRegExp (re) { | ||
return Object.prototype.toString.call(re) === '[object RegExp]' | ||
} | ||
export default function replace (options = {}) { | ||
@@ -11,3 +17,17 @@ const filter = createFilter(options.include, options.exclude) | ||
patterns.forEach((it) => { | ||
if (Object.prototype.toString.call(it.test) === '[object RegExp]') { | ||
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 (typeof it.match === 'string') { | ||
it.matcher = createFilter(it.match) | ||
} | ||
// test | ||
if (isRegExp(it.test)) { | ||
it.testIsRegexp = true | ||
@@ -17,2 +37,3 @@ } else if (typeof it.test === 'string') { | ||
} | ||
// replace | ||
if (typeof it.replace === 'string') { | ||
@@ -23,3 +44,19 @@ it.replaceIsString = true | ||
} | ||
it.filter = createFilter(it.include, it.exclude) | ||
// content by file | ||
if (typeof it.file === 'string') { | ||
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 (typeof it.text === 'string') { | ||
it.replaceContent = (res) => { | ||
res.content = it.text | ||
} | ||
} | ||
contents.push(it) | ||
@@ -39,3 +76,3 @@ }) | ||
let hasReplacements = false | ||
const magicString = new MagicString(code) | ||
let magicString = new MagicString(code) | ||
contents.forEach((pattern) => { | ||
@@ -45,5 +82,29 @@ if (!pattern.filter(id)) { | ||
} | ||
if (pattern.match && !pattern.match.test(id)) { | ||
if (pattern.matcher && !pattern.matcher(id)) { | ||
return | ||
} | ||
// replace content | ||
if (pattern.replaceContent) { | ||
let res = { | ||
id, | ||
code, | ||
magicString | ||
} | ||
pattern.replaceContent(res) | ||
if (res.content && res.content !== code) { | ||
hasReplacements = true | ||
magicString = new MagicString(res.content) | ||
code = res.content | ||
} | ||
} | ||
// transform | ||
if (pattern.transform) { | ||
let newCode = pattern.transform(code, id) | ||
if (newCode !== code) { | ||
hasReplacements = true | ||
magicString = new MagicString(newCode) | ||
code = newCode | ||
} | ||
} | ||
// test & replace | ||
if (pattern.testIsRegexp) { | ||
@@ -50,0 +111,0 @@ let match = pattern.test.exec(code) |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
20436
489
80
3