-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
rolling up some ES module code as a single ES module yields a very broken module... #3234
Comments
No need for adjectives like "very broken" and "horribly wrong". It's insulting to the authors of the project. By definition any bug will produce an incorrect result. Stating the facts is sufficient. If you use The input code is creating getters dynamically and Rollup appears to be unaware of that: class Parser {
constructor(name, dict, dataview) {
this.name = name;
this.length = dict.length;
this.start = dict.offset;
this.offset = 0;
this.data = dataview;
[ `getInt8`,
`getUint8`,
`getInt16`,
`getUint16`,
`getInt32`,
`getUint32`,
`getBigInt64`,
`getBigUint64`
].forEach(name => {
let fn = name.replace(/get(Big)?/,'').toLowerCase();
let increment = parseInt(name.replace(/[^\d]/g,'')) / 8;
Object.defineProperty(this, fn, {
get: () => this.getValue(name, increment)
});
});
}
...
getValue(type, increment) {
let pos = this.start + this.offset;
this.offset += increment;
try {
return this.data[type](pos);
} catch (e) {
console.error(`parser`, type, increment, this);
console.error(`parser`, this.start, this.offset);
throw e;
}
} If you diff the output without/with tree shaking you can see that it drops statements like this: @@ -1282,7 +1266,6 @@
this.flavor = p.uint32;
this.length = p.uint32;
this.numTables = p.uint16;
- p.uint16;
this.totalSfntSize = p.uint32;
this.majorVersion = p.uint16;
this.minorVersion = p.uint16;
@@ -1353,7 +1336,6 @@
this.length = p.uint32;
this.numTables = p.uint16;
- p.uint16; // why woff2 even has any reserved bytes is a complete mystery. But it does.
this.totalSfntSize = p.uint32;
this.totalCompressedSize = p.uint32; Rollup thought those statements were side-effect-free and could be safely removed. That is not the case. Arguably they should be retained if |
No offense intended, nor malice behind the post; they were words used in surprise, not anger. Thank you for the reply. |
I for one took "horribly wrong" in jest as it was followed by the O_o, and giggled when I read it. Please let us know if @kzc's suggestion works. |
Yep, with the addition of It might be worth adding a note to the "Treeshaking" section in the readme that this will probably lead to a broken rolled up file if your code makes use of |
As it so happens, even if the getters were declared in the class without
Rollup appears to assume that all getters are side effect free if tree shaking is enabled.
@lukastaegert Other than disabling tree shaking altogether, is there a setting for |
Not yet, but it might be interesting to implement as a stop-gap solution, e.g. make every property access a side-effect with |
How Do We Reproduce?
rollup --format=esm Font.js > Font.rolled.js
and edit index.html to use this rolled up script instead.Expected Behavior
Identical behaviour in steps 2/3 and steps 5/6
Actual Behavior
steps 5/6 show things having broken considerably.
I don't even know how to go about trying to reduce things here.
The text was updated successfully, but these errors were encountered: