GithubHelp home page GithubHelp logo

Mixing cjs and esm imports results in duplicated version of the lib because of "exports" setup in "package.json" about react-toastify HOT 2 OPEN

mishani0x0ef avatar mishani0x0ef commented on September 24, 2024
Mixing cjs and esm imports results in duplicated version of the lib because of "exports" setup in "package.json"

from react-toastify.

Comments (2)

mishani0x0ef avatar mishani0x0ef commented on September 24, 2024

Just to add, for those who faced the same issue and are looking for a solution.

If you can use esm instead of cjs - just use it. It's better, believe me 😄

If you cannot avoid cjs - as a workaround for this issue use cjs everywhere (assuming you cannot tune your bundler to workaround it in other way).

I know it's painful, but this is what currently available.

const { toast } = require('react-toastify');

Depending on your project configuration, you may also need to:

  • Disable linting error:
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { toast } = require('react-toastify');
  • Provide a new linting rule in your eslintrc.js to enforce importing react-toastify using cjs style:
{
  'no-restricted-imports': [
    'error',
    {
      name: 'react-toastify',
      message: 'Please use CommonJS importing syntax for this lib - require',
    },
  ]
}
  • Also, you may need to change other settings of your tsconfig, package.json or bundler configuration

from react-toastify.

mishani0x0ef avatar mishani0x0ef commented on September 24, 2024

Also, there is tricky workaround for webpack.

The idea is to remove exports section before compilation using a custom plugin:

const { validate } = require('schema-utils');
const path = require('path');
const fs = require('fs');

const schema = {
  type: 'object',
  properties: {
    path: {
      type: 'string',
    },
  },
};

class DualCjsEsmWorkaroundPlugin {
  static name = 'DualCjsEsmWorkaroundPlugin';

  constructor(options) {
    validate(schema, options);

    const defaultOptions = {
      path: null,
    };

    this.options = { ...defaultOptions, ...options };
  }

  apply(compiler) {
    if (!this.options.path) {
      return;
    }

    compiler.hooks.initialize.tap(DualCjsEsmWorkaroundPlugin.name, () =>
      this._removeExportsWhenPossible(),
    );
  }

  _removeExportsWhenPossible() {
    try {
      const packageJsonPath = path.join(this.options.path, 'package.json');
      const packageJson = require(packageJsonPath);
      const canRemoveExports = packageJson.module && packageJson.main && packageJson.exports;

      if (!canRemoveExports) {
        return;
      }

      const packageJsonWithoutExports = JSON.stringify(
        { ...packageJson, exports: undefined },
        null,
        2,
      );

      fs.writeFileSync(packageJsonPath, packageJsonWithoutExports);
    } catch (error) {
      console.error(
        `[${DualCjsEsmWorkaroundPlugin.name}]: error while trying to remove externals: ${error}`,
      );
    }
  }
}

// webpack.config.js

module.exports = {
  // other configs

  plugins: [
    new DualCjsEsmWorkaroundPlugin({ path: path.join(__dirname, 'node_modules/react-toastify') }),

    // other plugins
  ],
};

from react-toastify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.