pages-manifest-plugin.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _webpack = require("next/dist/compiled/webpack/webpack");
  7. var _constants = require("../../../shared/lib/constants");
  8. var _getRouteFromEntrypoint = _interopRequireDefault(require("../../../server/get-route-from-entrypoint"));
  9. var _normalizePathSep = require("../../../shared/lib/page-path/normalize-path-sep");
  10. class PagesManifestPlugin {
  11. constructor({ serverless , dev , isEdgeRuntime , appDirEnabled }){
  12. this.serverless = serverless;
  13. this.dev = dev;
  14. this.isEdgeRuntime = isEdgeRuntime;
  15. this.appDirEnabled = appDirEnabled;
  16. }
  17. createAssets(compilation, assets) {
  18. const entrypoints = compilation.entrypoints;
  19. const pages = {};
  20. const appPaths = {};
  21. for (const entrypoint of entrypoints.values()){
  22. const pagePath = (0, _getRouteFromEntrypoint).default(entrypoint.name, this.appDirEnabled);
  23. if (!pagePath) {
  24. continue;
  25. }
  26. const files = entrypoint.getFiles().filter((file)=>!file.includes("webpack-runtime") && !file.includes("webpack-api-runtime") && file.endsWith(".js"));
  27. // Skip entries which are empty
  28. if (!files.length) {
  29. continue;
  30. }
  31. // Write filename, replace any backslashes in path (on windows) with forwardslashes for cross-platform consistency.
  32. let file1 = files[files.length - 1];
  33. if (!this.dev) {
  34. if (!this.isEdgeRuntime) {
  35. file1 = file1.slice(3);
  36. }
  37. }
  38. file1 = (0, _normalizePathSep).normalizePathSep(file1);
  39. if (entrypoint.name.startsWith("app/")) {
  40. appPaths[pagePath] = file1;
  41. } else {
  42. pages[pagePath] = file1;
  43. }
  44. }
  45. // This plugin is used by both the Node server and Edge server compilers,
  46. // we need to merge both pages to generate the full manifest.
  47. if (this.isEdgeRuntime) {
  48. edgeServerPages = pages;
  49. edgeServerAppPaths = appPaths;
  50. } else {
  51. nodeServerPages = pages;
  52. nodeServerAppPaths = appPaths;
  53. }
  54. assets[`${!this.dev && !this.isEdgeRuntime ? "../" : ""}` + _constants.PAGES_MANIFEST] = new _webpack.sources.RawSource(JSON.stringify({
  55. ...edgeServerPages,
  56. ...nodeServerPages
  57. }, null, 2));
  58. if (this.appDirEnabled) {
  59. assets[`${!this.dev && !this.isEdgeRuntime ? "../" : ""}` + _constants.APP_PATHS_MANIFEST] = new _webpack.sources.RawSource(JSON.stringify({
  60. ...edgeServerAppPaths,
  61. ...nodeServerAppPaths
  62. }, null, 2));
  63. }
  64. }
  65. apply(compiler) {
  66. compiler.hooks.make.tap("NextJsPagesManifest", (compilation)=>{
  67. // @ts-ignore TODO: Remove ignore when webpack 5 is stable
  68. compilation.hooks.processAssets.tap({
  69. name: "NextJsPagesManifest",
  70. // @ts-ignore TODO: Remove ignore when webpack 5 is stable
  71. stage: _webpack.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
  72. }, (assets)=>{
  73. this.createAssets(compilation, assets);
  74. });
  75. });
  76. }
  77. }
  78. exports.default = PagesManifestPlugin;
  79. function _interopRequireDefault(obj) {
  80. return obj && obj.__esModule ? obj : {
  81. default: obj
  82. };
  83. }
  84. let edgeServerPages = {};
  85. let nodeServerPages = {};
  86. let edgeServerAppPaths = {};
  87. let nodeServerAppPaths = {};
  88. //# sourceMappingURL=pages-manifest-plugin.js.map