Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External Library using Rollup.JS with Formik doesn't respect named exports

I am using Rollup to put together a shared library of form components which uses Formik as the base layer. I'm currently getting the following compile error for 'scheduler' which is used under the hood by Formik.

I've tried installing it manually as a separate npm dependency, but still get the following error.

[!] Error: 'unstable_runWithPriority' is not exported by node_modules/formik/node_modules/scheduler/index.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
node_modules/formik/dist/formik.esm.js (9:9)
 7: import toPath from 'lodash-es/toPath';
 8: import invariant from 'tiny-warning';
 9: import { unstable_runWithPriority, LowPriority } from 'scheduler';

Following the Rollup Docs: I tried using the named Exports part of the rollup.config.js e.g.:

plugins: [


    peerDepsExternal(),
    postcss({ extract: true, plugins: [autoprefixer] }),
    json({  include: 'node_modules/**' }),
    babel({ exclude: 'node_modules/**', presets: ['@babel/env', '@babel/preset-react'] }),
    localResolve(),
    resolve({dedupe: [ 'react', 'react-dom' ]}),
    commonjs({
      namedExports: {
        // left-hand side can be an absolute path, a path
        // relative to the current directory, or the name
        // of a module in node_modules
        'node_modules/formik/node_modules/scheduler/index.js': ['unstable_runWithPriority'],
        'scheduler': ['unstable_runWithPriority'],
        'node_modules/scheduler': ['unstable_runWithPriority'],
        './node_modules/scheduler': ['unstable_runWithPriority'],
        '../node_modules/formik/node_modules/scheduler/index.js': ['unstable_runWithPriority']
      }
    }),
    globals(),
    externals(),
    builtins(),
    filesize()

  ]

As you can see I've tried a few destinations / paths for good measure. Can anyone point me in the right direction as to how to get this compiling again? Or how to properly define my named export? I read a few issues around the web that suggested that the order of the plugins array could affect things, but I swapped them about a bit and I'm still at a loss.

like image 845
Squiggs. Avatar asked Nov 28 '19 09:11

Squiggs.


1 Answers

You are probably thinking no one on planet earth will ever have the same obscure error. You would be wrong. Found the solution. (It was related to order on the plugins)

plugins: [
    globals(),
    builtins(),
    externals(),
    babel({ exclude: 'node_modules/**', presets: ['@babel/env', '@babel/preset-react'] }),
    commonjs({
      namedExports: {
        // left-hand side can be an absolute path, a path
        // relative to the current directory, or the name
        // of a module in node_modules
        'node_modules/formik/node_modules/scheduler/index.js' : ['unstable_runWithPriority'],
      }
    }),
    peerDepsExternal(),
    postcss({ extract: true, plugins: [autoprefixer] }),
    json({  include: 'node_modules/**' }),
    localResolve(),
    resolve({dedupe: [ 'react', 'react-dom' ]}),
    filesize()

  ]
like image 165
Squiggs. Avatar answered Nov 20 '22 20:11

Squiggs.