Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set resolve for babel-loader presets

Tags:

webpack

I'm trying to make a bundle with webpack. I have a bit complex dependency: some components require other ones that are located in some /home/.../ folder.

Component (/home/.../far-away-components/base-component.js file):

import {base_component} from "base-component";
exports class MyComponent extends base_component {
   ...
}

webpack.config.js:

var path = require("path");
module.exports = {
    ...
    module: {
        loaders: [
            {
                test: /\.(js)$/,
                loader: "babel",
                query: {
                    presets: ['react', 'es2015', "stage-0"]
                }
            }
        ]
    },
    resolve: {
        alias: {
            "base-component": "/home/.../far-away-components/base-component.js"
    }
};

The problem is that far-away-components folder doesn't contain node_modules and I get an error: Couldn't find preset "react" relative to directory "/home/.../far-away-components".

How can I set resolve for babel-loader presets?

like image 424
mqklin Avatar asked Jan 03 '16 07:01

mqklin


People also ask

How do I add presets to Babel?

To make your own preset (either for local usage or to npm), you need to export a config object. It could just return an array of plugins.. Presets can contain other presets, and plugins with options. For more info, check out the babel handbook section on presets. Preset ordering is reversed (last to first).

Can I configure Babel?

Babel can be configured! Many other tools have similar configs: ESLint ( .eslintrc ), Prettier ( .prettierrc ). All Babel API options are allowed. However, if the option requires JavaScript, you may want to use a JavaScript configuration file.

How do I pass options to the Babel loader?

See the babel options. You can pass options to the loader by using the options property: This loader also supports the following loader-specific option: cacheDirectory: Default false. When set, the given directory will be used to cache the results of the loader.

How does Babel handle config sources ordered by ascending priority?

If it is a relative path, it will be resolved from cwd. Once Babel processes the input file specified by BABEL_SHOW_CONFIG_FOR, Babel will print effective configs to the console. Here is an example output: Babel will print effective config sources ordered by ascending priority. Using the example above, the priority is:


2 Answers

@mqklin: You needed this work-around using, ".map(require.resolve)", because you did not exclude /node_modules/ in your loaders. The format below should work:

    loaders: [
  {
    test: [/\.js$/, /\.es6$/],
    exclude: /node_modules/,
    loader: 'babel-loader',
    query: {
      cacheDirectory: true,
      presets: ['react', 'es2015'],
    }
  }
]
like image 34
Jim Kim Avatar answered Sep 30 '22 06:09

Jim Kim


Solution from github issue:

query: {
  presets: [
    'babel-preset-es2015',
    'babel-preset-react',
    'babel-preset-stage-0',
  ].map(require.resolve),
}
like image 115
mqklin Avatar answered Sep 30 '22 05:09

mqklin