Does Meteor support npm packages with ES module exports?

Hi, I’ve tried to use lodash-es in Meteor project but without the success. I’m getting the following error on the client:

=> Errors prevented startup:                  
   
   While minifying app code:
   packages/minifyStdJS/plugin/minify-js.js:96:26: Babili minification error within packages/modules.js:
   node_modules/lodash-es/add.js, line 22
   
   Only one default export allowed per module.:
   
   export default add;
   
   at maybeThrowMinifyErrorBySourceFile (packages/minifyStdJS/plugin/minify-js.js:96:26)
   at files.forEach.file (packages/minifyStdJS/plugin/minify-js.js:135:9)
   at Array.forEach (<anonymous>)
   at MeteorBabelMinifier.processFilesForBundle (packages/minifyStdJS/plugin/minify-js.js:118:9)
   
   
=> Your application has errors. Waiting for file change.

or this one on the server:

W20171102-16:38:37.698(1)? (STDERR) packages/modules.js:395
W20171102-16:38:37.699(1)? (STDERR) export { default as add } from './add.js';
W20171102-16:38:37.700(1)? (STDERR) ^^^^^^
W20171102-16:38:37.700(1)? (STDERR) 
W20171102-16:38:37.701(1)? (STDERR) SyntaxError: Unexpected token export
W20171102-16:38:37.701(1)? (STDERR)     at /Users/jagi/workspace/meteor/test/.meteor/local/build/programs/server/boot.js:392:18
W20171102-16:38:37.703(1)? (STDERR)     at Array.forEach (<anonymous>)
W20171102-16:38:37.703(1)? (STDERR)     at Function._.each._.forEach (/Users/jagi/.meteor/packages/meteor-tool/.1.6.0.smovct++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20171102-16:38:37.704(1)? (STDERR)     at /Users/jagi/workspace/meteor/test/.meteor/local/build/programs/server/boot.js:220:5
W20171102-16:38:37.705(1)? (STDERR)     at /Users/jagi/workspace/meteor/test/.meteor/local/build/programs/server/boot.js:463:5
W20171102-16:38:37.706(1)? (STDERR)     at Function.run (/Users/jagi/workspace/meteor/test/.meteor/local/build/programs/server/profile.js:510:12)
W20171102-16:38:37.706(1)? (STDERR)     at /Users/jagi/workspace/meteor/test/.meteor/local/build/programs/server/boot.js:462:11

I know that Meteor is using Reify for managing imports/exports and probably I can’t use ES npm modules and I have to transpile them which is really sad.

I was also curious if Meteor does tree-shaking and I’ve just read this in a blog post:

Tracing import statements and tree-shaking are also impossible because Meteor wants to be backwards compatible with pre-1.3 versions.

So my question is, what direction is MDG heading if it goes about ES modules support?

Why I’m asking this question is because I want to create NPM module that I will use in Meteor apps but also with other build systems like Rollup or Webpack. So I was investigating this problem and trying decide what module import/export syntax (or transpilation technique) to use. When, I’m transpiling my code using Babel then such a package works great with Meteor and other build systems but tree-shaking is impossible. Of course I could use files/methods cherry-picking like it’s done in lodash but it also has its downsides, so the best option would be using pure ES modules but apparently they are not supported in Meteor. Any thoughts?

1 Like

I think you might want to show your support at this feature request: https://github.com/meteor/meteor-feature-requests/issues/6

3 Likes

@jagi what about building both commonjs and es bundles:

// rollup.config.js
import babel from 'rollup-plugin-babel';

export default {
  input: 'src/index.js',
  plugins: [
    babel({
      exclude: 'node_modules/**',
      runtimeHelpers: true
    })
  ],
  output: [
    { file: 'dist/index.cjs.js', format: 'cjs' },
    { file: 'dist/index.es.js', format: 'es' }
  ]
};

then rollup or webpack should be able to tree-shake using the es bundle. Not sure how to exclude the es bundle from the meteor bundle then though… :thinking:

2 Likes

@maxfi you know, there are ways to solve this problem like building several bundles and it will work in Meteor. However, the question was more about the future and direction in which MDG is heading. Having one unified way of creating packages would make everyone’s life easier. I’m sure that all the developer will be using ES modules in their packages and project really soon.

1 Like

Yeah, would be great if meteor could handle this automatically. :grinning:

1 Like