bublejs / buble

https://buble.surge.sh
MIT License
869 stars 67 forks source link

Tagged templates in different files overwrite each other when executed in the same scope #239

Open papandreou opened 4 years ago

papandreou commented 4 years ago

Given a setup like this:

foo.js

function hey([str]) {
  return str;
}

function bar() {
  console.log(hey`foo`);
}

bar.js

function hey([str]) {
  return str;
}

function foo() {
  console.log(hey`bar`);
}

index.html

<script src="foo.js"></script>
<script src="bar.js"></script>
<script>
  foo();
  bar();
</script>

Running the above in a browser outputs:

foo
bar

After running it through buble --yes dangerousTaggedTemplateString sourcedir -o targetdir it outputs:

bar
bar

This happens because both transpiled files put the quasis into global variables called templateObject, which then overwrite each other. I think this is what was pointed out on the original PR here: https://github.com/bublejs/buble/pull/67#issuecomment-349340228

In general I would assume that the "compile directory" mode of buble would avoid introducing colliding identifiers in the root scope across all the files. I think using this mode to transpile a bunch of test files for execution in a browser is a very common use case? Maybe the root scope object could just be carried over to the subsequent files so the var would come out as templateObject$1 in the second file?

Babel has the same bug btw. :)