Skip to content

Commit

Permalink
refactor: use forge.config.ts in the webpack typescript template (#3012)
Browse files Browse the repository at this point in the history
* feat!: prefer forge.config.js over package.json config

This change affects the `init` and `import`
commands.

* run lint

* fix tests

* another test change

* fix import

* copy over forge.config files rather than mutate base tmpl

* i broke a test with rebase

* fix lint

* feat: typed forge.config.ts

* chore: extract utils to @electron-forge/core-utils to remove cyclic dep

* chore: update tests for extracted logic in core-utils

* chore: fix tests

Co-authored-by: Erick Zhao <erick@hotmail.ca>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
  • Loading branch information
3 people authored Oct 31, 2022
1 parent ab8ea66 commit b80a275
Show file tree
Hide file tree
Showing 34 changed files with 258 additions and 106 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@knodes/typedoc-plugin-monorepo-readmes": "0.22.5",
"@malept/eslint-config": "^2.0.0",
"@swc/cli": "^0.1.49",
"@swc/core": "^1.2.87",
Expand Down Expand Up @@ -133,6 +134,7 @@
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-tsdoc": "^0.2.14",
"fetch-mock": "^9.10.7",
"fork-ts-checker-webpack-plugin": "^7.2.13",
"generate-changelog": "^1.8.0",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
Expand All @@ -149,7 +151,6 @@
"typedoc": "^0.22.15",
"typedoc-plugin-missing-exports": "^1.0.0",
"typedoc-plugin-rename-defaults": "^0.6.4",
"@knodes/typedoc-plugin-monorepo-readmes": "0.22.5",
"typescript": "^4.6.3",
"xvfb-maybe": "^0.2.1"
},
Expand Down
1 change: 1 addition & 0 deletions packages/api/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"dependencies": {
"@electron-forge/async-ora": "6.0.0-beta.70",
"@electron-forge/core-utils": "^6.0.0-beta.70",
"@electron-forge/maker-base": "6.0.0-beta.70",
"@electron-forge/plugin-base": "6.0.0-beta.70",
"@electron-forge/publisher-base": "6.0.0-beta.70",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/import.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import path from 'path';

import { asyncOra } from '@electron-forge/async-ora';
import { updateElectronDependency } from '@electron-forge/core-utils';
import baseTemplate from '@electron-forge/template-base';
import chalk from 'chalk';
import debug from 'debug';
import fs from 'fs-extra';
import { merge } from 'lodash';

import { updateElectronDependency } from '../util/electron-version';
import installDepList, { DepType, DepVersionRestriction } from '../util/install-dependencies';
import { info, warn } from '../util/messages';
import { readRawPackageJson } from '../util/read-package-json';
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/make.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import path from 'path';

import { asyncOra } from '@electron-forge/async-ora';
import { getElectronVersion } from '@electron-forge/core-utils';
import { MakerBase } from '@electron-forge/maker-base';
import { ForgeArch, ForgeConfigMaker, ForgeMakeResult, ForgePlatform, IForgeResolvableMaker, ResolvedForgeConfig } from '@electron-forge/shared-types';
import { getHostArch } from '@electron/get';
import chalk from 'chalk';
import filenamify from 'filenamify';
import fs from 'fs-extra';

import { getElectronVersion } from '../util/electron-version';
import getForgeConfig from '../util/forge-config';
import { runHook, runMutatingHook } from '../util/hook';
import { info, warn } from '../util/messages';
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import { promisify } from 'util';

import { fakeOra, OraImpl, ora as realOra } from '@electron-forge/async-ora';
import { getElectronVersion } from '@electron-forge/core-utils';
import { ForgeArch, ForgePlatform } from '@electron-forge/shared-types';
import { getHostArch } from '@electron/get';
import chalk from 'chalk';
Expand All @@ -10,7 +11,6 @@ import packager, { HookFunction } from 'electron-packager';
import glob from 'fast-glob';
import fs from 'fs-extra';

import { getElectronVersion } from '../util/electron-version';
import getForgeConfig from '../util/forge-config';
import { runHook } from '../util/hook';
import { warn } from '../util/messages';
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { spawn, SpawnOptions } from 'child_process';

import { asyncOra } from '@electron-forge/async-ora';
import { getElectronVersion } from '@electron-forge/core-utils';
import { ElectronProcess, ForgeArch, ForgePlatform, StartOptions } from '@electron-forge/shared-types';
import chalk from 'chalk';
import debug from 'debug';

import locateElectronExecutable from '../util/electron-executable';
import { getElectronVersion } from '../util/electron-version';
import getForgeConfig from '../util/forge-config';
import { runHook } from '../util/hook';
import { readMutatedPackageJson } from '../util/read-package-json';
Expand Down
3 changes: 1 addition & 2 deletions packages/api/core/src/util/electron-executable.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from 'path';

import { getElectronModulePath } from '@electron-forge/core-utils';
import chalk from 'chalk';
import logSymbols from 'log-symbols';

import { getElectronModulePath } from './electron-version';

type PackageJSON = Record<string, unknown>;
type Dependencies = Record<string, string>;

Expand Down
7 changes: 3 additions & 4 deletions packages/api/core/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getElectronVersion } from './electron-version';
import { getElectronVersion, hasYarn, packagerRebuildHook, yarnOrNpmSpawn } from '@electron-forge/core-utils';

