pro-components icon indicating copy to clipboard operation
pro-components copied to clipboard

🐛[BUG] antd 4 中未暴露 theme,编译报错

Open yee94 opened this issue 3 years ago • 15 comments

提问前先看看:

https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way/blob/main/README-zh_CN.md

🐛 bug 描述

import { ConfigProvider as AntdConfigProvider, theme as antdTheme } from 'antd';

Antd 4 中未暴露 theme 属性,当前编译报错

© 版本信息

  • @ant-design/pro-provider @ 2.0.2

🚑 其他信息

image

yee94 avatar Sep 03 '22 09:09 yee94

这个问题太坑了。。这2天正好在弄。。。结果给卡住了

liuhuapiaoyuan avatar Sep 03 '22 18:09 liuhuapiaoyuan

这个什么时候能修复呢?等着用,谢谢

hustcer avatar Sep 05 '22 02:09 hustcer

同问

hqwlkj avatar Sep 05 '22 03:09 hqwlkj

确实有这个问题,暂时写了个脚本启动前在ES文件写入了export const theme = null;临时解决了下,不然vite下完全用不了。

xukechu avatar Sep 05 '22 03:09 xukechu

确实有这个问题,暂时写了个脚本启动前在ES文件写入了export const theme = null;临时解决了下,不然vite下完全用不了。

照你的方法处理完以后还会有新的错误 `Remix App Server started at http://localhost:3000 (http://172.16.0.232:3000) /Users/user/test-proj/node_modules/@ant-design/pro-components/es/index.js:1 export * from '@ant-design/pro-card'; ^^^^^^

SyntaxError: Unexpected token 'export' at wrapSafe (internal/modules/cjs/loader.js:1001:16) at Module._compile (internal/modules/cjs/loader.js:1049:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:14) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:92:18) at Object. (/Users/user/test-proj/app/routes/index.jsx:2:38) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) GET / 500 - - 38.846 ms`

baidan4855 avatar Sep 05 '22 10:09 baidan4855

是在antd/es/index.js文件下追加一个"export const theme = null;"

xukechu avatar Sep 06 '22 01:09 xukechu

确实有这个问题,暂时写了个脚本启动前在ES文件写入了export const theme = null;临时解决了下,不然vite下完全用不了。

照你的方法处理完以后还会有新的错误 `Remix App Server started at http://localhost:3000 (http://172.16.0.232:3000) /Users/user/test-proj/node_modules/@ant-design/pro-components/es/index.js:1 export * from '@ant-design/pro-card'; ^^^^^^

SyntaxError: Unexpected token 'export' at wrapSafe (internal/modules/cjs/loader.js:1001:16) at Module._compile (internal/modules/cjs/loader.js:1049:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) at Module.load (internal/modules/cjs/loader.js:950:32) at Function.Module._load (internal/modules/cjs/loader.js:790:14) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:92:18) at Object. (/Users/user/test-proj/app/routes/index.jsx:2:38) at Module._compile (internal/modules/cjs/loader.js:1085:14) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10) GET / 500 - - 38.846 ms`

`import path from "path"; import fs from "fs/promises";

/**

  • antd es/index.js文件打个 export const theme = null; 补丁避免pro-x组件失败
  • */ async function shim() { const esFilePath = path.resolve( process.cwd(), "node_modules/antd/es/index.js" ); try { const result = await fs.readFile(esFilePath, "utf-8"); if (!result.includes("export const theme = null")) { await fs.appendFile(esFilePath, export const theme = null;, "utf-8"); } } catch (e) { console.error(e); } }

shim(); `

xukechu avatar Sep 06 '22 01:09 xukechu

我直接把这个文件修改了下,在末尾加上这个导出,不过我是用的remix不是vite。

baidan4855 avatar Sep 06 '22 02:09 baidan4855

这个配置是添加在哪里的?

@xukechu

hqwlkj avatar Sep 06 '22 06:09 hqwlkj

这个配置是添加在哪里的?

@xukechu

node_modules/antd/es/index.js

xukechu avatar Sep 08 '22 02:09 xukechu

这个配置是添加在哪里的? @xukechu

node_modules/antd/es/index.js

没有什么作用,会导致其他问题的出现,我以前是使用 vite 一直报错,现在换成了 umi 没有报错了。

hqwlkj avatar Sep 08 '22 02:09 hqwlkj

https://github.com/ant-design/pro-components/issues/5762#issuecomment-1240310513

Chastrlove avatar Sep 08 '22 07:09 Chastrlove

import react from '@vitejs/plugin-react'
import { readFile } from 'fs/promises'
import * as path from 'path'
import { defineConfig } from 'vite'
import ignoreStylePlugin from 'vite-ignore-style'

// https://vitejs.dev/config/
export default defineConfig({
  base: '/indexed-algorithm/',
  plugins: [
    react(),
    ignoreStylePlugin({
      libraryName: 'antd'
    }),
    {
      // 升级antd 5 后删除
      name: 'add-missing-theme',
      apply: 'build',
      transform: (code, id) => {
        const reg = /antd(?:\/|\\{1,2})es(?:\/|\\{1,2})index\.js/
        if (reg.test(id)) {
          return `${code}\nexport const theme = undefined;`
        }
        return code
      }
    }
  ],
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: {
          '@root-entry-name': 'variable'
        },
        javascriptEnabled: true
      }
    }
  },
  resolve: {
    alias: [
      { find: /^~/, replacement: '' },
      {
        find: '@',
        replacement: path.resolve(__dirname, 'src')
      }
    ]
  },
  optimizeDeps: {
    esbuildOptions: {
      plugins: [
        {
        // 升级antd 5 后删除
          name: 'add-missing-theme',
          setup(build) {
            build.onLoad(
              { filter: /antd(?:\/|\\{1,2})es(?:\/|\\{1,2})index\.js/ },
              async (args) => {
                const text = await readFile(args.path, 'utf8')
                return {
                  contents: `${text}\nexport const theme = undefined;`,
                  loader: 'js'
                }
              }
            )
          }
        }
      ]
    }
  }
})

faner11 avatar Sep 09 '22 01:09 faner11

下个版本的antd 会支持一下,大家等一等

chenshuai2144 avatar Sep 09 '22 07:09 chenshuai2144

下个版本的antd 会支持一下,大家等一等

[email protected]中还是会出现这个错误,有什么临时修复方法吗

hsbtr avatar Nov 08 '22 10:11 hsbtr

关掉 babel import

chenshuai2144 avatar Jan 03 '23 08:01 chenshuai2144

下个版本的antd 会支持一下,大家等一等

哪个版本的antd可以?

Boyceman avatar Jan 11 '23 08:01 Boyceman

下个版本的antd 会支持一下,大家等一等

哪个版本的antd可以?

antd 5.x的这个问题已经没有

hsbtr avatar Jan 11 '23 08:01 hsbtr

下个版本的antd 会支持一下,大家等一等

哪个版本的antd可以?

antd 5.x的这个问题已经没有

请问4.x有没有好用的版本呢?因为升级antd牵一发动全身,不敢妄动主要是😂

Boyceman avatar Jan 11 '23 08:01 Boyceman

下个版本的antd 会支持一下,大家等一等

哪个版本的antd可以?

antd 5.x的这个问题已经没有

请问4.x有没有好用的版本呢?因为升级antd牵一发动全身,不敢妄动主要是😂 这个就不清楚了,可以尝试更到最新的4.x版本

hsbtr avatar Jan 11 '23 09:01 hsbtr