app-build-manifest-plugin.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _webpack = require("next/dist/compiled/webpack/webpack");
  6. var _constants = require("../../../shared/lib/constants");
  7. var _buildManifestPlugin = require("./build-manifest-plugin");
  8. var _getAppRouteFromEntrypoint = _interopRequireDefault(require("../../../server/get-app-route-from-entrypoint"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. const PLUGIN_NAME = "AppBuildManifestPlugin";
  15. class AppBuildManifestPlugin {
  16. constructor(options){
  17. this.dev = options.dev;
  18. }
  19. apply(compiler) {
  20. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation, { normalModuleFactory })=>{
  21. compilation.dependencyFactories.set(_webpack.webpack.dependencies.ModuleDependency, normalModuleFactory);
  22. compilation.dependencyTemplates.set(_webpack.webpack.dependencies.ModuleDependency, new _webpack.webpack.dependencies.NullDependency.Template());
  23. });
  24. compiler.hooks.make.tap(PLUGIN_NAME, (compilation)=>{
  25. compilation.hooks.processAssets.tap({
  26. name: PLUGIN_NAME,
  27. // @ts-ignore TODO: Remove ignore when webpack 5 is stable
  28. stage: _webpack.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
  29. }, (assets)=>this.createAsset(assets, compilation));
  30. });
  31. }
  32. createAsset(assets, compilation) {
  33. const manifest = {
  34. pages: {}
  35. };
  36. const systemEntrypoints = new Set([
  37. _constants.CLIENT_STATIC_FILES_RUNTIME_MAIN,
  38. _constants.CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
  39. _constants.CLIENT_STATIC_FILES_RUNTIME_AMP,
  40. _constants.CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
  41. ]);
  42. const mainFiles = new Set((0, _buildManifestPlugin).getEntrypointFiles(compilation.entrypoints.get(_constants.CLIENT_STATIC_FILES_RUNTIME_MAIN_APP)));
  43. for (const entrypoint of compilation.entrypoints.values()){
  44. if (!entrypoint.name) {
  45. continue;
  46. }
  47. if (systemEntrypoints.has(entrypoint.name)) {
  48. continue;
  49. }
  50. const pagePath = (0, _getAppRouteFromEntrypoint).default(entrypoint.name);
  51. if (!pagePath) {
  52. continue;
  53. }
  54. const filesForPage = (0, _buildManifestPlugin).getEntrypointFiles(entrypoint);
  55. manifest.pages[pagePath] = [
  56. ...new Set([
  57. ...mainFiles,
  58. ...filesForPage
  59. ])
  60. ];
  61. }
  62. const json = JSON.stringify(manifest, null, 2);
  63. assets[_constants.APP_BUILD_MANIFEST] = new _webpack.sources.RawSource(json);
  64. }
  65. }
  66. exports.AppBuildManifestPlugin = AppBuildManifestPlugin;
  67. //# sourceMappingURL=app-build-manifest-plugin.js.map