Skip to content

Commit

Permalink
feat(plugin-webpack): support web workers by defining entry points wi…
Browse files Browse the repository at this point in the history
…thout HTML files
  • Loading branch information
MarshallOfSound committed May 15, 2018
1 parent a79931f commit a85ce4e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
5 changes: 4 additions & 1 deletion packages/plugin/webpack/src/Config.ts
Expand Up @@ -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
*/
Expand Down
44 changes: 28 additions & 16 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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);
}

Expand Down

0 comments on commit a85ce4e

Please sign in to comment.