prismarine-nbt
prismarine-nbt copied to clipboard
minification/bundling breaks library native is not defined.
I've build a small app using vite and when I run it in development it works fine but running it in production fails on load due to native not being defined. vite: https://vitejs.dev/ I have attached a small project that replicates this issue. vite-prismarine-nbt-bug1.zip
one solution for this might be using new Function instead of eval and passing whatever is suppose to be native as a parameter.
I was able to fix this by changing pnbt.js (generated using the command in the readme) by updating Compiler.compile
to the following
compile(code) {
// Local variable to provide some context to eval()
const native = this.native; // eslint-disable-line
const { PartialReadError } = require("./utils"); // eslint-disable-line
return new Function(
"native",
"PartialReadError",
"return (" + code + ")"
)(native, PartialReadError)(); // eslint-disable-line
}
```
even though this fixes it for me it may be worth looking into a longer term solution on the packages side.
I'm having the same problem with rollup. I try solve it with the configuration:
export default [
{
// ... other
output: {
preserveModules: true
},
treeshake: false
}
]
I'm having the same problem with rollup. I try solve it with the configuration:
export default [ { // ... other output: { preserveModules: true }, treeshake: false } ]
this config solved your problem? I tried but with no success
node-protodef is compiling JS and running that with eval(), so the assumption is that the local variables are being captured by the eval(). Not fully sure what's going on here but it could be that perhaps the native
var declaration is being deleted (as there are no references inside the codeblock) or it's being renamed somewhere.
Maybe doing something like a blank
Object.getOwnPropertyDescriptors({ native })
statement could fix it
node-protodef is compiling JS and running that with eval(), so the assumption is that the local variables are being captured by the eval(). Not fully sure what's going on here but it could be that perhaps the
native
var declaration is being deleted (as there are no references inside the codeblock) or it's being renamed somewhere.Maybe doing something like a blank
Object.getOwnPropertyDescriptors({ native })
statement could fix it
Can I open a PR adding this fix?
I'm having the same problem with rollup. I try solve it with the configuration:
export default [ { // ... other output: { preserveModules: true }, treeshake: false } ]
this config solved your problem? I tried but with no success
yes, this config did solved my problem
the problem I located looks like this:
foo(expression) {
const native = this.native;
eval(expression); // native is used here
}
“native” is removed by rollup's default behavior
Can I open a PR adding this fix?
You can open PR if you've tested it to work. The tree shaking functionality is in a way expected to cause problems because it's supposed to remove unused code. If this is the only place that there are problems I think it's ok to add a dummy statement (maybe even just Object(native)
to handle this.
Can I open a PR adding this fix?
You can open PR if you've tested it to work. The tree shaking functionality is in a way expected to cause problems because it's supposed to remove unused code. If this is the only place that there are problems I think it's ok to add a dummy statement (maybe even just
Object(native)
to handle this.
I don't think this is sufficient due to minification being able to change the variable name, I think the best option is to wrap the code into a function instead of eval so the values can be named as arguments, the downside of this is some work will have to be done to make sure the code generated does not depend on things other than native as they will have to be specifically included in that scope
This library should really not require any specific configuration for bundling, keeping the variable from DCE does fix it for that config maybe but it won't help on other configurations where minification is involved