Skip to content
Permalink
Browse files

feat(plugin-webpack): support web workers by defining entry points wi…

…thout HTML files
  • Loading branch information
MarshallOfSound committed May 15, 2018
1 parent a79931f commit a85ce4ebf668c56e91cdd17120423fb1cbb078f6
Showing with 32 additions and 17 deletions.
  1. +4 −1 packages/plugin/webpack/src/Config.ts
  2. +28 −16 packages/plugin/webpack/src/WebpackPlugin.ts
@@ -3,8 +3,11 @@ import { Configuration as WebpackConfiguration } from 'webpack';
export interface WebpackPluginEntryPoint {
/**
* Relative or absolute path to the HTML template file for this entry point
*
* If this is a window, you MUST provide this. Only leave it unset for things
* like WebWorker scripts.
*/
html: string;
html?: string;
/**
* Relative or absolute path to the main JS file for this entry point
*/
@@ -99,32 +99,42 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
return null;
}

getMainConfig = async () => {
const mainConfig = this.resolveConfig(this.config.mainConfig);

if (!mainConfig.entry) {
throw new Error('Required config option "entry" has not been defined');
}

getDefines = (upOneMore = false) => {
const defines: { [key: string]: string; } = {};
let index = 0;
if (!this.config.renderer.entryPoints || !Array.isArray(this.config.renderer.entryPoints)) {
throw new Error('Required config option "renderer.entryPoints" has not been defined');
}
for (const entryPoint of this.config.renderer.entryPoints) {
defines[`${entryPoint.name.toUpperCase().replace(/ /g, '_')}_WEBPACK_ENTRY`] =
this.isProd
? `\`file://\$\{require('path').resolve(__dirname, '../renderer', '${entryPoint.name}', 'index.html')\}\``
: `'http://localhost:${BASE_PORT}/${entryPoint.name}'`;
if (entryPoint.html) {
defines[`${entryPoint.name.toUpperCase().replace(/ /g, '_')}_WEBPACK_ENTRY`] =
this.isProd
? `\`file://\$\{require('path').resolve(__dirname, '../renderer', '${upOneMore ? '..' : '.'}', '${entryPoint.name}', 'index.html')\}\``
: `'http://localhost:${BASE_PORT}/${entryPoint.name}'`;
} else {
defines[`${entryPoint.name.toUpperCase().replace(/ /g, '_')}_WEBPACK_ENTRY`] =
this.isProd
? `\`file://\$\{require('path').resolve(__dirname, '../renderer', '${upOneMore ? '..' : '.'}', '${entryPoint.name}', 'index.js')\}\``
: `'http://localhost:${BASE_PORT}/${entryPoint.name}/index.js'`;
}

if (entryPoint.preload) {
defines[`${entryPoint.name.toUpperCase().replace(/ /g, '_')}_PRELOAD_WEBPACK_ENTRY`] =
this.isProd
? `\`file://\$\{require('path').resolve(__dirname, '../renderer', '${entryPoint.name}', 'preload.js')\}\``
: `'${path.resolve(this.baseDir, 'renderer', entryPoint.name, 'preload.js')}'`;
}
index += 1;
}
return defines;
}

getMainConfig = async () => {
const mainConfig = this.resolveConfig(this.config.mainConfig);

if (!mainConfig.entry) {
throw new Error('Required config option "entry" has not been defined');
}

const defines = this.getDefines();
return merge.smart({
devtool: 'source-map',
target: 'electron-main',
@@ -180,9 +190,10 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
const prefixedEntries = entryPoint.prefixedEntries || [];
entry[entryPoint.name] = prefixedEntries
.concat([entryPoint.js])
.concat(this.isProd ? [] : ['webpack-hot-middleware/client']);
.concat(this.isProd || !Boolean(entryPoint.html) ? [] : ['webpack-hot-middleware/client']);
}

const defines = this.getDefines(true);
return merge.smart({
entry,
devtool: 'inline-source-map',
@@ -191,19 +202,20 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
output: {
path: path.resolve(this.baseDir, 'renderer'),
filename: '[name]/index.js',
globalObject: 'self',
},
node: {
__dirname: false,
__filename: false,
},
plugins: entryPoints.map(entryPoint =>
plugins: entryPoints.filter(entryPoint => Boolean(entryPoint.html)).map(entryPoint =>
new HtmlWebpackPlugin({
title: entryPoint.name,
template: entryPoint.html,
filename: `${entryPoint.name}/index.html`,
chunks: [entryPoint.name].concat(entryPoint.additionalChunks || []),
}),
).concat(this.isProd ? [] : [new webpack.HotModuleReplacementPlugin()]),
).concat([new webpack.DefinePlugin(defines)]).concat(this.isProd ? [] : [new webpack.HotModuleReplacementPlugin()]),
}, rendererConfig);
}

0 comments on commit a85ce4e

Please sign in to comment.
You can’t perform that action at this time.