meteor-vite
meteor-vite copied to clipboard
Nested imports are not supported
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.
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.