prismarine-nbt icon indicating copy to clipboard operation
prismarine-nbt copied to clipboard

minification/bundling breaks library native is not defined.

Open IanSSenne opened this issue 2 years ago • 9 comments

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

IanSSenne avatar Apr 22 '22 23:04 IanSSenne

one solution for this might be using new Function instead of eval and passing whatever is suppose to be native as a parameter.

IanSSenne avatar Apr 22 '22 23:04 IanSSenne

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.

IanSSenne avatar Apr 22 '22 23:04 IanSSenne

I'm having the same problem with rollup. I try solve it with the configuration:

export default [
    {
        // ... other
        output: {
            preserveModules: true
        },
        treeshake: false
    }
]

superx101 avatar Apr 19 '24 06:04 superx101

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

LeoCaprile avatar May 08 '24 00:05 LeoCaprile

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

extremeheat avatar May 08 '24 00:05 extremeheat

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?

LeoCaprile avatar May 08 '24 00:05 LeoCaprile

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

superx101 avatar May 08 '24 02:05 superx101

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.

extremeheat avatar May 12 '24 21:05 extremeheat

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

IanSSenne avatar May 12 '24 23:05 IanSSenne