Skip to content

Compiling esm esm cannot be used directly in the bundler module #2659

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

Open
Mrxyy opened this issue Feb 28, 2023 · 5 comments · May be fixed by #2922
Open

Compiling esm esm cannot be used directly in the bundler module #2659

Mrxyy opened this issue Feb 28, 2023 · 5 comments · May be fixed by #2922
Labels

Comments

@Mrxyy
Copy link

Mrxyy commented Feb 28, 2023

Bug description

Compiling esm code cannot be used directly in the bundler module,.e.g:
image
The Object.assign cant work. Because bundle module is not the same as browser esm.module export variable just only have getter.
image
image

Steps to reproduce

  1. use binding @external("../test","test")
  2. complie esm code

image

3. I hava a Compatibility method,but i think Not good enough.

image

AssemblyScript version

^0.27.0

@Mrxyy Mrxyy added the bug label Feb 28, 2023
@CountBleck
Copy link
Member

What bundler are you using?

@Mrxyy
Copy link
Author

Mrxyy commented Feb 28, 2023

What bundler are you using?

webpack5

@CountBleck
Copy link
Member

I suppose the solution is to use Object.defineProperties() instead of Object.assign()

@Mrxyy
Copy link
Author

Mrxyy commented Feb 28, 2023

I suppose the solution is to use Object.defineProperties() instead of Object.assign()

Yes, the above code is just to ensure that the current compiled results work.

@YSandro
Copy link

YSandro commented Feb 3, 2024

Same problem.
Generated binding to esm:

const __module0 = imports["./main.js"];
const adaptedImports = {
  "./main.js": Object.assign(Object.create(__module0), {
    myFunc(x) {
      // assembly/index/my_func(i32) => bool
      return __module0.myFunc(x) ? 1 : 0;
    },
  }),
};

In Chrome and Vivaldi works fine.
if Firefox throw:

Uncaught TypeError: "myFunc" is read-only

If I edit manually to

const __module0 = imports["./main.js"];
const adaptedImports = {
  "./main.js": Object.assign({
    myFunc(x) {
      // assembly/index/my_func(i32) => bool
      return __module0.myFunc(x) ? 1 : 0;
    },
  }),
};

or to

const __module0 = imports["./main.js"];
const adaptedImports = {
  "./main.js": Object.create(__module0),
};

it works.
Also works when calling func via globalThis in env.ts:

// env
@external("env", "myFunc")
export declare function my_func(x: f32): boolean

main.ts:

// ./main.js
globalThis.myFunc = (x: number): boolean => {
  console.info('main.js myFunc()');
  return true;
}

But manual edits or calls through a global object are unnecessary work.

CountBleck added a commit to CountBleck/assemblyscript that referenced this issue May 25, 2025
Apparently some bundlers use objects with read-only properties for
imported modules. This conflicts with Object.assign even though the
object being assigned to only has read-only properties on its prototype.
Regardless, if the assignment is done before the prototype is set,
everything works just fine.

Fixes AssemblyScript#2659
@CountBleck CountBleck linked a pull request May 25, 2025 that will close this issue
2 tasks
CountBleck added a commit to CountBleck/assemblyscript that referenced this issue May 25, 2025
Apparently some bundlers use objects with read-only properties for
imported modules. This conflicts with Object.assign even though the
object being assigned to only has read-only properties on its prototype.
Regardless, if the assignment is done before the prototype is set,
everything works just fine.

Fixes AssemblyScript#2659
CountBleck added a commit to CountBleck/assemblyscript that referenced this issue May 29, 2025
Apparently some bundlers use objects with read-only properties for
imported modules. This conflicts with Object.assign even though the
object being assigned to only has read-only properties on its prototype.
Regardless, if the assignment is done before the prototype is set,
everything works just fine.

Fixes AssemblyScript#2659
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants