Akryum / meteor-vite

MIT License
33 stars 16 forks source link

Nested imports are not supported #10

Open red-meadow opened 1 year ago

red-meadow commented 1 year ago

This code doesn't work:

if (Meteor.isServer) {
  import { xxx } from 'yyy'
  console.log(xxx)
}

It would be nice to have this feature. I got around this problem by replacing import with require() in the existing project, but AFAIK it's not the same.

Akryum commented 1 year ago

Nested imports are a non-standard feature of Meteor.

For the specific use case of Meteor.isServer and Meteor.isClient, we could replace them statically with true or false so that the relevant code and imports are tree-shaken (if it doesn't have side-effects):

import { xxx } from 'yyy'

if (Meteor.isServer) {
  console.log(xxx)
}

Another solution is dynamic imports.

if (Meteor.isServer) {
  const { xxx } = await import('yyy')
  console.log(xxx)
}
red-meadow commented 1 year ago

I tired to use dynamic imports, they worked fine in development but failed on a build step. Vite treated them as a part of a client code, created chunks and there were problems with imports inside those chunks, e. g. this import could not be resolved: import { Email } from 'meteor/email'

I replaced all dynamic imports with require() and everything seems to be working just fine. The server code in not bundled for the client.