developit / babel-preset-modernize

Apache License 2.0
120 stars 1 forks source link

Compile time exceptions #4

Open theKashey opened 3 years ago

theKashey commented 3 years ago

Trying to apply this preset in different forms and it throws different errors.

The configuration which works:

const modernize = [
    path.resolve(preset, './plugins/transform-umd-to-cjs'),
    // Currently disabled, causes problems with non-CJS files:
    // cjs && path.resolve(preset, './plugins/transform-cjs-to-esm'),
    [path.resolve(preset, './plugins/transform-arguments'), { loose }],
    // path.resolve(preset, './plugins/transform-destructuring'),
    // path.resolve(preset, './plugins/transform-classes'),
    // path.resolve(preset, './plugins/transform-methods'),
    // too broken:
    // path.resolve(preset, './plugins/transform-mangle-identifiers'), // the only one disabled before
    path.resolve(preset, './plugins/transform-fallbacks'),
    // path.resolve(preset, './plugins/transform-iife-arrows'),
    path.resolve(preset, './plugins/transform-implicit-scope'),
    path.resolve(preset, './plugins/transform-array-spread'),
    // path.resolve(preset, './plugins/transform-template-literals'),
    // path.resolve(preset, './plugins/transform-optimize-webpack'),
    // path.resolve(preset, './plugins/transform-remove-polyfills'),
    // module && path.resolve(preset, './plugins/transform-implicit-strict')
  ].filter(Boolean);

Trying to compile on "result bundles" results in not very replicable errors (firstly in remove-polyfills), so I've ended applying this preset via webpack loader per every file. Just a few caused an error.



interesting failure found at intersection-observer@0.7.0 the code

// Exit early if we're not running in a browser.
if (typeof window !== 'object') {
  return;
}

caused an error

Module parse failed: 'return' outside of function (12:2)
You may need an appropriate loader to handle this file type.
|  */
| if (false) {
>   return;
| }

After fixing all problems (removing plugins causing errors and changing some scripts) 28kb were removed from a total 1.5mb bundle. Size comparison can be found at https://gist.github.com/theKashey/45050f6fc341e0e324ac4fbee46a5c72

developit commented 3 years ago

Thanks for doing such a thorough investigation @theKashey!

FWIW the preset is less effective when applied per-module because it can't apply cross-module knowledge when removing polyfills or helper functions. It does produce useless error stacks though, that's for sure.

The early exit issue is actually just because you're applying this to a non-module file - switching Babel-loader's options to include sourceType:'unambiguous' would fix that.

I'll try to look into the per-library errors you uncovered. Rough edges like these are why I sat on this code for so long :P