Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cached variables can lead to incorrect toplevel trasnpilation #165

Closed
ma2ciek opened this issue Nov 9, 2018 · 6 comments · Fixed by #166
Closed

Cached variables can lead to incorrect toplevel trasnpilation #165

ma2ciek opened this issue Nov 9, 2018 · 6 comments · Fixed by #166

Comments

@ma2ciek
Copy link

ma2ciek commented Nov 9, 2018

Bug report or Feature request?

Bug

Version (complete output of terser -V)

3.10.11

Complete CLI command or minify() options used

// Terser options
{
	toplevel: true,
	mangle: true,
	nameCache: {
		vars: {
			props: {
				$add: 'foo'
			}
		},
		props: {
			props: {}
		}
	}
}

terser input

// a.js
import { add as someAdd } from './b';

console.log( someAdd( 1, 2 ) );
// b.js
export function add( x, y ) {
	return x + y;
}

terser output or error

export function foo(n,r){return n+r}
import{add as o}from"./b";console.log(o(1,2));

Expected result

The original exported add function should stay as it is. Eventually, its name should be transformed along with all its occurrences in imports.

@ma2ciek
Copy link
Author

ma2ciek commented Nov 9, 2018

I've created a repository with this bug reproducible: https://github.com/ma2ciek/terser-bug.

@kzc
Copy link
Contributor

kzc commented Nov 9, 2018

Thanks for the detailed bug report.

Initially I thought that the nameCache was contrived and couldn't be created through normal minification, but that's not the case:

$ rm -f cache.json

$ echo 'function add(){Math.random();} add(); add();' | terser --toplevel -mc --name-cache cache.json
function n(){Math.random()}n(),n();

$ cat cache.json && echo
{"vars":{"props":{"$add":"n"}}}

$ echo 'export function add(x, y){ return x + y; }' | terser --toplevel -mc --name-cache cache.json
export function n(n,r){return n+r}

I have a fix for this.

@kzc
Copy link
Contributor

kzc commented Nov 9, 2018

Other bug variations:

$ cat cache.json && echo
{"vars":{"props":{"$add":"n"}}}

$ echo 'export class add{}' | terser --toplevel -mc --name-cache cache.json
export class n{}

$ echo 'export var add = 1;' | terser -mc --name-cache cache.json
export var n=1;

$ echo 'export let add = 1;' | terser -mc --name-cache cache.json
export let n=1;

$ echo 'export const add = 1;' | terser -mc --name-cache cache.json
export const n=1;

$ echo 'export let sub = 0, add = 1;' | terser --module -mc --name-cache cache.json
export let sub=0,n=1;

$ echo 'export let {x: add} = obj;' | terser --module -mc --name-cache cache.json
export let{x:n}=obj;

Any other failing scenarios?

@kzc
Copy link
Contributor

kzc commented Nov 9, 2018

Okay, I have a more general fix that should handle all export declaration scenarios.

@kzc
Copy link
Contributor

kzc commented Nov 10, 2018

@ma2ciek Please try #166 and report your findings.

@ma2ciek
Copy link
Author

ma2ciek commented Nov 13, 2018

Thanks for the quick fix! It works perfectly on that branch for our needs. I've tested it on our codebase and no error has occured after the minification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants