Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying js babel loader #3052

Closed
baseten opened this issue Nov 28, 2017 · 5 comments
Closed

Modifying js babel loader #3052

baseten opened this issue Nov 28, 2017 · 5 comments
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@baseten
Copy link

baseten commented Nov 28, 2017

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?

@baseten
Copy link
Author

baseten commented Nov 28, 2017

On looking further into this, I can provide the necessary program properties manually. This seems to be nearly what I'm after, but the exclude seems to be ignored, am I doing anything obviously wrong? That debugger is never hit at all.

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

...

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

    generateBabelConfig( program, stage )
        .then( babelConfig => {
            config
                .removeLoader( 'js' )
                .loader( 'js', {
                    test: /\.jsx?$/,
                    exclude: ( modulePath ) => {
                        debugger;

                        return /node_modules/.test( modulePath ) &&
                            !/node_modules\/(swiper|dom7)/.test( modulePath );
                    },
                    loader: 'babel',
                    query: babelConfig
                } );
        } );
};

@baseten
Copy link
Author

baseten commented 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
                } );
        } );
};

@calcsam
Copy link
Contributor

calcsam commented 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/

@calcsam calcsam closed this as completed Nov 28, 2017
@jquense
Copy link
Contributor

jquense commented Nov 28, 2017

you can edit the parts of the babel loader directly without needing to generate an entire new query here

@bytheway875
Copy link

@jquense Due to a bug introducd in gatsby 1.9.162, you actually cannot override the exclude on the js loader to load anything from node_modules because it merges your custom excludes to an array that excludes all of node_modules. Can confirm that this implementation works to resolve this issue, but it is kind of nasty.

@fk fk added the type: question or discussion Issue discussing or asking a question about Gatsby label Feb 15, 2018
@fk fk added the type: documentation An issue or pull request for improving or updating Gatsby's documentation label Feb 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation An issue or pull request for improving or updating Gatsby's documentation type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

5 participants