next-dev.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env node
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.nextDev = void 0;
  7. var _indexJs = _interopRequireDefault(require("next/dist/compiled/arg/index.js"));
  8. var _fs = require("fs");
  9. var _startServer = require("../server/lib/start-server");
  10. var _utils = require("../server/lib/utils");
  11. var Log = _interopRequireWildcard(require("../build/output/log"));
  12. var _output = require("../build/output");
  13. var _isError = _interopRequireDefault(require("../lib/is-error"));
  14. var _getProjectDir = require("../lib/get-project-dir");
  15. var _constants = require("../shared/lib/constants");
  16. var _path = _interopRequireDefault(require("path"));
  17. function _interopRequireDefault(obj) {
  18. return obj && obj.__esModule ? obj : {
  19. default: obj
  20. };
  21. }
  22. function _getRequireWildcardCache() {
  23. if (typeof WeakMap !== "function") return null;
  24. var cache = new WeakMap();
  25. _getRequireWildcardCache = function() {
  26. return cache;
  27. };
  28. return cache;
  29. }
  30. function _interopRequireWildcard(obj) {
  31. if (obj && obj.__esModule) {
  32. return obj;
  33. }
  34. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  35. return {
  36. default: obj
  37. };
  38. }
  39. var cache = _getRequireWildcardCache();
  40. if (cache && cache.has(obj)) {
  41. return cache.get(obj);
  42. }
  43. var newObj = {};
  44. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  45. for(var key in obj){
  46. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  47. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  48. if (desc && (desc.get || desc.set)) {
  49. Object.defineProperty(newObj, key, desc);
  50. } else {
  51. newObj[key] = obj[key];
  52. }
  53. }
  54. }
  55. newObj.default = obj;
  56. if (cache) {
  57. cache.set(obj, newObj);
  58. }
  59. return newObj;
  60. }
  61. const nextDev = (argv)=>{
  62. const validArgs = {
  63. // Types
  64. "--help": Boolean,
  65. "--port": Number,
  66. "--hostname": String,
  67. // Aliases
  68. "-h": "--help",
  69. "-p": "--port",
  70. "-H": "--hostname"
  71. };
  72. let args;
  73. try {
  74. args = (0, _indexJs).default(validArgs, {
  75. argv
  76. });
  77. } catch (error) {
  78. if ((0, _isError).default(error) && error.code === "ARG_UNKNOWN_OPTION") {
  79. return (0, _utils).printAndExit(error.message, 1);
  80. }
  81. throw error;
  82. }
  83. if (args["--help"]) {
  84. console.log(`
  85. Description
  86. Starts the application in development mode (hot-code reloading, error
  87. reporting, etc.)
  88. Usage
  89. $ next dev <dir> -p <port number>
  90. <dir> represents the directory of the Next.js application.
  91. If no directory is provided, the current directory will be used.
  92. Options
  93. --port, -p A port number on which to start the application
  94. --hostname, -H Hostname on which to start the application (default: 0.0.0.0)
  95. --help, -h Displays this message
  96. `);
  97. process.exit(0);
  98. }
  99. const dir = (0, _getProjectDir).getProjectDir(args._[0]);
  100. // Check if pages dir exists and warn if not
  101. if (!(0, _fs).existsSync(dir)) {
  102. (0, _utils).printAndExit(`> No such directory exists as the project root: ${dir}`);
  103. }
  104. async function preflight() {
  105. const { getPackageVersion } = await Promise.resolve(require("../lib/get-package-version"));
  106. const [sassVersion, nodeSassVersion] = await Promise.all([
  107. getPackageVersion({
  108. cwd: dir,
  109. name: "sass"
  110. }),
  111. getPackageVersion({
  112. cwd: dir,
  113. name: "node-sass"
  114. }),
  115. ]);
  116. if (sassVersion && nodeSassVersion) {
  117. Log.warn("Your project has both `sass` and `node-sass` installed as dependencies, but should only use one or the other. " + "Please remove the `node-sass` dependency from your project. " + " Read more: https://nextjs.org/docs/messages/duplicate-sass");
  118. }
  119. }
  120. const port = (0, _utils).getPort(args);
  121. // If neither --port nor PORT were specified, it's okay to retry new ports.
  122. const allowRetry = args["--port"] === undefined && process.env.PORT === undefined;
  123. // We do not set a default host value here to prevent breaking
  124. // some set-ups that rely on listening on other interfaces
  125. const host = args["--hostname"];
  126. (0, _startServer).startServer({
  127. allowRetry,
  128. dev: true,
  129. dir,
  130. hostname: host,
  131. isNextDevCommand: true,
  132. port
  133. }).then(async (app)=>{
  134. const appUrl = `http://${app.hostname}:${app.port}`;
  135. (0, _output).startedDevelopmentServer(appUrl, `${host || "0.0.0.0"}:${app.port}`);
  136. // Start preflight after server is listening and ignore errors:
  137. preflight().catch(()=>{});
  138. // Finalize server bootup:
  139. await app.prepare();
  140. }).catch((err)=>{
  141. if (err.code === "EADDRINUSE") {
  142. let errorMessage = `Port ${port} is already in use.`;
  143. const pkgAppPath = require("next/dist/compiled/find-up").sync("package.json", {
  144. cwd: dir
  145. });
  146. const appPackage = require(pkgAppPath);
  147. if (appPackage.scripts) {
  148. const nextScript = Object.entries(appPackage.scripts).find((scriptLine)=>scriptLine[1] === "next");
  149. if (nextScript) {
  150. errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p <some other port>\`.`;
  151. }
  152. }
  153. console.error(errorMessage);
  154. } else {
  155. console.error(err);
  156. }
  157. process.nextTick(()=>process.exit(1));
  158. });
  159. for (const CONFIG_FILE of _constants.CONFIG_FILES){
  160. (0, _fs).watchFile(_path.default.join(dir, CONFIG_FILE), (cur, prev)=>{
  161. if (cur.size > 0 || prev.size > 0) {
  162. console.log(`\n> Found a change in ${CONFIG_FILE}. Restart the server to see the changes in effect.`);
  163. }
  164. });
  165. }
  166. };
  167. exports.nextDev = nextDev;
  168. //# sourceMappingURL=next-dev.js.map