react-loadable-plugin.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var _path = require("path");
  7. function _default({ types: t }) {
  8. return {
  9. visitor: {
  10. ImportDeclaration (path, state) {
  11. let source = path.node.source.value;
  12. if (source !== "next/dynamic") return;
  13. let defaultSpecifier = path.get("specifiers").find((specifier)=>{
  14. return specifier.isImportDefaultSpecifier();
  15. });
  16. if (!defaultSpecifier) return;
  17. const bindingName = defaultSpecifier.node.local.name;
  18. const binding = path.scope.getBinding(bindingName);
  19. if (!binding) {
  20. return;
  21. }
  22. binding.referencePaths.forEach((refPath)=>{
  23. var ref2, ref1;
  24. let callExpression = refPath.parentPath;
  25. if (callExpression.isMemberExpression() && callExpression.node.computed === false) {
  26. const property = callExpression.get("property");
  27. if (!Array.isArray(property) && property.isIdentifier({
  28. name: "Map"
  29. })) {
  30. callExpression = callExpression.parentPath;
  31. }
  32. }
  33. if (!callExpression.isCallExpression()) return;
  34. const callExpression_ = callExpression;
  35. let args = callExpression_.get("arguments");
  36. if (args.length > 2) {
  37. throw callExpression_.buildCodeFrameError("next/dynamic only accepts 2 arguments");
  38. }
  39. if (!args[0]) {
  40. return;
  41. }
  42. let loader;
  43. let options;
  44. if (args[0].isObjectExpression()) {
  45. options = args[0];
  46. } else {
  47. if (!args[1]) {
  48. callExpression_.node.arguments.push(t.objectExpression([]));
  49. }
  50. // This is needed as the code is modified above
  51. args = callExpression_.get("arguments");
  52. loader = args[0];
  53. options = args[1];
  54. }
  55. if (!options.isObjectExpression()) return;
  56. const options_ = options;
  57. let properties = options_.get("properties");
  58. let propertiesMap = {};
  59. properties.forEach((property)=>{
  60. const key = property.get("key");
  61. propertiesMap[key.node.name] = property;
  62. });
  63. if (propertiesMap.loadableGenerated) {
  64. return;
  65. }
  66. if (propertiesMap.loader) {
  67. loader = propertiesMap.loader.get("value");
  68. }
  69. if (propertiesMap.modules) {
  70. loader = propertiesMap.modules.get("value");
  71. }
  72. if (!loader || Array.isArray(loader)) {
  73. return;
  74. }
  75. const dynamicImports = [];
  76. const dynamicKeys = [];
  77. loader.traverse({
  78. Import (importPath) {
  79. var ref;
  80. const importArguments = importPath.parentPath.get("arguments");
  81. if (!Array.isArray(importArguments)) return;
  82. const node = importArguments[0].node;
  83. dynamicImports.push(node);
  84. dynamicKeys.push(t.binaryExpression("+", t.stringLiteral((((ref = state.file.opts.caller) == null ? void 0 : ref.pagesDir) ? (0, _path).relative(state.file.opts.caller.pagesDir, state.file.opts.filename) : state.file.opts.filename) + " -> "), node));
  85. }
  86. });
  87. if (!dynamicImports.length) return;
  88. options.node.properties.push(t.objectProperty(t.identifier("loadableGenerated"), t.objectExpression(((ref2 = state.file.opts.caller) == null ? void 0 : ref2.isDev) || ((ref1 = state.file.opts.caller) == null ? void 0 : ref1.isServer) ? [
  89. t.objectProperty(t.identifier("modules"), t.arrayExpression(dynamicKeys)),
  90. ] : [
  91. t.objectProperty(t.identifier("webpack"), t.arrowFunctionExpression([], t.arrayExpression(dynamicImports.map((dynamicImport)=>{
  92. return t.callExpression(t.memberExpression(t.identifier("require"), t.identifier("resolveWeak")), [
  93. dynamicImport
  94. ]);
  95. })))),
  96. ])));
  97. // Turns `dynamic(import('something'))` into `dynamic(() => import('something'))` for backwards compat.
  98. // This is the replicate the behavior in versions below Next.js 7 where we magically handled not executing the `import()` too.
  99. // We'll deprecate this behavior and provide a codemod for it in 7.1.
  100. if (loader.isCallExpression()) {
  101. const arrowFunction = t.arrowFunctionExpression([], loader.node);
  102. loader.replaceWith(arrowFunction);
  103. }
  104. });
  105. }
  106. }
  107. };
  108. }
  109. //# sourceMappingURL=react-loadable-plugin.js.map