Skip to content
Permalink
Browse files

feat: add a barebones webpack template

  • Loading branch information
MarshallOfSound committed May 23, 2019
1 parent 068c2f8 commit 3b935c8f54b4cddc7555fab9196b5e1e99db1564
@@ -10,7 +10,8 @@
"packages/maker/*",
"packages/publisher/*",
"packages/utils/*",
"packages/plugin/*"
"packages/plugin/*",
"packages/template/*"
]
},
"config": {
@@ -37,6 +37,7 @@
"@electron-forge/plugin-base": "6.0.0-beta.34",
"@electron-forge/publisher-base": "6.0.0-beta.34",
"@electron-forge/shared-types": "6.0.0-beta.34",
"@electron-forge/template-webpack": "6.0.0-beta.34",
"colors": "^1.2.0",
"cross-spawn-promise": "^0.10.1",
"debug": "^4.1.0",
@@ -57,25 +57,7 @@ export default async (dir: string, template: string) => {
await installDepList(dir, templateModule.devDependencies || [], DepType.DEV);
});

await asyncOra('Copying Template Files', async () => {
const { templateDirectory } = templateModule;
if (templateDirectory) {
const tmplPath = templateDirectory;
if (!path.isAbsolute(templateDirectory)) {
throw new Error(`Custom template path needs to be absolute, this is an issue with "electron-forge-template-${template}"`);
}

const files = glob.sync(path.resolve(tmplPath, '**/*'));

for (const file of files) {
if ((await fs.stat(file)).isFile()) {
await copy(file, path.resolve(dir, path.relative(tmplPath, file).replace(/^_/, '.')));
}
}
}
});

if (typeof templateModule.postCopy === 'function') {
await Promise.resolve(templateModule.postCopy(dir));
if (typeof templateModule.initializeTemplate === 'function') {
await Promise.resolve(templateModule.initializeTemplate(dir));
}
};
@@ -0,0 +1 @@
tmpl
@@ -0,0 +1,20 @@
{
"name": "@electron-forge/template-webpack",
"version": "6.0.0-beta.34",
"description": "Webpack template for Electron Forge, gets you started with Webpack really quickly",
"repository": "https://github.com/electron-userland/electron-forge",
"author": "Samuel Attard",
"license": "MIT",
"main": "dist/WebpackTemplate.js",
"typings": "dist/WebpackTemplate.d.ts",
"scripts": {
"test": "echo No Tests"
},
"engines": {
"node": ">= 8.0"
},
"dependencies": {
"@electron-forge/async-ora": "6.0.0-beta.34",
"@electron-forge/shared-types": "6.0.0-beta.34"
}
}
@@ -0,0 +1,55 @@
import { ForgeTemplate } from '@electron-forge/shared-types';
import { asyncOra } from '@electron-forge/async-ora';

import fs from 'fs-extra';
import path from 'path';

const currentVersion = require('../package').version;

class WebpackTemplate implements ForgeTemplate {
public devDependencies = [
`@electron-forge/plugin-webpack@${currentVersion}`
];

public initializeTemplate = async (directory: string) => {
await asyncOra('Setting up forge configuration', async () => {
const pjPath = path.resolve(directory, 'package.json');
const currentPJ = await fs.readJson(pjPath);
currentPJ.main = '.webpack/main';
currentPJ.config.forge.plugins = currentPJ.config.forge.plugins || [];
currentPJ.config.forge.plugins.push([
'@electron-forge/plugin-webpack',
{
mainConfig: './webpack.main.config.js',
renderer: {
config: './webpack.renderer.config.js',
entryPoints: [{
html: './src/index.html',
js: './src/renderer.js',
name: 'main_window',
}],
},
},
]);
await fs.writeJson(pjPath, currentPJ, {
spaces: 2,
});
});
await asyncOra('Setting up webpack configuration', async () => {
await fs.appendFile(path.resolve(directory, '.gitignore'), '.webpack\n');
await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.main.config.js'), path.resolve(directory, 'webpack.main.config.js'));
await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.renderer.config.js'), path.resolve(directory, 'webpack.renderer.config.js'));
await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'webpack.rules.js'), path.resolve(directory, 'webpack.rules.js'));
await fs.copy(path.resolve(__dirname, '..', 'tmpl', 'renderer.js'), path.resolve(directory, 'src', 'renderer.js'));
let indexContents = await fs.readFile(path.resolve(directory, 'src', 'index.js'), 'utf8');
indexContents = indexContents.split('\n').map(line => {
if (line.includes('mainWindow.loadURL')) return ' mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);';
return line;
}).join('\n');
await fs.writeFile(path.resolve(directory, 'src', 'main.js'), indexContents);
await fs.remove(path.resolve(directory, 'src', 'index.js'));
});
}
}

export default new WebpackTemplate();
@@ -0,0 +1 @@
console.log('Heya, this is coming from a webpack bundle');
@@ -0,0 +1,3 @@
module.exports = {
entry: './src/main.js',
};
@@ -0,0 +1,6 @@
module.exports = {
// Put your normal webpack config below here
module: {
rules: require('./webpack.rules'),
},
};
@@ -0,0 +1,18 @@
module.exports = [
// Put your webpack loader rules in this array. This is where you would put
// your ts-loader configuration for instance:
/**
* Typescript Example:
*
* {
* test: /\.tsx?$/,
* exclude: /(node_modules|.webpack)/,
* loaders: [{
* loader: 'ts-loader',
* options: {
* transpileOnly: true
* }
* }]
* }
*/
];
@@ -119,6 +119,5 @@ export interface StartOptions {
export interface ForgeTemplate {
dependencies?: string[];
devDependencies?: string[];
templateDirectory?: string;
postCopy?: (dir: string) => void;
initializeTemplate?: (dir: string) => void;
}

0 comments on commit 3b935c8

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