zoukankan      html  css  js  c++  java
  • antd-mobile使用报错

    在第一次使用时,按照官网的进行配置,完了报错找不到antd-mobile下面的css

    解决方法来源于 :https://github.com/ant-design/ant-design-mobile/issues/516#issuecomment-293632772

    依赖:
    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: {
                presets: ['es2015','react']
          },
    }
    
    //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',
        ],
      },

    在.babelrc文件中配置,在原文中式在 JS 哪里配置的,但是我配置的时候报错, 下面这样配置才可以使用,不解。。。,除了这里其他都是一致的,

    {
      "plugins": [
        ["import", {
          style: 'css' ,  // 'less',
          libraryName: 'antd-mobile'
        }]
      ]
    }

    我自己的配置源码:

    const webpack = require("webpack"),
        path = require('path'),
        ExtractTextPlugin = require("extract-text-webpack-plugin"),
        HtmlWebpackPlugin = require('html-webpack-plugin'),
          pxtorem = require('postcss-pxtorem');
          
    const modelPath = 'test';      //运行打包的模块
          
    const svgSpriteDirs = [
        require.resolve('antd-mobile').replace(/warn.js$/, ''), // antd-mobile 内置svg
        //path.resolve(__dirname, 'src/my-project-svg-foler'),  // 业务代码本地私有 svg 存放目录
    ];
    module.exports = {
        entry:{//入口
            index: path.resolve(__dirname, 'src/'+ modelPath +'/index.js'),
            vendor: ['babel-polyfill', 'react', 'react-dom', 'react-router'] //插件入口,合并第三方包
        },
        output:{//出口
            path: path.resolve(__dirname, 'dist/'+ modelPath),
            filename: '[name].[hash:7].js', //入口文件命名
            chunkFilename: '[name].chunk.[hash:7].js' //非入口文件命名
        },
        module: {
            loaders: [
                {
                    test: /.js$/,
                    exclude: /node_modules/,
                    loader: 'babel-loader',
                    query: {
                        presets: ['es2015','react']
                    }
                },
                {
                    test: /.css/,
                    loader: ExtractTextPlugin.extract({//css样式抽离
                        fallback: "style-loader",
                        use: "css-loader"
                    })
                },
                {
                    test: /.scss$/,
                    loader: 'style-loader!css-loader!autoprefixer-loader?{browsers:["last 2 version"]}!sass-loader'
                },
                {
                    test: /.less$/,
                    loader: 'style-loader!css-loader!autoprefixer-loader?{browsers:["last 2 version"]}!less-loader'
                },
                {
                    test: /.(png|jpg)$/,
                    loader: 'url-loader?limit=8192&name=images/[hash:8].[name].[ext]'
                },
                {
                    test: /.woff$/,
                    loader: 'url-loader?limit=100000'
                },
                {
                  test: /.svg$/,
                  loader: 'svg-sprite-loader',
                  include: svgSpriteDirs,
                }
            ]
        },
        plugins:[
            new webpack.DefinePlugin({
                'process.env': {
                    NODE_ENV: JSON.stringify(process.env.NODE_ENV)
                }
            }),
            //文件压缩
            new webpack.optimize.UglifyJsPlugin({
                output: {
                    comments: false
                },
                compress: {
                    warnings: false
                }
            }),
            //插件合并
            new webpack.optimize.CommonsChunkPlugin({
                name:"vendor",
                filename:"vendor.[hash:7].js"
            }),
            //css引入--内联
            new ExtractTextPlugin("[name].[hash:7].css"),
            //导出最终生成的入口文件html
            new HtmlWebpackPlugin({
                filename: 'index.html',//文件名
                template: 'src/'+ modelPath +'/index.html',//入口
                inject: true,
                minify: {
                    removeComments: true,
                    collapseWhitespace: true,
                    removeAttributeQuotes: true
                },
                chunksSortMode: 'dependency'
            })
        ],
        //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',
            ],
          },
        devServer: {
            host: 'localhost',
            hot:true,
            port: 8081
        }
    };

    依赖:

    {
      "name": "test",
      "version": "1.0.0",
      "description": "",
      "main": "webpack.config.js",
      "scripts": {
        "build": "set NODE_ENV=production&&webpack -p --progress --colors",
        "dev": "webpack-dev-server --devtool eval --progress --colors --hot --open"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "autoprefixer-loader": "^3.2.0",
        "babel-core": "^6.25.0",
        "babel-loader": "^7.0.0",
        "babel-plugin-import": "^1.4.0",
        "babel-plugin-transform-remove-strict-mode": "^0.0.2",
        "babel-preset-es2015": "^6.24.1",
        "babel-preset-react": "^6.16.0",
        "css-loader": "^0.28.4",
        "extract-text-webpack-plugin": "^2.1.2",
        "file-loader": "^0.11.2",
        "html-webpack-plugin": "^2.28.0",
        "less-loader": "^4.0.5",
        "node-sass": "^4.5.3",
        "react-hot-loader": "^3.0.0-beta.6",
        "redux-devtools": "^3.3.2",
        "sass-loader": "^6.0.5",
        "style-loader": "^0.18.2",
        "svg-sprite-loader": "^3.2.4",
        "url-loader": "^0.5.9",
        "webpack": "^2.6.1",
        "webpack-dev-server": "^2.4.5"
      },
      "dependencies": {
        "antd-mobile": "^1.6.3",
        "babel-polyfill": "^6.23.0",
        "react": "^15.5.4",
        "react-dom": "^15.4.2",
        "react-redux": "^5.0.2",
        "react-router": "^3.0.0",
        "redux": "^3.6.0",
        "weui": "^1.1.2",
        "whatwg-fetch": "^2.0.3"
      }
    }

    目录结构

  • 相关阅读:
    java加载类的方法1.classloader 2.class.forName()
    servlet与线程与jdbc connection的关系
    static再次深入理解
    多线程读某个共享变量有时候也要给读方法加锁
    多线程读一个全局变量要不要加锁?还是说只是当修改全局变量的时候才要加锁?
    接口耗时打印并统计
    Java从设计模式[本场比赛状态转换武器]状态分析(State)模式
    Openstack中间DVR Part1 -- 东西走向的交通处理
    写酷“大神”的公开信
    从反思谈论阵列和指针的几个问题,腾讯的笔名
  • 原文地址:https://www.cnblogs.com/bruce-gou/p/7452374.html
Copyright © 2011-2022 走看看