59naga / babel-plugin-add-module-exports

【v0.2 no longer maintained】 Fix babel/babel#2212 - Follow the babel@5 behavior for babel@6
731 stars 54 forks source link

Is this plugin is compatible with Babel 7 and webpack4? #72

Open DCbryant opened 5 years ago

DCbryant commented 5 years ago

I met the problem just like that https://stackoverflow.com/questions/52646625/cannot-export-default-with-webpack-4-and-babel-7. I used the babel-plugin-add-module-exports but can not resolve it.I also set the env modules ,but webpack still had some warnings that "export 'starrySky' was not found in '../utils/starrySky', I did export it with export default xxx

bartdominiak commented 5 years ago

Yes, it's working. But you need to use require('./yourModule.js')

adityak74 commented 5 years ago

This doesn't seem to work for my .css.js files, which used to work as expected with webpack 3. I upgraded to webpack 4. and Babel 7.

@bartdominiak Can I look at your usage example, I don't know if I am missing a configuration here.

Holybasil commented 4 years ago

I met the same problem. And while webpack4+babel7

babel.config.js

module.exports = {
  presets: [["@vue/app", { modules: "commonjs" }]],
  plugins: ["add-module-exports"]
};

module.js

const a = 1
const b = 2
export default { a, b }

xxx.js

import { a } from "./module.js"    // a == 1

However, after publish module.js on npm .

xxx.js

import { a } from "module"    // a == undefined
ljharb commented 4 years ago

@Holybasil that's because named imports are not destructuring. In your case, you need to import obj from 'module'; const { a, b } = module;

Holybasil commented 4 years ago

@ljharb then what has babel-plugin-add-module-exports done? Something happens when I publish a module on npm and then import it from node_modules.

ljharb commented 4 years ago

it’s ensured that your default export is the module.exports value, instead of it being an object with a default property.

If you want the named import, you have to use named export syntax - not export default.

Holybasil commented 4 years ago

@ljharb well, I just forget to rebuild my module. Acturally, babel-plugin-add-module-exports works well and it removes .default that what exactly I want. Now

import { a } from "module"      // a==1

while module.js

const a = 1
const b = 2
export default { a, b }

My fault and thank you!😄