Skip to content
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

BUG: Possibly tree-shaking issue(not enabled by default on v2) #7055

Closed
izelnakri opened this issue Oct 11, 2021 · 3 comments
Closed

BUG: Possibly tree-shaking issue(not enabled by default on v2) #7055

izelnakri opened this issue Oct 11, 2021 · 3 comments

Comments

@izelnakri
Copy link

izelnakri commented Oct 11, 2021

🐛 bug report

Hi there, first of all wanted to say thank you for working on such a complex project. I know people create many issues for this project but I couldnt find any specifically for the problem I encountered and I think this could be an interesting bug report & constructive feedback for the initial dev experience.

The code below could also be a nice test case for treeshaking and top-level async/await feature when parcel ships them by default, since I published the universal ORM @memoria/model on npm, it is a very flexible and intuitive, advanced/universal ORM for JS.

I followed bunch of treeshaking related issues here, it took me some time to dig down which one to look at, had found one but than again it also wasn't clear if/when it will get implemented or what is today causing the blocking, would be helpful if we perhaps shortly explain/warn in the README in the meantime.

🎛 Configuration (.babelrc, package.json, cli command)

No configuration

🤔 Expected Behavior

Program runs correctly, zero-config.

😯 Current Behavior

Given these two files:

// sketchpad.ts
// by default memoria Model uses in-memory adapter for CRUD operations:

import Model, {
  Config,
  PrimaryGeneratedColumn,
  Generated,
  PrimaryColumn,
  Column,
  BelongsTo,
  HasMany,
  CreateDateColumn,
} from "@memoria/model";

class Photo extends Model {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  href: string;

  @Column("bool")
  is_public: boolean;

  @BelongsTo((type) => User, (user) => user.photos)
  user: User;
}

class User extends Model {
  @PrimaryGeneratedColumn("increment")
  id: number;

  @Column()
  first_name: string;

  @Column()
  last_name: string;

  @CreateDateColumn()
  created_at: string;

  @Column("int")
  @Generated()
  points: number;

  @HasMany((type) => Photo, (photo) => photo.user)
  photos: Photo[];
}

let a = new User();
console.log("new User() output:");
console.log(a);

async function main() {
  try {
    await Config.resetForTests();
    // console.log(Config.Schemas[1].relations);
    let user = await User.insert({ first_name: "Izel", last_name: "Nakri" });
    console.log("User insert:");
    console.log(user);

    let updatedUser = await User.update({ id: user.id, first_name: "Moris" });

    console.log("User update:");
    console.log(updatedUser);

    let savedUser = await User.update({ id: user.id, first_name: "Mo" });
    console.log("User save:");
    console.log(savedUser);

    let foundUser = await User.find(user.id);

    console.log("foundUser:");
    console.log(foundUser);

    console.log("User.count:", await User.count());

    let deletedUser = await User.delete(user);

    console.log("deletedUser:");
    console.log(deletedUser);
    console.log("User.count:", await User.count());

    let insertedUsers = await User.insertAll([
      { first_name: "Yukihiro", last_name: "Matsumoto" },
      { first_name: "Yehuda", last_name: "Katz" },
    ]);
    console.log("insertedUsers:");
    console.log(insertedUsers);

    console.log("User.count:", await User.count());

    let foundUsers = await User.findAll();
    console.log("foundUsers:");
    console.log(foundUsers);

    let changedUsers = await User.updateAll(
      foundUsers.map((foundUser) => {
        foundUser.first_name = "Changed";
        return foundUser;
      })
    );
    console.log("changedUsers:");
    console.log(changedUsers);

    let savedUsers = await User.saveAll(
      foundUsers.map((foundUser) => {
        foundUser.first_name = "Changed2";
        return foundUser;
      })
    );
    console.log("savedUsers:");
    console.log(savedUsers);

    console.log("User.count:", await User.count());

    let allDeletedUsers = await User.deleteAll(changedUsers);
    console.log("allDeletedUsers:");
    console.log(allDeletedUsers);
    console.log("User.count:", await User.count());

    // Relationships
  } catch (error) {
    console.log("error:");
    console.log(error);
  }

  console.log("-------------- THE END ---------------");
}

main(); // without wrapping it in main(top-level async/await) caused a build error.
<!DOCTYPE html>
<html>

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    <script src="./sketchpad.ts" type="module"></script>
  </head>

  <body>
    <h1>This is index.html</h1>
  </body>
</html>

$ parcel index.html
This succeeeds in the terminal but when viewed on localhost:1234:

index.js:255 Uncaught TypeError: Cannot read properties of undefined (reading 'MongoEntityManager')
    at Object.MongoEntityManager (index.js:255)
    at esmodule-helpers.js:16
    at Array.forEach (<anonymous>)
    at Object.exports.exportAll (esmodule-helpers.js:10)
    at Object.gBE0e.reflect-metadata (index.ts:97)
    at newRequire (index.c88dcc78.js:71)
    at localRequire (index.c88dcc78.js:83)
    at Object.42dNh.typeorm (index.js:1)
    at newRequire (index.c88dcc78.js:71)
    at localRequire (index.c88dcc78.js:83)

This happens because code is not properly tree-shaked, @memoria/adapters include SQLAdapter export which probably triggers this error.

WIP @Memoria API: https://github.com/izelnakri/memoria

💁 Possible Solution

Both top-level await and treeshaking should work zero config.

🔦 Context

Provided above. Let me know if we should amend anything here, I can edit.

💻 Code Sample

Provided above.

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-rc.0
Node v16.10.0
npm/Yarn 7.14.0
Operating System Arch Linux
@devongovett
Copy link
Member

Possibly related to #7016. Can you test with the latest nightly version?

@izelnakri
Copy link
Author

I just run them on the latest nightly and it works! Thank you for quick response, would appreciate if we cut a new release candidate version soon with this.

I still had to wrap the script in an async function main() {, the top-level await transpilation for a typescript file still doesn't work. I get Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules is this intentional? What is the latest decision on top-level await for parcel currently? I'm curious because top-level await scripts work on node.js currently.

@mischnic
Copy link
Member

Follow #4028 regarding top level await

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

No branches or pull requests

3 participants