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.

Feature request: Ability to easily include / exclude additional files for babel-loader

See original GitHub issue

The default behaviour is that all of node_modules is excluded from babel-loader:

In my case, I need to support IE11, and there is 1 package punycode in node_modules that I need to feed into babel-loader, using the same babel config used on my app JS files.

The cleanest way I could get this to work was to dig into the source, copy-paste-tweak:

// webpack.mix.js
mix.webpackConfig({
    module: {
        rules: [
            {
                test: path.resolve(__dirname, 'node_modules/punycode'),
                use: [
                    {
                        loader: 'babel-loader',
                        options: Config.babel()
                    }
                ]
            }
        ]
    }
})

While this works it felt a clunky, and that I was reaching into the internals a little bit. It got me thinking if there could be an easier way, some psudo code might look like:

// Don't pass any node_modules into babel-loader, default behaviour matching how it is now
mix.js('resources/js/app.js', 'public/js');

// Pass only "punycode" and "someotherpackage" packages files into babel-loader, ignore the rest of node_modules
mix
    .options({
        babelLoader: {
            packages: ['punycode', 'someotherpackage'],
        }
    })
    .js('resources/js/app.js', 'public/js');


// Override the default babel-loader test
mix
    .options({
        babelLoader: {
            test: /\.(jsx?|tsx?)$/,
        }
    })
    .js('resources/js/app.js', 'public/js');

I’m sure there there are many reasons why this API isn’t possible and probably needs more thought. But I think something like this could be useful as its come up at least a few times before:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
dauncommented, Jan 6, 2021

I wrote an extension to configure which npm packages get compiled by babel. Might solve your problem. See laravel-mix-transpile-node-modules.

mix.transpileNodeModules(['punycode'])
0reactions
stale[bot]commented, Jun 3, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ability to easily include / exclude additional files for babel ...
In my case, I need to support IE11, and there is 1 package punycode in node_modules that I need to feed into babel-loader,...
Read more >
Bountysource
Feature request : Ability to easily include / exclude additional files for babel-loader.
Read more >
How to include node module for Babel using Webpack
I found it helpful to use the function for exclude as I was able to add console logs within the function to check...
Read more >
babel/preset-env
@babel/preset-env takes any target environments you've specified and checks them against its mappings to compile a list of plugins and passes it to...
Read more >
TypeScript - webpack
In this guide we will learn how to integrate TypeScript with webpack. Basic Setup. First install the TypeScript compiler and loader by running:...
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