diff --git a/packages/plugin/webpack/src/util/AssetRelocatorPatch.ts b/packages/plugin/webpack/src/util/AssetRelocatorPatch.ts index 9df094beab..6661073a05 100644 --- a/packages/plugin/webpack/src/util/AssetRelocatorPatch.ts +++ b/packages/plugin/webpack/src/util/AssetRelocatorPatch.ts @@ -10,6 +10,21 @@ export default class AssetRelocatorPatch { this.nodeIntegration = nodeIntegration; } + private injectedProductionDirnameCode(): string { + if (this.nodeIntegration) { + // In production the assets are found one directory up from + // __dirname + // + // __dirname cannot be used directly until this PR lands + // https://github.com/jantimon/html-webpack-plugin/pull/1650 + return 'require("path").resolve(require("path").dirname(__filename), "..")'; + } + + // If nodeIntegration is disabled, we replace __dirname + // with an empty string so no error is thrown at runtime + return '""'; + } + public apply(compiler: Compiler) { compiler.hooks.compilation.tap( 'asset-relocator-forge-patch', @@ -36,19 +51,7 @@ export default class AssetRelocatorPatch { } if (this.isProd) { - return originalInjectCode.replace( - '__dirname', - this.nodeIntegration - // In production the assets are found one directory up from - // __dirname - // - // __dirname cannot be used directly until this PR lands - // https://github.com/jantimon/html-webpack-plugin/pull/1650 - ? 'require("path").resolve(require("path").dirname(__filename), "..")' - // If nodeIntegration is disabled, we replace __dirname - // with an empty string so no error is thrown at runtime - : '""', - ); + return originalInjectCode.replace('__dirname', this.injectedProductionDirnameCode()); } return originalInjectCode.replace(