Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin-webpack): add nodeIntegration config for renderers #2330

Merged
merged 1 commit into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/plugin/webpack/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ export interface WebpackPluginRendererConfig {
* actually packaged with your app.
*/
jsonStats?: boolean;
/**
* Adjusts the Webpack config for the renderer based on whether `nodeIntegration` for the
* `BrowserWindow` is enabled. Namely, for Webpack's `target` option:
*
* * When `nodeIntegration` is true, the `target` is `electron-renderer`.
* * When `nodeIntegration` is false, the `target` is `web`.
*
* Unfortunately, we cannot derive the value from the main process code as it can be a
* dynamically generated value at runtime, and Webpack processes at build-time.
*
* Defaults to `false` (as it is disabled by default in Electron >= 5).
*/
nodeIntegration?: boolean;
/**
* Array of entry points, these should map to the windows your app needs to
* open. Each window requires it's own entry point
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin/webpack/src/WebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export default class WebpackConfigGenerator {
return this.isProd ? 'production' : 'development';
}

get rendererTarget() {
return this.pluginConfig.renderer.nodeIntegration ? 'electron-renderer' : 'web';
}

rendererEntryPoint(
entryPoint: WebpackPluginEntryPoint,
inRendererDir: boolean,
Expand Down Expand Up @@ -215,7 +219,7 @@ export default class WebpackConfigGenerator {
return webpackMerge({
entry,
devtool: this.sourceMapOption(),
target: ['web', 'electron-renderer'],
target: this.rendererTarget,
mode: this.mode,
output: {
path: path.resolve(this.webpackDir, 'renderer'),
Expand Down
35 changes: 33 additions & 2 deletions packages/plugin/webpack/test/WebpackConfig_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ import { WebpackPluginConfig, WebpackPluginEntryPoint } from '../src/Config';
const mockProjectDir = process.platform === 'win32' ? 'C:\\path' : '/path';

describe('WebpackConfigGenerator', () => {
describe('rendererTarget', () => {
it('is web if undefined', () => {
const config = {
renderer: {},
} as WebpackPluginConfig;
const generator = new WebpackConfigGenerator(config, '/', false, 3000);
expect(generator.rendererTarget).to.equal('web');
});

it('is web if false', () => {
const config = {
renderer: {
nodeIntegration: false,
},
} as WebpackPluginConfig;
const generator = new WebpackConfigGenerator(config, '/', false, 3000);
expect(generator.rendererTarget).to.equal('web');
});

it('is electron-renderer if true', () => {
const config = {
renderer: {
nodeIntegration: true,
},
} as WebpackPluginConfig;
const generator = new WebpackConfigGenerator(config, '/', false, 3000);
expect(generator.rendererTarget).to.equal('electron-renderer');
});
});

describe('getDefines', () => {
it('throws an error if renderer.entryPoints does not exist', () => {
const config = {
Expand Down Expand Up @@ -348,11 +378,12 @@ describe('WebpackConfigGenerator', () => {
name: 'main',
js: 'rendererScript.js',
}],
nodeIntegration: true,
},
} as WebpackPluginConfig;
const generator = new WebpackConfigGenerator(config, mockProjectDir, false, 3000);
const webpackConfig = await generator.getRendererConfig(config.renderer.entryPoints);
expect(webpackConfig.target).to.deep.equal(['web', 'electron-renderer']);
expect(webpackConfig.target).to.deep.equal('electron-renderer');
expect(webpackConfig.mode).to.equal('development');
expect(webpackConfig.entry).to.deep.equal({
main: ['rendererScript.js'],
Expand Down Expand Up @@ -397,7 +428,7 @@ describe('WebpackConfigGenerator', () => {
} as WebpackPluginConfig;
const generator = new WebpackConfigGenerator(config, mockProjectDir, true, 3000);
const webpackConfig = await generator.getRendererConfig(config.renderer.entryPoints);
expect(webpackConfig.target).to.deep.equal(['web', 'electron-renderer']);
expect(webpackConfig.target).to.deep.equal('web');
expect(webpackConfig.mode).to.equal('production');
expect(webpackConfig.entry).to.deep.equal({
main: ['rendererScript.js'],
Expand Down