fixrequire.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // 'index.' character codes
  2. if (Editor.remote.App.version === "2.4.5") return;
  3. const pluginDistPath = Editor.url(global.pluginDistPath);
  4. const Module = require.main.constructor;
  5. const NativeModule = process.NativeModule;
  6. const pluginNodeModulesPath = global.pluginNodeModulesPath;
  7. // const panelId
  8. if (!process.mainModule.paths.includes(pluginNodeModulesPath)) {
  9. process.mainModule.paths.push(pluginNodeModulesPath)
  10. }
  11. Module._resolveFilename = function(request, parent, isMain, options) {
  12. if (NativeModule.canBeRequiredByUsers(request)) {
  13. return request;
  14. }
  15. var paths;
  16. if (typeof options === 'object' && options !== null &&
  17. Array.isArray(options.paths)) {
  18. const fakeParent = new Module('', null);
  19. paths = [];
  20. for (var i = 0; i < options.paths.length; i++) {
  21. const path = options.paths[i];
  22. fakeParent.paths = Module._nodeModulePaths(path);
  23. const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true);
  24. for (var j = 0; j < lookupPaths.length; j++) {
  25. if (!paths.includes(lookupPaths[j]))
  26. paths.push(lookupPaths[j]);
  27. }
  28. }
  29. } else {
  30. paths = Module._resolveLookupPaths(request, parent, true);
  31. }
  32. // Look up the filename first, since that's the cache key.
  33. let filename = Module._findPath(request, paths, isMain);
  34. if (!filename) {
  35. //找不到,尝试去插件目录找啊---------
  36. filename = Module._findPath(request, [pluginDistPath], isMain);
  37. if (!filename) {
  38. const requireStack = [];
  39. for (var cursor = parent; cursor; cursor = cursor.parent) {
  40. requireStack.push(cursor.filename || cursor.id);
  41. }
  42. let message = `Cannot find module '${request}'`;
  43. if (requireStack.length > 0) {
  44. message = message + '\nRequire stack:\n- ' + requireStack.join('\n- ');
  45. }
  46. // eslint-disable-next-line no-restricted-syntax
  47. var err = new Error(message);
  48. err.code = 'MODULE_NOT_FOUND';
  49. err.requireStack = requireStack;
  50. throw err;
  51. }
  52. }
  53. return filename;
  54. };