Skip to content

Instantly share code, notes, and snippets.

@developit
Last active March 7, 2022 12:38
Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save developit/081148d83348ebe9a1bc1ba0707e1bb8 to your computer and use it in GitHub Desktop.
Save developit/081148d83348ebe9a1bc1ba0707e1bb8 to your computer and use it in GitHub Desktop.
const MODULE_DIR = /(.*([\/\\]node_modules|\.\.)[\/\\](@[^\/\\]+[\/\\])?[^\/\\]+)([\/\\].*)?$/g;
{
loader: 'babel-loader',
test: /\.jsx?$/,
include(filepath) {
if (filepath.split(/[/\\]/).indexOf('node_modules')===-1) return true;
let pkg, manifest = path.resolve(filepath.replace(MODULE_DIR, '$1'), 'package.json');
try { pkg = JSON.parse(fs.readFileSync(manifest)); } catch (e) {}
return !!(pkg.module || pkg['jsnext:main']);
}
}
@NekR
Copy link

NekR commented Apr 22, 2017

return !!pkg && !!(pkg.module || pkg['jsnext:main']);

@DamianFekete
Copy link

Thanks.
Don't forget to import fs (const fs = require('fs');)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment