[SOLVED] Bundle-visualizer shown packages - client only or everything?

Hi,

can someone explain bundle-visualizer for me? I have a method with a run function like this:

  run({ fileType }) {
    if (Meteor.isServer) {
      const getS3 = require('./getS3.server').default
      return getS3({ fileType })
    }
  },

In getS3.server.js I import aws-sdk and this is the only place where I import it in the entire app (and the only place where I import or require getS3.server.js).

However, if I run bundle-visualizer, I can see aws-sdk in the chart (which is huge).
Is this normal? How can I avoid bundling it to the client or see the packages actually delivered?

Hey, just noticed this went unanswered!

Yes, bundle-visualizer will only show packages on the client, which does mean that the aws-sdk is being shipped to the client.

The reason why is that the bundler uses static analysis to compile the bundle (ie. reads the import and require statements to see what needs to be included).

There’s a couple of solutions you can use to solve this:

  1. Put getS3.server.js inside a server/ directory. Meteor will never bundle this for the client, meaning it’s dependencies also won’t be loaded. This means require will throw on the client, but you’ve already got it behind an if (Meteor.isServer) test so all good! (This is what I personally use)

  2. Use dynamic import:

      async run({ fileType }) {
        if (Meteor.isServer) {
          const getS3 = await import('./getS3.server').default
          return getS3({ fileType })
        }
    

    Which ends up making your function call async

1 Like

Thanks for the answer. What I don’t understand is that I thought if I specify mainModule in package.json, the import behavior changes and meteor will only include the files which I explicitly import somewhere (starting from the specified entry point). So I don’t have to use the magic import folder.

Does this mean that everything outside of a server folder will be bundled for the client no matter the entry point and the import statements?

Your original thoughts are correct.
It’s just that this line require('./getS3.server') counts as an explicit import, so it does get bundled

1 Like