antd和antd-mobile样式的按需加载配置

发布于 2022-09-07 04:14:10 字数 2489 浏览 17 评论 0

在开发react+antd(antd-mobile)+webpack 的项目中想要配置antd(antd-mobile)的样式按需加载,在网上找到两种解决方式都是利用babel-plugin-import插件 配置在不同的地方:
1、.babelrc

{
  "plugins": [
    ["import", { "libraryName": "antd-mobile", "style": "css" }] // `style: true` 会加载 less 文件
  ]
}

结果就报错了
图片描述

2、babel-loader option
配置了之后根本比起作用
图片描述

不知道我这两种配置都分别错在了哪里,附上我的package.json的配置文件,麻烦大神们看下是否有冲突

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "cross-env NODE_ENV=development node build/webpack.dev.js --host 0.0.0.0",
    "start": "cross-env NODE_ENV=development node build/webpack.dev.js",
    "build": "cross-env NODE_ENV=produce node build/webpack.produce.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^7.1.1",
    "babel-core": "^6.25.0",
    "babel-eslint": "^7.1.1",
    "babel-loader": "^7.1.1",
    "babel-plugin-import": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "cross-env": "^5.0.1",
    "css-loader": "^0.28.4",
    "eslint": "^3.19.0",
    "eslint-config-standard": "^6.2.1",
    "eslint-friendly-formatter": "^2.0.7",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-html": "^2.0.0",
    "eslint-plugin-promise": "^3.4.0",
    "eslint-plugin-react": "^7.1.0",
    "eslint-plugin-standard": "^2.0.1",
    "extract-text-webpack-plugin": "^2.1.0",
    "file-loader": "^0.11.2",
    "html-webpack-plugin": "^2.29.0",
    "image-webpack-loader": "^3.3.1",
    "jsx-loader": "^0.13.2",
    "less": "^2.7.2",
    "less-loader": "^4.0.5",
    "postcss-loader": "^0.9.1",
    "precss": "^1.4.0",
    "react-hot-loader": "^1.3.1",
    "style-loader": "^0.18.2",
    "uglifyjs-webpack-plugin": "^0.4.6",
    "url-loader": "^0.5.9",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.5.1"
  },
  "dependencies": {
    "antd": "^2.12.1",
    "antd-mobile": "^2.1.8",
    "axios": "^0.16.2",
    "echarts": "^3.6.2",
    "jquery": "^3.2.1",
    "moment": "^2.20.1",
    "rc-form": "^2.2.0",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-redux": "^4.4.0",
    "react-router": "^3.0.0",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄人书 2022-09-14 04:14:10

antd, antd-mobile 一起用,这样试下:

    // .babelrc

    "plugins": [
        [
            "import", [
                {
                    "libraryName": "antd-mobile",
                    "style": true
                },
                {
                    "libraryName": "antd",
                    "style": true    
                }
            ]
        ]
    ]
夏の忆 2022-09-14 04:14:10

我的最终解决方法
图片描述
在package.json里添加的antd样式
图片描述
在main.js路由配置里引入了antd-mibile的样式

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文