rollup/rollup-plugin-babel

multiple entries - _rollupPluginBabelHelpers.js only contains helpers for final entry in array of entries

U-4-E-A opened this issue ยท 6 comments

Configs

rollup.config.js: -

    import babel from "rollup-plugin-babel"
    import pkg from "./package.json"
    
    const externals = Object.keys(pkg.peerDependencies || {})
    
    export default [
      {
        input: __dirname + "/src/File1.js",
        output: {
          dir: __dirname + "/lib/",
          format: "esm",
        },
        plugins: [
          babel({}),
        ],
        preserveModules: true,
        external: externals
      },
      {
        input: __dirname + "/src/File2.js",
        output: {
          dir: __dirname + "/lib/",
          format: "esm",
        },
        plugins: [
          babel({})
        ],
        preserveModules: true,
        external: externals
      },
      ...
    ]

.babelrc (inside /src): -

    {
      "presets": [
        ["@babel/preset-env", {"modules": false}],
        "@babel/preset-react"
      ],
      "plugins": [
        [
          "@babel/plugin-proposal-class-properties"
        ],
        [
          "babel-plugin-styled-components"
        ]
      ]
    }

The problem

When I include the code in my webpack compiler, I get errors like these: -

    WARNING in ./lib/File1.js 29:146-161 "export 'defineProperty' (imported as '_defineProperty') was not found in './_virtual/_rollupPluginBabelHelpers.js'

    WARNING in ./lib/File2.js 45:11-25 "export 'objectSpread2' (imported as '_objectSpread2') was not found in './_virtual/_rollupPluginBabelHelpers.js'

It seems that providing an array of multiple entries results in a _rollupPluginBabelHelpers.js populated with only the helpers required for the final entry in the array.

Can anyone help me with this? I have read all the documentation I can find and googled and I can't find an answer to this. Thanks.

What's your use case for mixing those:

  • multi configs (don't confuse that with multi entries - which you actually dont use, multi entries are about passing multiple things to input option within a single config)
  • same output directory
  • using preserveModules

Combination of those looks rather bizarre to me, so you'd have to explain what you are trying to achieve before I can advise anything.

Thanks for responding. I should have mentioned I am somewhat new to rollup. This is my first project using it.

These are model files relating to a database - they contain various constants and functions, most of which need to be exported for other functions to use. I used preserveModules as I was having an issue (similar to the current one) where I was exporting them all and any constants/functions with duplicate names across the files were being ignored except for the last one found. So if I had const TABLE_NAME = "this_table" and const TABLE_NAME = "that_table" I would end up with only 1 TABLE_NAME constant. preserveModules was the only config I could find that would stop this from happening. It might have effectively been a work-around but trying to get the config to work was bottle-necking the project and I was hoping to just get something to work, at least in the meantime.

So I have

src
----Model1.js
----Model2.js
----Model3.js

...etc. I want to export them all to a single or multiple files, with all the constants and functions remaining intact.

I hope I have explained that correctly.

It all depends on how do you plan to import things exported by those files. One of your options is to do something like this:

export default {
    input: [__dirname + "/src/File1.js", __dirname + "/src/File2.js"],
    output: {
        dir: __dirname + "/lib/",
        format: "esm",
    },
    plugins: [
        babel({}),
    ],
    external: externals
}

That looks like it worked perfectly. Thank you very much. I can stop pulling my hair out now.

One quick question... is there any way to have rollup autogenerate a main index file which exports all the files in /lib/ ? For one of the libraries I have created, I would like to be able to use the following syntax: -

import { Foo, Bar } from "@my/library"

There is no such option in rollup natively, possibly you could write a plugin to do that. Or you could play with babel macros and this https://github.com/kentcdodds/codegen.macro . Or you'd have to write those reexports by hand :p

I think the issue has been resolved, so I'm going to close this. Feel free to ask for reopening if you still have some problems