Skip to content

Commit

Permalink
refactor(plugin-webpack): extract production __dirname code to its ow…
Browse files Browse the repository at this point in the history
…n method
  • Loading branch information
malept committed Jul 23, 2021
1 parent 4fda813 commit 2daf053
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/plugin/webpack/src/util/AssetRelocatorPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand Down

0 comments on commit 2daf053

Please sign in to comment.