diff --git a/.changeset/thin-webs-enjoy.md b/.changeset/thin-webs-enjoy.md new file mode 100644 index 0000000000..52522af4ec --- /dev/null +++ b/.changeset/thin-webs-enjoy.md @@ -0,0 +1,5 @@ +--- +"@lynx-js/template-webpack-plugin": patch +--- + +Remove `compiler.hooks.initialize` as [it's not called in child compilers](https://github.com/web-infra-dev/rspack/blob/aa4ad886b900770787ecddd625d3e24a51b6b99c/packages/rspack/src/rspack.ts#L78). diff --git a/packages/webpack/template-webpack-plugin/src/LynxTemplatePlugin.ts b/packages/webpack/template-webpack-plugin/src/LynxTemplatePlugin.ts index b3686cdd1f..b9a9f71553 100644 --- a/packages/webpack/template-webpack-plugin/src/LynxTemplatePlugin.ts +++ b/packages/webpack/template-webpack-plugin/src/LynxTemplatePlugin.ts @@ -469,57 +469,55 @@ class LynxTemplatePluginImpl { this.hash = createHash(compiler.options.output.hashFunction ?? 'xxhash64'); - compiler.hooks.initialize.tap(this.name, () => { - // entryName to fileName conversion function - const userOptionFilename = this.#options.filename; - - const filenameFunction = typeof userOptionFilename === 'function' - ? userOptionFilename - // Replace '[name]' with entry name - : (entryName: string) => - userOptionFilename.replace(/\[name\]/g, entryName); - - /** output filenames for the given entry names */ - const entryNames = Object.keys(compiler.options.entry); - const outputFileNames = new Set( - (entryNames.length > 0 ? entryNames : ['main']).map((name) => - filenameFunction(name) - ), - ); + // entryName to fileName conversion function + const userOptionFilename = this.#options.filename; + + const filenameFunction = typeof userOptionFilename === 'function' + ? userOptionFilename + // Replace '[name]' with entry name + : (entryName: string) => + userOptionFilename.replace(/\[name\]/g, entryName); + + /** output filenames for the given entry names */ + const entryNames = Object.keys(compiler.options.entry); + const outputFileNames = new Set( + (entryNames.length > 0 ? entryNames : ['main']).map((name) => + filenameFunction(name) + ), + ); - outputFileNames.forEach((outputFileName) => { - // convert absolute filename into relative so that webpack can - // generate it at correct location - let filename = outputFileName; - if (path.resolve(filename) === path.normalize(filename)) { - filename = path.relative( - /** Once initialized the path is always a string */ - compiler.options.output.path!, - filename, - ); - } + outputFileNames.forEach((outputFileName) => { + // convert absolute filename into relative so that webpack can + // generate it at correct location + let filename = outputFileName; + if (path.resolve(filename) === path.normalize(filename)) { + filename = path.relative( + /** Once initialized the path is always a string */ + compiler.options.output.path!, + filename, + ); + } - compiler.hooks.thisCompilation.tap(this.name, (compilation) => { - compilation.hooks.processAssets.tapPromise( - { - name: this.name, - stage: - /** - * Generate the html after minification and dev tooling is done - * and source-map is generated - */ - compiler.webpack.Compilation - .PROCESS_ASSETS_STAGE_OPTIMIZE_HASH, - }, - () => { - return this.#generateTemplate( - compiler, - compilation, - filename, - ); - }, - ); - }); + compiler.hooks.thisCompilation.tap(this.name, (compilation) => { + compilation.hooks.processAssets.tapPromise( + { + name: this.name, + stage: + /** + * Generate the html after minification and dev tooling is done + * and source-map is generated + */ + compiler.webpack.Compilation + .PROCESS_ASSETS_STAGE_OPTIMIZE_HASH, + }, + () => { + return this.#generateTemplate( + compiler, + compilation, + filename, + ); + }, + ); }); });