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.

webpack2 ant-design-mobile error

See original GitHub issue

本地环境

  • antd-mobile 版本: “antd-mobile”: “^0.9.8”,
  • 操作系统及其版本: windows10 node: 7.0.0
  • 浏览器及其版本: chrome55

你做了什么?

import 'antd-mobile/lib/button/style';
import Button from 'antd-mobile/lib/button';

import React, {Component} from 'react'

class AntNavBar extends Component {
  render () {
    return (
    <div>
      <Button>Start</Button>
    </div>
    )
  }
}

export default AntNavBar

.babelrc

{
  "presets": ["es2015", "stage-2", "react"],
  "plugins": [["import", { "libraryName": "antd-mobile", "style": "css" }],"transform-runtime", "transform-decorators-legacy", "transform-class-properties"],
  "comments": false
}

你期待的结果是:

像官网一样正常显示

实际上的结果:

ERROR in ./~/.0.9.8@antd-mobile/lib/button/index.js
Module not found: Error: Can't resolve 'react-native' in 'D:\test\js\1112\react-redux-antd-mobile\node_modules\.0.9.8@antd-mobile\lib\button'

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:34 (8 by maintainers)

github_iconTop GitHub Comments

22reactions
paranoidjkcommented, Mar 23, 2017

@timzaak https://github.com/ant-design/ant-design-mobile/issues/1010

什么时候能兼容webpack2.X, 现在webpack 都出到2.3了。

你的说法不准确,并不是 antd-mobile 来兼容 webpack 2.x, 只是配置方式变了而已,用户选择什么构建工具我们无法控制,你要使用 webpack2,你就得自己探索它的配置方法。

6reactions
timzaakcommented, Apr 13, 2017

@deot 万分感谢!试了试,真的是按需加载。完美!!

最后总结一下:

依赖:
less  
less-loader 
svg-sprite-loader
babel-plugin-import

webpack配置文件:
//svg
const svgSpriteDirs = [
  require.resolve('antd-mobile').replace(/warn\.js$/, ''), // antd-mobile 内置svg
  //path.resolve(__dirname, 'src/my-project-svg-foler'),  // 业务代码本地私有 svg 存放目录
];
{
      test: /\.svg$/,
      loader: 'svg-sprite-loader',
      include: svgSpriteDirs,
}
//less
{
      test: /\.less$/,
      use: [{
        loader: "style-loader"
      }, {
        loader: "css-loader"
      }, {
        loader: "less-loader"
      }]
}
// babel js
{  
      test: /\.js$/, 
      loader: 'babel-loader',
      exclude: /node_modules/,
      query: {
            plugins: [["import", {
                  libraryName: "antd-mobile",
                  style: true, 
            }]]
      },
}

//resolve,解决 antd-mobile 代码查找问题
resolve: {
    mainFiles: ["index.web","index"],// 这里哦
    modules: ['app', 'node_modules', path.join(__dirname, '../node_modules')],
    extensions: [
      '.web.tsx', '.web.ts', '.web.jsx', '.web.js', '.ts', '.tsx',
      '.js',
      '.jsx',
      '.react.js',
    ],
    mainFields: [
      'browser',
      'jsnext:main',
      'main',
    ],
  },

以官方的NavBar demo测试,下面的引入方式也能按需加载
import {NavBar,Icon} from 'antd-mobile'

具体配置可参照 https://git.oschina.net/timzaak/graphql-web-mobile-starter 项目。不过配置有点复杂,不推荐。

@gooddaddy 你的问题必须看到你完整的配置才可以往下走。

Read more comments on GitHub >

github_iconTop Results From Across the Web

next+antd-mobile · Issue #4327 - GitHub
Error: next-transpile-modules - an unexpected error happened when trying to resolve "antd-mobile". Are you sure the name module you are trying ...
Read more >
Ant design mobile failed to work in react native - Stack Overflow
I will never make mistake like this one. And the solution is changing ant-mobile to ant-mobile-rn. It works for me.
Read more >
FAQ - Ant design pro v2
Ant Design Pro has built-in umi, umi uses webpack devServer to support the proxy. ... If this does not solve your problem, please...
Read more >
Customize Theme - Ant Design
We take a typical webpack.config.js file as example to customize it's less-loader options. ... which cause style option of babel-plugin-import not working.
Read more >
Customize Theme - Ant Design - A UI Design Language
If you choose other boilerplates, you can write a webpack config about less-loader ... which cause style option of babel-plugin-import not working.
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