meteor-vite icon indicating copy to clipboard operation
meteor-vite copied to clipboard

Nested imports are not supported

Open red-meadow opened this issue 2 years ago • 2 comments

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.

red-meadow avatar Sep 18 '22 22:09 red-meadow

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)
}

Akryum avatar Sep 21 '22 09:09 Akryum

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.

red-meadow avatar Feb 02 '23 22:02 red-meadow