Giter Site home page Giter Site logo

Comments (8)

tbranyen avatar tbranyen commented on June 8, 2024 1

I fixed this in 1.0.0. Can you give it a try and let me know if it works as you'd expect?

from babel-plugin-transform-commonjs.

tbranyen avatar tbranyen commented on June 8, 2024

Hey @coreyfarrell good that you have a workaround using the overrides option. Do you have a proposal for how we should check if something is CJS or not? I could do require/module/exports sniffing, or check the sourceType, but perhaps what you have is fine, just expand it a bit more to:

test: [path.resolve(__dirname, '../node_modules/**')],
plugins: ['transform-commonjs']

This way all node_modules get affected. I'm open to other ideas though on how to support this feature request.

from babel-plugin-transform-commonjs.

coreyfarrell avatar coreyfarrell commented on June 8, 2024

Thanks for the response. I'm unable to expand the override test to all node_modules as most that I use from web pages are already ES modules (camelcase is currently the only exception).

I don't have a good proposal for checking if something is CJS. I think looking for module.exports or exports variables would probably be best, only inject export default module.exports; if it's found. I'll try taking a look at this when I get a chance.

from babel-plugin-transform-commonjs.

coreyfarrell avatar coreyfarrell commented on June 8, 2024

Works for me, I just moved transform-commonjs to my common plugins list and verified it no longer breaks ES modules.

from babel-plugin-transform-commonjs.

coreyfarrell avatar coreyfarrell commented on June 8, 2024

I've just found a case where this is failing to detect an ES module. I should have a test case soon.

from babel-plugin-transform-commonjs.

coreyfarrell avatar coreyfarrell commented on June 8, 2024

We could also set isCJS = false if we find any imports/exports in the Program enter. To avoid the cases where you have something like:
import { require } from 'module'

This actually would not work since isCJS starts false and is set true if any 'CJS' features are detected. Maybe state.esmExports would be needed. I'm thinking that if ESM export keyword is found this should block setup of the module variable and export default module.exports;. In that case only require() statements should be processed to replace with import statements.

from babel-plugin-transform-commonjs.

tbranyen avatar tbranyen commented on June 8, 2024

IMO if we find any clue this is an es module, we need to abort out, or make it a toggleable flag to handle requires.

from babel-plugin-transform-commonjs.

tbranyen avatar tbranyen commented on June 8, 2024

I think the logic is "good enough" for now, and will continue to get better. I'm going to close this since there's nothing else actionable at the moment.

from babel-plugin-transform-commonjs.

Related Issues (13)

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.