developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.03k stars 362 forks source link

Maybe should compile `node_modules` with babel when use inline dependencies #936

Open EthianWong opened 2 years ago

EthianWong commented 2 years ago

Now microbundle is use exclude: 'node_modules/**' to create babel config, I think it's reasonable when not inline dependencies. But if use inline dependencies... this options maybe is weird?

// foo/bar.js (in node_modules)
module.exports = {a: ()=>console.log('1')}
// My module
import abc from 'foo/bar.js';
window.de = abc

export const b = () => {
  console.log('2')
}

Then add foo module to devDependencies.

Result:

window.de={a:()=>console.log("1")};var o=function(){console.log('2')};export{o as b};

If i use inline dependencies mean i hope it's part of mine module, my modules arrow function has compile (it's right) but foo modules has nothing change.

Maybe should not set exclude: 'node_modules/**' when use inline dependencies?

EthianWong commented 2 years ago

We encourage library authors not to distribute code that uses untranspiled ES6 features (other than modules) for this reason. Consumers of your library should not have to transpile your ES6 code, any more than they should have to transpile your CoffeeScript, ClojureScript or TypeScript. ------- @rollup/plugin-babel

The rollup officials not recommend use untranspiled ES6 features to the module, but some authors don't think so, such as debug. So should microbundle allow developers to choose whether to enable babel for node_modules when they use inline dependencies? Currently mcicorbundle specifies exclude: 'node_modules/**' which leaves no way for developers to change.

EthianWong commented 2 years ago

@developit @rschristian How do you feel about this?

bradfrosty commented 2 years ago

This makes a lot of sense in my opinion specifically for inlined dependencies. If the inlined dependency is not transpiled, this means the application needs to re-transpile a library built by microbundle. Wouldn't that make using non-modern mode for this library irrelevant, if it contains a mix of transpiled and untranspiled code in the bundle?

developit commented 2 years ago

FWIW I am in favor of this. My preference would be to run the "modern to legacy" portion of the JS transpilation on bundles, rather than on source files, similar to the setup described in this diagram: https://github.com/developit/optimize-plugin#how-does-this-work

iShawnWang commented 1 year ago

Same issue here, such as query-string release es6 code to npm : https://github.com/sindresorhus/query-string/issues?q=es5

aries-wlb commented 1 year ago

may I ask what's the solution for this? Meet the same issue and I want to transpile the specific package in node_modules.

rschristian commented 1 year ago

There's no built-in/automatic solution for the moment, but you can, post-build, run Babel over your bundles to remove any erroneous modern features.