-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Scope hoisted ES6 classes fail if minified #2513
Comments
It works without |
@mischnic That's interesting, though I really don't want Babel to transpile down to ES5. So it'd be great to have this working w/ ES6 outputs too... Happy to take a look myself, just not quite sure where to start. Any pointers? |
This call here
renames the variables. It seems to miss some occurences. Minimal example: class Foo {
copy() {
return new Foo();
}
}
new Foo(); |
For some reason, the reference to the class inside of
Replacing
with scope.rename(name, newName) fixes the issue. (There was a similar issue before)
Babel works correctly: const core = require('@babel/core');
const traverse = require('@babel/traverse').default;
const code = `
class Foo {
copy() {
return new Foo();
}
}
new Foo();
`;
const { ast } = core.transformSync(code, {ast: true});
traverse(ast, {
Program(path) {
console.log(path.scope.getBinding('Foo').referencePaths.length);
}
}); |
* feature/multi-output: (28 commits) docs(examples): add troubleshooting notes fix(examples): add quote escaping in xml-converter build(examples): update tsconfig for commit-table-ssr docs(examples): update readmes to refer to wiki build instructions build: update project tpl scripts, ignore files build: update make-module script build: update bundle-module outputs, emit meta data, update .gitignore fix(transducers): update juxt re-export build: temporarily add webpack until parcel-bundler/parcel#2513 is fixed feat(examples): add package-stats charting example minor(examples): cleanup build(examples): update dev deps build(examples): enable scope hoisting in `yarn build` scripts minor: update imports in readme's / examples build: update .gitignore refactor(examples): update imports & deps of all examples fix(csp): disable __State reverse enum lookup fix(rstream-log): remove __Level reverse enum lookup, update Level (non const) fix(rstream-gestures): disable __GestureType reverse enum export fix(rstream): disable __State reverse enum lookups ...
* develop: (65 commits) test(api): fix import in mixin test build: add test-only yarn script build: fix cjs output path in bundle-module script docs(examples): add troubleshooting notes fix(examples): add quote escaping in xml-converter build(examples): update tsconfig for commit-table-ssr docs(examples): update readmes to refer to wiki build instructions build: update project tpl scripts, ignore files build: update make-module script build: update bundle-module outputs, emit meta data, update .gitignore fix(transducers): update juxt re-export build: temporarily add webpack until parcel-bundler/parcel#2513 is fixed feat(examples): add package-stats charting example minor(examples): cleanup build(examples): update dev deps build(examples): enable scope hoisting in `yarn build` scripts minor: update imports in readme's / examples build: update .gitignore refactor(examples): update imports & deps of all examples fix(csp): disable __State reverse enum lookup ...
@devongovett The babel bugs become more even more obscure: class abc {
copy() {
return new Foo();
}
}
new abc(); Remove const core = require("@babel/core");
const traverse = require("@babel/traverse").default;
const generator = require("@babel/generator").default;
const code = `
class Foo {
copy() {
return new Foo();
}
}
new Foo();
`;
const { ast } = core.transformSync(code, { ast: true });
traverse(ast, {
Program(path) {
const { scope } = path;
scope.crawl();
// parcel's rename algorithm:
// ------------------------------
let binding = scope.getBinding("Foo");
// Rename all references
for (let path of binding.referencePaths) {
if (path.node.name === "Foo") {
path.node.name = "abc";
}
}
// Rename binding identifier, and update scope.
scope.removeOwnBinding("Foo");
scope.bindings["abc"] = binding;
binding.identifier.name = "abc";
}
});
console.log(generator(ast).code); |
This is possibly fixed by #2986. |
Hi @mischnic @devongovett - thanks for this!!! Is that fix going to be released anytime soon or will be part of v2? |
🐛 bug report
I think this is related to #2041, but showing the same symptom in a slightly different context. I.e. It seems that when using scope hoisting and minification, any class method cannot refer to its own class, i.e. to create a copy of itself:
🎛 Configuration (.babelrc, package.json, cli command)
🤔 Expected Behavior
The above code should execute as expected (and DOES work if minification is disabled), but throws a runtime error due to wrong naming resolution in the minified version:
😯 Current Behavior
The relevant parts of the built, non-minified version are:
However, once minified, this turns into and the generated
$wwEC$export$Foo
does NOT get properly replaced:💁 Possible Solution
Not sure if this is an issue w/ Terser or Parcel... Right now the only workaround for some cases is to delegate to separate functions, e.g. to implement the above
.copy()
operation:🔦 Context
It's a pretty common scenario for class methods to refer to its own class by name and right now this issue prevents any package containing such cases from using scope hoisting, which is a real shame!
💻 Code Sample
See above.
🌍 Your Environment
The text was updated successfully, but these errors were encountered: