Open red-meadow opened 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)
}
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.
This code doesn't work:
It would be nice to have this feature. I got around this problem by replacing
import
withrequire()
in the existing project, but AFAIK it's not the same.