Skip to content

Commit

Permalink
fix(webpack-plugin): Ensure asset relocator injected code works with …
Browse files Browse the repository at this point in the history
…nodeIntegration disabled (#2396)

* Adjust injected code when nodeIntegration = false

Co-authored-by: Mark Lee <[email protected]>
  • Loading branch information
timfish and malept authored Jul 23, 2021
1 parent 760382c commit 146fc31
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
5 changes: 4 additions & 1 deletion packages/plugin/webpack/src/WebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ export default class WebpackConfigGenerator {
filename: `${entryPoint.name}/index.html`,
chunks: [entryPoint.name].concat(entryPoint.additionalChunks || []),
}) as WebpackPluginInstance).concat(
[new webpack.DefinePlugin(defines), new AssetRelocatorPatch(this.isProd)],
[
new webpack.DefinePlugin(defines),
new AssetRelocatorPatch(this.isProd, !!this.pluginConfig.renderer.nodeIntegration),
],
);
return webpackMerge({
entry,
Expand Down
33 changes: 24 additions & 9 deletions packages/plugin/webpack/src/util/AssetRelocatorPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,26 @@ import { Chunk, Compiler } from 'webpack';
export default class AssetRelocatorPatch {
private readonly isProd: boolean;

constructor(isProd: boolean) {
private readonly nodeIntegration: boolean;

constructor(isProd: boolean, nodeIntegration: boolean) {
this.isProd = isProd;
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) {
Expand Down Expand Up @@ -32,20 +50,17 @@ export default class AssetRelocatorPatch {
throw new Error('The installed version of @vercel/webpack-asset-relocator-loader does not appear to be compatible with Forge');
}

if (this.isProd) {
return originalInjectCode.replace('__dirname', this.injectedProductionDirnameCode());
}

return originalInjectCode.replace(
'__dirname',
this.isProd
// 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), "..")'
// In development, the app is loaded via webpack-dev-server
// so __dirname is useless because it points to Electron
// internal code. Instead we hard-code the absolute path to
// the webpack output.
: JSON.stringify(compiler.options.output.path),
JSON.stringify(compiler.options.output.path),
);
};
}
Expand Down
18 changes: 17 additions & 1 deletion packages/plugin/webpack/test/AssetRelocatorPatch_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ describe('AssetRelocatorPatch', () => {
const config = {
mainConfig: './webpack.main.config.js',
renderer: {
nodeIntegration: true,
config: './webpack.renderer.config.js',
entryPoints: [
{
Expand Down Expand Up @@ -176,7 +177,7 @@ describe('AssetRelocatorPatch', () => {
});

describe('Production', () => {
const generator = new WebpackConfigGenerator(config, appPath, true, 3000);
let generator = new WebpackConfigGenerator(config, appPath, true, 3000);

it('builds main', async () => {
const mainConfig = generator.getMainConfig();
Expand Down Expand Up @@ -224,5 +225,20 @@ describe('AssetRelocatorPatch', () => {
expect(output).to.contain('Hello, world! from the preload');
expect(output).to.contain('Hello, world! from the renderer');
});

it('builds renderer with nodeIntegration = false', async () => {
config.renderer.nodeIntegration = false;
generator = new WebpackConfigGenerator(config, appPath, true, 3000);

const rendererConfig = await generator.getRendererConfig(config.renderer.entryPoints);
await asyncWebpack(rendererConfig);

await expectOutputFileToHaveTheCorrectNativeModulePath({
outDir: rendererOut,
jsPath: path.join(rendererOut, 'main_window/index.js'),
nativeModulesString: '.ab="/native_modules/"',
nativePathString: `.ab+"${nativePathSuffix}"`,
});
});
});
});

0 comments on commit 146fc31

Please sign in to comment.