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?
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).
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.
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.
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:
@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'],
}
}
]
Solution from github issue:
query: {
presets: [
'babel-preset-es2015',
'babel-preset-react',
'babel-preset-stage-0',
].map(require.resolve),
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With