Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Fix nameCache (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Denty <samddenty@gmail.com>
  • Loading branch information
2 people authored and TrySound committed Jan 11, 2020
1 parent 9243334 commit f23a346
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 3 deletions.
29 changes: 28 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,34 @@ function terser(userOptions = {}) {

result.then(handler, handler);

return result;
return result.then(result => {
if (result.nameCache) {
let { vars, props } = userOptions.nameCache;

// only assign nameCache.vars if it was provided, and if terser produced values:
if (vars) {
const newVars = result.nameCache.vars && result.nameCache.vars.props;
if (newVars) {
vars.props = vars.props || {};
Object.assign(vars.props, newVars);
}
}

// support populating an empty nameCache object:
if (!props) {
props = userOptions.nameCache.props = {};
}

// merge updated props into original nameCache object:
const newProps = result.nameCache.props && result.nameCache.props.props;
if (newProps) {
props.props = props.props || {};
Object.assign(props.props, newProps);
}
}

return result.result
});
}
};
}
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/properties-and-locals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function recurse(count) {
if (count > 0) return recurse(count - 1);
return count;
}
const obj = {
foo: 1,
_priv: 2
};
console.log(obj, recurse(10));
5 changes: 5 additions & 0 deletions test/fixtures/properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const obj = {
foo: 1,
_priv: 2
};
console.log(obj);
118 changes: 117 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,120 @@ test("include only one chunk file by regex", async () => {
expect(chunk1.map).toBeFalsy();
expect(chunk2.code).toBe(`var chunk2 = 'chunk-2';\nconsole.log(chunk2);\n`);
expect(chunk2.map).toBeFalsy();
});
});

test("terser accepts the nameCache option", async () => {
const nameCache = {
props: {
props: {
$_priv: 'custom'
}
}
};
const bundle = await rollup({
input: "test/fixtures/properties.js",
plugins: [terser({
mangle: {
properties: {
regex: /^_/
}
},
nameCache
})]
});
const result = await bundle.generate({ format: "es" });
expect(result.output[0].code.trim()).toEqual(`console.log({foo:1,custom:2});`);
});

test("terser updates the nameCache object", async () => {
const nameCache = {
props: {
props: {
$_priv: 'f'
}
}
};
const props = nameCache.props;
const bundle = await rollup({
input: "test/fixtures/properties.js",
plugins: [terser({
mangle: {
properties: {
regex: /./
}
},
nameCache
})]
});
const result = await bundle.generate({ format: "es" });
expect(result.output[0].code.trim()).toEqual(`console.log({o:1,f:2});`);
expect(nameCache.props).toBe(props);
expect(nameCache).toEqual({
props: {
props: {
$_priv: 'f',
$foo: 'o'
}
}
});
});

test("omits populates an empty nameCache object", async () => {
const nameCache = {};
const bundle = await rollup({
input: "test/fixtures/properties-and-locals.js",
plugins: [terser({
mangle: {
properties: {
regex: /./
}
},
nameCache
})]
});
const result = await bundle.generate({ format: "es" });
expect(result.output[0].code.trim()).toEqual(`console.log({o:1,i:2},function o(n){return n>0?o(n-1):n}(10));`);
expect(nameCache).toEqual({
props: {
props: {
$_priv: 'i',
$foo: 'o'
}
}
});
});

// Note: nameCache.vars never gets populated, but this is a Terser issue.
// Here we're just testing that an empty vars object doesn't get added to nameCache if it wasn't there previously.
test("terser preserve vars in nameCache when provided", async () => {
const nameCache = {
vars: {
props: {}
}
};
const bundle = await rollup({
input: "test/fixtures/properties-and-locals.js",
plugins: [terser({
mangle: {
properties: {
regex: /./
}
},
nameCache
})]
});
const result = await bundle.generate({ format: "es" });
expect(result.output[0].code.trim()).toEqual(`console.log({o:1,i:2},function o(n){return n>0?o(n-1):n}(10));`);
expect(nameCache).toEqual({
props: {
props: {
$_priv: 'i',
$foo: 'o'
}
},
vars: {
props: {}
}
});
});

2 changes: 1 addition & 1 deletion transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const transform = (code, optionsString) => {
if (result.error) {
throw result.error;
} else {
return result;
return { result, nameCache: options.nameCache };
}
};

Expand Down

0 comments on commit f23a346

Please sign in to comment.