Skip to content

Commit

Permalink
fix(plugin-webpack): prevent the renderer config from overriding its …
Browse files Browse the repository at this point in the history
…preload config's target (#1853)

* fix: override preload script webpack target to electron-preload

while merging to create a Webpack config for preload script, made the target from the renderer config ignored and manually set to `electron-preload`

* fix: syntax error in ed70861

* chore: lint fix

* test: added tests for renderer config with custom target

Added 3 tests
- Renderer config : generates config with custom target - web
- Preload renderer config : generates a production config 
- Preload renderer config : generates config overriding custom target with 'electron-preload'

* Apply suggestions from code review

* Update packages/plugin/webpack/test/WebpackConfig_spec.ts

Co-authored-by: Mark Lee <malept@users.noreply.github.com>
  • Loading branch information
vazra and malept authored Aug 5, 2020
1 parent b0ca86a commit 8126a73
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/plugin/webpack/src/WebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ export default class WebpackConfigGenerator {

return webpackMerge({
devtool: 'inline-source-map',
target: 'electron-preload',
mode: this.mode,
entry: prefixedEntries.concat([
entryPoint.js,
Expand All @@ -188,7 +187,9 @@ export default class WebpackConfigGenerator {
__dirname: false,
__filename: false,
},
}, rendererConfig || {});
},
rendererConfig || {},
{ target: 'electron-preload' });
}

async getRendererConfig(entryPoints: WebpackPluginEntryPoint[]) {
Expand Down
65 changes: 65 additions & 0 deletions packages/plugin/webpack/test/WebpackConfig_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,54 @@ describe('WebpackConfigGenerator', () => {
filename: 'preload.js',
});
});

it('generates a production config', async () => {
const config = {
renderer: {
entryPoints: [{
name: 'main',
preload: {
js: 'preload.js',
},
}],
},
} as WebpackPluginConfig;
const generator = new WebpackConfigGenerator(config, mockProjectDir, true, 3000);
const entryPoint = config.renderer.entryPoints[0];
const webpackConfig = await generator.getPreloadRendererConfig(
entryPoint,
entryPoint.preload!,
);
expect(webpackConfig.target).to.equal('electron-preload');
expect(webpackConfig.mode).to.equal('production');
expect(webpackConfig.entry).to.deep.equal(['preload.js']);
expect(webpackConfig.output).to.deep.equal({
path: path.join(mockProjectDir, '.webpack', 'renderer', 'main'),
filename: 'preload.js',
});
});
it('prevents the preload target from being overridden', async () => {
const config = {
renderer: {
config: {
target: 'web',
},
entryPoints: [{
name: 'main',
preload: {
js: 'preload.js',
},
}],
},
} as WebpackPluginConfig;
const generator = new WebpackConfigGenerator(config, mockProjectDir, true, 3000);
const entryPoint = config.renderer.entryPoints[0];
const webpackConfig = await generator.getPreloadRendererConfig(
entryPoint,
entryPoint.preload!,
);
expect(webpackConfig.target).to.equal('electron-preload');
});
});

describe('getRendererConfig', () => {
Expand Down Expand Up @@ -300,5 +348,22 @@ describe('WebpackConfigGenerator', () => {
});
expect(webpackConfig.plugins!.length).to.equal(1);
});

it('can override the renderer target', async () => {
const config = {
renderer: {
config: {
target: 'web',
},
entryPoints: [{
name: 'main',
js: 'renderer.js',
}],
},
} as WebpackPluginConfig;
const generator = new WebpackConfigGenerator(config, mockProjectDir, true, 3000);
const webpackConfig = await generator.getRendererConfig(config.renderer.entryPoints);
expect(webpackConfig.target).to.equal('web');
});
});
});

0 comments on commit 8126a73

Please sign in to comment.