11import { join } from "path" ;
2- import { writeFileSync , readFileSync } from "fs" ;
3- const utils = require ( "../lib/utils" ) ;
42
53export enum messages {
64 compilationComplete = "Webpack compilation complete." ,
@@ -37,10 +35,6 @@ export class WatchStateLoggerPlugin {
3735 . keys ( compilation . assets )
3836 . filter ( assetKey => compilation . assets [ assetKey ] . emitted ) ;
3937
40- if ( compilation . errors . length > 0 ) {
41- WatchStateLoggerPlugin . rewriteHotUpdateChunk ( compiler , compilation , emittedFiles ) ;
42- }
43-
4438 // provide fake paths to the {N} CLI - relative to the 'app' folder
4539 // in order to trigger the livesync process
4640 const emittedFilesFakePaths = emittedFiles
@@ -51,62 +45,4 @@ export class WatchStateLoggerPlugin {
5145 process . send && process . send ( { emittedFiles : emittedFilesFakePaths } , error => null ) ;
5246 } ) ;
5347 }
54-
55- /**
56- * Rewrite an errored chunk to make the hot module replace successful.
57- * @param compiler the webpack compiler
58- * @param emittedFiles the emitted files from the current compilation
59- */
60- private static rewriteHotUpdateChunk ( compiler , compilation , emittedFiles : string [ ] ) {
61- const chunk = this . findHotUpdateChunk ( emittedFiles ) ;
62- if ( ! chunk ) {
63- return ;
64- }
65-
66- const { name } = utils . parseHotUpdateChunkName ( chunk ) ;
67- if ( ! name ) {
68- return ;
69- }
70-
71- const absolutePath = join ( compiler . outputPath , chunk ) ;
72-
73- const newContent = this . getWebpackHotUpdateReplacementContent ( compilation . errors , absolutePath , name ) ;
74- writeFileSync ( absolutePath , newContent ) ;
75- }
76-
77- private static findHotUpdateChunk ( emittedFiles : string [ ] ) {
78- return emittedFiles . find ( file => file . endsWith ( "hot-update.js" ) ) ;
79- }
80-
81- /**
82- * Gets only the modules object after 'webpackHotUpdate("bundle",' in the chunk
83- */
84- private static getModulesObjectFromChunk ( chunkPath ) {
85- let content = readFileSync ( chunkPath , "utf8" )
86- const startIndex = content . indexOf ( "," ) + 1 ;
87- let endIndex = content . length - 1 ;
88- if ( content . endsWith ( ';' ) ) {
89- endIndex -- ;
90- }
91- return content . substring ( startIndex , endIndex ) ;
92- }
93-
94- /**
95- * Gets the webpackHotUpdate call with updated modules not to include the ones with errors
96- */
97- private static getWebpackHotUpdateReplacementContent ( compilationErrors , filePath , moduleName ) {
98- const errorModuleIds = compilationErrors . filter ( x => x . module ) . map ( x => x . module . id ) ;
99- if ( ! errorModuleIds || errorModuleIds . length == 0 ) {
100- // could not determine error modiles so discard everything
101- return `webpackHotUpdate('${ moduleName } ', {});` ;
102- }
103- const updatedModules = this . getModulesObjectFromChunk ( filePath ) ;
104-
105- // we need to filter the modules with a function in the file as it is a relaxed JSON not valid to be parsed and manipulated
106- return `const filter = function(updatedModules, modules) {
107- modules.forEach(moduleId => delete updatedModules[moduleId]);
108- return updatedModules;
109- }
110- webpackHotUpdate('${ moduleName } ', filter(${ updatedModules } , ${ JSON . stringify ( errorModuleIds ) } ));` ;
111- }
11248}
0 commit comments