rewrite-imports
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -6,29 +6,28 @@ 'use strict'; | ||
function alias(key) { | ||
key = key.trim(); | ||
var name = key.split(' as '); | ||
(name.length > 1) && (key=name.shift()); | ||
return { key: key, name: name[0] }; | ||
function destruct(keys, target) { | ||
var tmp, out=[]; | ||
while (keys.length) { | ||
tmp = keys.shift().trim().split(' as '); | ||
out.push(tmp.join(':')); | ||
} | ||
return ("const { " + (out.join(', ')) + " } = " + target + ";"); | ||
} | ||
function generate(keys, dep, base, fn) { | ||
var tmp = base || (dep.split('/').pop().replace(/\W/g, '_') + '$' + num++); // uniqueness | ||
var name = base || alias(tmp).name; | ||
dep = fn + "('" + dep + "')"; | ||
var obj; | ||
var out = "const " + name + " = " + dep + ";"; | ||
if (keys.length && !base) { | ||
return destruct(keys, dep); | ||
} | ||
keys.forEach(function (key) { | ||
obj = alias(key); | ||
out += "\nconst " + (obj.name) + " = " + tmp + "." + (obj.key) + ";"; | ||
}); | ||
var out = "const " + base + " = " + dep + ";"; | ||
if (keys.length) { | ||
out += '\n' + destruct(keys, base); | ||
} | ||
return out; | ||
} | ||
var num; | ||
module.exports = function (str, fn) { | ||
num = 0; | ||
fn = fn || 'require'; | ||
@@ -35,0 +34,0 @@ return str |
@@ -6,29 +6,28 @@ 'use strict'; | ||
function alias(key) { | ||
key = key.trim(); | ||
let name = key.split(' as '); | ||
(name.length > 1) && (key=name.shift()); | ||
return { key, name: name[0] }; | ||
function destruct(keys, target) { | ||
let tmp, out=[]; | ||
while (keys.length) { | ||
tmp = keys.shift().trim().split(' as '); | ||
out.push(tmp.join(':')); | ||
} | ||
return `const { ${ out.join(', ')} } = ${target};`; | ||
} | ||
function generate(keys, dep, base, fn) { | ||
const tmp = base || (dep.split('/').pop().replace(/\W/g, '_') + '$' + num++); // uniqueness | ||
const name = base || alias(tmp).name; | ||
dep = `${fn}('${dep}')`; | ||
let obj; | ||
let out = `const ${name} = ${dep};`; | ||
if (keys.length && !base) { | ||
return destruct(keys, dep); | ||
} | ||
keys.forEach(key => { | ||
obj = alias(key); | ||
out += `\nconst ${obj.name} = ${tmp}.${obj.key};`; | ||
}); | ||
let out = `const ${base} = ${dep};`; | ||
if (keys.length) { | ||
out += '\n' + destruct(keys, base); | ||
} | ||
return out; | ||
} | ||
let num; | ||
module.exports = function (str, fn) { | ||
num = 0; | ||
fn = fn || 'require'; | ||
@@ -35,0 +34,0 @@ return str |
{ | ||
"name": "rewrite-imports", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"repository": "lukeed/rewrite-imports", | ||
@@ -5,0 +5,0 @@ "description": "Rewrite `import` statements as `require()`s; via RegExp", |
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
6434
56