没有babel.config.js文件怎么按需引入
时间: 2023-12-08 08:06:25 浏览: 75
使用 babel.min.js让es6的语法全部编译成es5的
如果没有`babel.config.js`文件,可以创建一个新的文件,并添加以下内容:
```javascript
module.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current"
}
}
],
"@babel/preset-react"
],
plugins: [
"react-hot-loader/babel",
"@babel/plugin-transform-runtime",
[
"import",
{
libraryName: "antd",
libraryDirectory: "es",
style: "css"
}
]
]
};
```
然后,通过在`webpack.config.js`文件中添加以下规则来实现按需引入:
```javascript
module.exports = {
// ...
module: {
rules: [
// ...
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: [
"react-hot-loader/babel",
"@babel/plugin-transform-runtime",
[
"import",
{
libraryName: "antd",
libraryDirectory: "es",
style: "css"
}
]
]
}
}
}
]
}
};
```
阅读全文