question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modifying js babel loader

See original GitHub issue

I’d like to modify the JS babel loader, so that I can tweak the excludes / includes and run the loader against some of my node_modules - I’m using some ES6 modules which allow this and will allow me to import only required parts of a library instead of whole bundled files.

I understand how to use modifyWebpackConfig in order to do this, but ideally I’d like to use Gatsby’s own babelConfig generator rather than having to reproduce this. The trouble is this relies on the program variable which doesn’t seem to be accessible within gatsby-node.js. Is this possible currently? Or would it be better to add some kind of configuration to allow tweaking of the include + exclude of the babel-loader?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
calcsamcommented, Nov 28, 2017

Glad you figured it out! This looks like a great example – could you add it to the docs?

https://www.gatsbyjs.org/docs/add-custom-webpack-config/

1reaction
basetencommented, Nov 28, 2017

Forgot to return the promise! This is working now, hopefully someone else will find it useful:

const generateBabelConfig = require( 'gatsby/dist/utils/babel-config' );

...

exports.modifyWebpackConfig = ( { config, stage } ) => {
    const program = {
        directory: __dirname,
        browserslist: [ '> 1%', 'last 2 versions', 'IE >= 9' ]
    };

    return generateBabelConfig( program, stage )
        .then( babelConfig => {
            config
                .removeLoader( 'js' )
                .loader( 'js', {
                    test: /\.jsx?$/,
                    exclude: ( modulePath ) => {
                        return /node_modules/.test( modulePath ) &&
                            !/node_modules\/(swiper|dom7)/.test( modulePath );
                    },
                    loader: 'babel',
                    query: babelConfig
                } );
        } );
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-loader - webpack
This package allows transpiling JavaScript files using Babel and webpack. Note: Issues with the output should be reported on the Babel Issues tracker....
Read more >
Configure Babel
Babel can be configured! Many other tools have similar configs: ESLint ( .eslintrc ), Prettier ( .prettierrc ). All Babel API options are...
Read more >
How to Webpack 5 with Babel - Setup Tutorial
Exercises: · Install a Babel Plugin via npm to your project to support an upcoming JavaScript feature · Add the Plugin to your...
Read more >
Upgrading to Babel-loader 8 from 7? What do I need to change?
All plugins are moved to @babel scope with Babel 7. To update your package.json, you need to rename all your plugins and presets...
Read more >
Advanced Features: Customizing Babel Config - Next.js
Next.js includes the next/babel preset to your app, which includes everything needed to compile React applications and server-side code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found