import { BuildIdentifierConfig, BuildIdentifierMap, fromBuildIdentifier } from './forge-config';
import rebuildHook from './rebuild';
import { hasYarn, yarnOrNpmSpawn } from './yarn-or-npm';

export default class ForgeUtils {
/**
Expand All @@ -19,7 +18,7 @@ export default class ForgeUtils {

hasYarn = hasYarn;

rebuildHook = rebuildHook;
rebuildHook = packagerRebuildHook;

yarnOrNpmSpawn = yarnOrNpmSpawn;
}
3 changes: 1 addition & 2 deletions packages/api/core/src/util/install-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { hasYarn, yarnOrNpmSpawn } from '@electron-forge/core-utils';
import { ExitError } from '@malept/cross-spawn-promise';
import debug from 'debug';

import { hasYarn, yarnOrNpmSpawn } from './yarn-or-npm';

const d = debug('electron-forge:dependency-installer');

export enum DepType {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/util/resolve-dir.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';

import { getElectronVersion } from '@electron-forge/core-utils';
import debug from 'debug';
import fs from 'fs-extra';

import { getElectronVersion } from './electron-version';
import { readRawPackageJson } from './read-package-json';

const d = debug('electron-forge:project-resolver');
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/test/fast/install-dependencies_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Install dependencies', () => {
spawnSpy.returns(spawnPromise);
hasYarnSpy = stub();
install = proxyquire.noCallThru().load('../../src/util/install-dependencies', {
'./yarn-or-npm': {
'@electron-forge/core-utils': {
yarnOrNpmSpawn: spawnSpy,
hasYarn: hasYarnSpy,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/test/fast/make_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('make', () => {
before(() => {
const electronPath = path.resolve(__dirname, 'node_modules/electron');
stubbedMake = proxyquire.noCallThru().load('../../src/api/make', {
'../util/electron-version': {
'@electron-forge/core-utils': {
getElectronModulePath: () => Promise.resolve(electronPath),
getElectronVersion: () => Promise.resolve('1.0.0'),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/test/fast/start_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('start', () => {

start = proxyquire.noCallThru().load('../../src/api/start', {
'../util/electron-executable': () => Promise.resolve('fake_electron_path'),
'../util/electron-version': {
'@electron-forge/core-utils': {
getElectronVersion: () => Promise.resolve('1.0.0'),
},
'../util/forge-config': async () => ({
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"dependencies": {
"@electron-forge/async-ora": "6.0.0-beta.70",
"@electron-forge/core": "6.0.0-beta.70",
"@electron-forge/core-utils": "^6.0.0-beta.70",
"@electron-forge/plugin-base": "6.0.0-beta.70",
"@electron-forge/shared-types": "6.0.0-beta.70",
"@electron-forge/web-multi-logger": "6.0.0-beta.70",
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import http from 'http';
import path from 'path';

import { asyncOra } from '@electron-forge/async-ora';
import { utils } from '@electron-forge/core';
import { getElectronVersion, packagerRebuildHook } from '@electron-forge/core-utils';
import { PluginBase } from '@electron-forge/plugin-base';
import { ForgeHookMap, ResolvedForgeConfig } from '@electron-forge/shared-types';
import Logger, { Tab } from '@electron-forge/web-multi-logger';
Expand Down Expand Up @@ -156,9 +156,9 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
prePackage: async (config, platform, arch) => {
this.isProd = true;
await fs.remove(this.baseDir);
await utils.rebuildHook(
await packagerRebuildHook(
this.projectDir,
await utils.getElectronVersion(this.projectDir, await fs.readJson(path.join(this.projectDir, 'package.json'))),
await getElectronVersion(this.projectDir, await fs.readJson(path.join(this.projectDir, 'package.json'))),
platform,
arch,
config.rebuildConfig
Expand Down
8 changes: 7 additions & 1 deletion packages/template/typescript-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
"fs-extra": "^10.0.0"
},
"devDependencies": {
"@electron-forge/maker-deb": "^6.0.0-beta.70",
"@electron-forge/maker-rpm": "^6.0.0-beta.70",
"@electron-forge/maker-squirrel": "^6.0.0-beta.70",
"@electron-forge/maker-zip": "^6.0.0-beta.70",
"@electron-forge/plugin-webpack": "^6.0.0-beta.70",
"@electron-forge/test-utils": "6.0.0-beta.70",
"chai": "^4.3.3",
"fast-glob": "^3.2.7"
"fast-glob": "^3.2.7",
"fork-ts-checker-webpack-plugin": "^7.2.13"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class TypeScriptWebpackTemplate extends BaseTemplate {
const filePath = (fileName: string) => path.join(directory, 'src', fileName);

// Copy Webpack files
await this.copyTemplateFile(directory, 'webpack.main.config.js');
await this.copyTemplateFile(directory, 'webpack.renderer.config.js');
await this.copyTemplateFile(directory, 'webpack.rules.js');
await this.copyTemplateFile(directory, 'webpack.plugins.js');
await this.copyTemplateFile(directory, 'webpack.main.config.ts');
await this.copyTemplateFile(directory, 'webpack.renderer.config.ts');
await this.copyTemplateFile(directory, 'webpack.rules.ts');
await this.copyTemplateFile(directory, 'webpack.plugins.ts');

await this.updateFileByLine(path.resolve(directory, 'src', 'index.html'), (line) => {
if (line.includes('link rel="stylesheet"')) return '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { expect } from 'chai';
import glob from 'fast-glob';
import fs from 'fs-extra';

import { api } from '../../../api/core';
import template from '../src/TypeScriptWebpackTemplate';

describe('TypeScriptWebpackTemplate', () => {
Expand All @@ -15,17 +16,22 @@ describe('TypeScriptWebpackTemplate', () => {
});

it('should succeed in initializing the typescript template', async () => {
await template.initializeTemplate(dir, {});
await api.init({
dir,
template: path.resolve(__dirname, '..', 'src', 'TypeScriptWebpackTemplate'),
interactive: false,
});
});

context('template files are copied to project', () => {
const expectedFiles = [
'tsconfig.json',
'.eslintrc.json',
'webpack.main.config.js',
'webpack.renderer.config.js',
'webpack.rules.js',
'webpack.plugins.js',
'forge.config.ts',
'webpack.main.config.ts',
'webpack.renderer.config.ts',
'webpack.rules.ts',
'webpack.plugins.ts',
path.join('src', 'index.ts'),
path.join('src', 'renderer.ts'),
path.join('src', 'preload.ts'),
Expand All @@ -44,10 +50,13 @@ describe('TypeScriptWebpackTemplate', () => {

describe('lint', () => {
before(async () => {
delete process.env.TS_NODE_PROJECT;
await testUtils.ensureModulesInstalled(
dir,
['electron', 'electron-squirrel-startup'],
template.devDependencies.filter((moduleName) => moduleName.includes('eslint') || moduleName.includes('typescript'))
template.devDependencies
.filter((moduleName) => moduleName.includes('eslint') || moduleName.includes('typescript'))
.concat(['@electron-forge/plugin-webpack'])
);
});

Expand Down
64 changes: 28 additions & 36 deletions packages/template/typescript-webpack/tmpl/forge.config.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
module.exports = {
import type { ForgeConfig } from '@electron-forge/shared-types';
import { MakerSquirrel } from '@electron-forge/maker-squirrel';
import { MakerZIP } from '@electron-forge/maker-zip';
import { MakerDeb } from '@electron-forge/maker-deb';
import { MakerRpm } from '@electron-forge/maker-rpm';
import { WebpackPlugin } from '@electron-forge/plugin-webpack';

import { mainConfig } from './webpack.main.config';
import { rendererConfig } from './webpack.renderer.config';

const config: ForgeConfig = {
packagerConfig: {},
rebuildConfig: {},
makers: [
{
name: '@electron-forge/maker-squirrel',
config: {},
},
{
name: '@electron-forge/maker-zip',
platforms: ['darwin'],
},
{
name: '@electron-forge/maker-deb',
config: {},
},
{
name: '@electron-forge/maker-rpm',
config: {},
},
],
makers: [new MakerSquirrel({}), new MakerZIP({}, ['darwin']), new MakerRpm({}), new MakerDeb({})],
plugins: [
{
name: '@electron-forge/plugin-webpack',
config: {
mainConfig: './webpack.main.config.js',
renderer: {
config: './webpack.renderer.config.js',
entryPoints: [
{
html: './src/index.html',
js: './src/renderer.ts',
name: 'main_window',
preload: {
js: './src/preload.ts',
},
new WebpackPlugin({
mainConfig,
renderer: {
config: rendererConfig,
entryPoints: [
{
html: './src/index.html',
js: './src/renderer.ts',
name: 'main_window',
preload: {
js: './src/preload.ts',
},
],
},
},
],
},
},
}),
],
};

export default config;
2 changes: 1 addition & 1 deletion packages/template/typescript-webpack/tmpl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"css-loader": "^6.0.0",
"eslint": "^8.0.1",
"eslint-plugin-import": "^2.25.0",
"fork-ts-checker-webpack-plugin": "^7.2.1",
"fork-ts-checker-webpack-plugin": "^7.2.13",
"node-loader": "^2.0.0",
"style-loader": "^3.0.0",
"ts-loader": "^9.2.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = {
import type { Configuration } from 'webpack';

export const mainConfig: Configuration = {
/**
* This is the main entry point for your application, it's the first file
* that runs in the main process.
Expand Down
3 changes: 0 additions & 3 deletions packages/template/typescript-webpack/tmpl/webpack.plugins.js

This file was deleted.

6 changes: 6 additions & 0 deletions packages/template/typescript-webpack/tmpl/webpack.plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type IForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const ForkTsCheckerWebpackPlugin: typeof IForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

export const plugins = [new ForkTsCheckerWebpackPlugin()];
Loading

0 comments on commit b80a275

Please sign in to comment.