Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack 合并文件时出现警告错误: Cannot resolve 'file' or 'directory' ./locale #3204

Closed
zhangchen2397 opened this issue Sep 29, 2016 · 10 comments

Comments

@zhangchen2397
Copy link

zhangchen2397 commented Sep 29, 2016

  • antd 版本: 2.0.0
  • 操作系统及其版本: OS X 10.11.6
  • 浏览器及其版本: chrome 53.0.2785.116

引入 antd 通过 webpack 打包合并文件时,控制台出现警告:

WARNING in ./~/antd/dist/antd.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./locale in /Users/samzhang/work/monitor/node_modules/antd/dist
 @ ./~/antd/dist/antd.js 14479:17-44

倒是不影响使用,看了下原因,引入 monent 模块所致,require('./locale/' + name);

function loadLocale(name) {
  var oldLocale = null;
  // TODO: Find a better way to register and load all the locales in Node
  if (!locales[name] && (typeof module !== 'undefined') &&
    module && module.exports) {
    try {
      oldLocale = globalLocale._abbr;
      require('./locale/' + name);
      // because defineLocale currently also sets the global locale, we
      // want to undo that for lazy loaded locales
      locale_locales__getSetGlobalLocale(oldLocale);
    } catch (e) {}
  }
  return locales[name];
}
@benjycui
Copy link
Contributor

可以把整个 trace 贴出来?

@benjycui
Copy link
Contributor

就是这个 error 的完整版:

WARNING in ./~/antd/dist/antd.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./locale in /Users/samzhang/work/monitor/node_modules/antd/dist
 @ ./~/antd/dist/antd.js 14479:17-44

@benjycui
Copy link
Contributor

然后使用 babel-plugin-import 后应该就没这个问题了。

@afc163
Copy link
Member

afc163 commented Sep 29, 2016

@pillys
Copy link

pillys commented Sep 29, 2016

我也遇到同样的问题,请问解决了吗? @benjycui

@zhangchen2397
Copy link
Author

@afc163 done
thanks

@Morphexe
Copy link

Morphexe commented Oct 4, 2016

I am still having this issue using babel-import-module, or using the @afc163 solution, am I missing something obvious?

This is only happening on my release config, I can debug it fine.

@afc163 afc163 changed the title webpack 合并文件时出现警告错误 webpack 合并文件时出现警告错误: Cannot resolve 'file' or 'directory' ./locale Oct 8, 2016
@afc163 afc163 reopened this Oct 8, 2016
@afc163
Copy link
Member

afc163 commented Oct 8, 2016

感觉已构建文件应该提前处理掉这个问题。

@afc163
Copy link
Member

afc163 commented Oct 13, 2016

总结一下,此问题出现在 [email protected][email protected] 中。如果你没有使用 babel-plugin-import 并且是 import { xxx } from 'antd' 这样使用的,你会遇到此问题。

d063ed4 里修复了 antd/dist/antd.js 里的 require ,[email protected] 版本将解决这个问题。

在新版本发布之前,解决方案有两个:

  1. 正确使用 babel-plugin-import 可以避免这个问题。
  2. 使用 noParse 避免 webpack 解析 antd/dist/antd.js也可以。

额外的警告,遇到这个问题也意味着你没有使用 antd 配套的按需加载功能(babel-plugin-import),可能会导致静态文件过大。


如果 2.1.0 之后,你还是遇到了 moment 的问题,解决方案可见:#3508

@webliving
Copy link

有同样的问题(已经解决), 另外还有个问题 antd 已经分离到 dll 文件中了, 但打包时不能依然打包了 antd ? 有谁知道吗? (webpack 3.x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants