Skip to content
Permalink
Browse files
base64 deshadowing indices
  • Loading branch information
guybedford authored and lukastaegert committed May 15, 2018
1 parent 14dbe39 commit 7c34a207e705737df59bf584433a3ea05ca3c901
Showing 17 changed files with 94 additions and 5 deletions.
@@ -22,6 +22,7 @@ import sha256 from 'hash.js/lib/hash/sha/256';
import { jsExts } from './utils/relativeId';
import ExternalVariable from './ast/variables/ExternalVariable';
import { GlobalsOption, OutputOptions, RawSourceMap } from './rollup/types';
import { toBase64 } from './utils/base64';

export interface ModuleDeclarations {
exports: ChunkExports;
@@ -365,11 +366,11 @@ export default class Chunk {
const exportedVariables = Array.from(this.exports.keys());
if (mangle) {
for (const variable of exportedVariables) {
safeExportName = (++i).toString(36);
safeExportName = toBase64(++i);
// skip past leading number identifiers
if (safeExportName.charCodeAt(0) === 49 /* '1' */) {
i += 9 * 36 ** (safeExportName.length - 1);
safeExportName = i.toString(36);
i += 9 * 64 ** (safeExportName.length - 1);
safeExportName = toBase64(i);
}
this.exportNames[safeExportName] = variable;
}
@@ -451,7 +452,7 @@ export default class Chunk {
function getSafeName(name: string): string {
let safeName = name;
while (used[safeName]) {
safeName = `${name}$${used[name]++}`;
safeName = `${name}$${toBase64(used[name]++)}`;
}
used[safeName] = 1;
return safeName;
@@ -10,6 +10,7 @@ import ArgumentsVariable from '../variables/ArgumentsVariable';
import Variable from '../variables/Variable';
import { ExpressionEntity } from '../nodes/shared/Expression';
import ExternalVariable from '../variables/ExternalVariable';
import { toBase64 } from '../../utils/base64';

export default class Scope {
parent: Scope | void;
@@ -95,7 +96,7 @@ export default class Scope {
let deshadowed,
i = 1;
do {
deshadowed = `${name}$$${i++}`;
deshadowed = `${name}$$${toBase64(i++)}`;
} while (names.has(deshadowed));

declaration.setSafeName(deshadowed);
@@ -0,0 +1,11 @@
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_';

export function toBase64(num: number) {
let outStr = '';
do {
const curDigit = num % 64;
num = Math.floor(num / 64);
outStr = chars[curDigit] + outStr;
} while (num !== 0);
return outStr;
}
@@ -0,0 +1,3 @@
module.exports = {
description: 'base64 deshadowing indices'
};
@@ -0,0 +1,37 @@
var name = 5;

var name$1 = 5;

var name$2 = 5;

var name$3 = 5;

var name$4 = 5;

var name$5 = 5;

var name$6 = 5;

var name$7 = 5;

var name$8 = 5;

var name$9 = 5;

var name$a = 5;

console.log(name);
console.log(name$1);
console.log(name$2);
console.log(name$3);
console.log(name$4);
console.log(name$5);
console.log(name$6);
console.log(name$7);
console.log(name$8);
console.log(name$9);
console.log(name$a);

var name$b = 'name';

export { name$b as name };
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1 @@
export var name = 5;
@@ -0,0 +1,25 @@
import { name as a } from './dep1.js';
import { name as b } from './dep2.js';
import { name as c } from './dep3.js';
import { name as d } from './dep4.js';
import { name as e } from './dep5.js';
import { name as f } from './dep6.js';
import { name as g } from './dep7.js';
import { name as h } from './dep8.js';
import { name as i } from './dep9.js';
import { name as j } from './dep10.js';
import { name as k } from './dep11.js';

console.log(a);
console.log(b);
console.log(c);
console.log(d);
console.log(e);
console.log(f);
console.log(g);
console.log(h);
console.log(i);
console.log(j);
console.log(k);

export var name = 'name';

0 comments on commit 7c34a20

Please sign in to comment.