Skip to content

Commit

Permalink
feat: esm forge.config.js support (#3358)
Browse files Browse the repository at this point in the history
* feat: esm forge.config.js support

* fix: dynamic import

* fix: dynamic import on windows

* fix(esm): move config path to local var
  • Loading branch information
mahnunchik authored Nov 2, 2023
1 parent 6990f33 commit 563fc17
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ packages/*/*/doc
packages/*/*/index.ts
packages/**/bad.js
tmpl
packages/api/core/helper/dynamic-import.js
1 change: 1 addition & 0 deletions packages/api/core/helper/dynamic-import.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function dynamicImport(path: string): Promise<any>;
5 changes: 5 additions & 0 deletions packages/api/core/helper/dynamic-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const url = require('url');

exports.dynamicImport = function dynamicImport(path) {
return import(url.pathToFileURL(path));
};
3 changes: 2 additions & 1 deletion packages/api/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
},
"files": [
"dist",
"src"
"src",
"helper"
]
}
14 changes: 11 additions & 3 deletions packages/api/core/src/util/forge-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as interpret from 'interpret';
import { template } from 'lodash';
import * as rechoir from 'rechoir';

import { dynamicImport } from '../../helper/dynamic-import.js';

import { runMutatingHook } from './hook';
import PluginInterface from './plugin-interface';
import { readRawPackageJson } from './read-package-json';
Expand Down Expand Up @@ -123,14 +125,20 @@ export default async (dir: string): Promise<ResolvedForgeConfig> => {
forgeConfig = forgeConfig || ({} as ForgeConfig);

if (await forgeConfigIsValidFilePath(dir, forgeConfig)) {
const forgeConfigPath = path.resolve(dir, forgeConfig as string);
try {
// The loaded "config" could potentially be a static forge config, ESM module or async function
// eslint-disable-next-line @typescript-eslint/no-var-requires
const loaded = require(path.resolve(dir, forgeConfig as string)) as MaybeESM<ForgeConfig | AsyncForgeConfigGenerator>;
let loaded;
try {
loaded = (await dynamicImport(forgeConfigPath)) as MaybeESM<ForgeConfig | AsyncForgeConfigGenerator>;
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
loaded = require(forgeConfigPath) as MaybeESM<ForgeConfig | AsyncForgeConfigGenerator>;
}
const maybeForgeConfig = 'default' in loaded ? loaded.default : loaded;
forgeConfig = typeof maybeForgeConfig === 'function' ? await maybeForgeConfig() : maybeForgeConfig;
} catch (err) {
console.error(`Failed to load: ${path.resolve(dir, forgeConfig as string)}`);
console.error(`Failed to load: ${forgeConfigPath}`);
throw err;
}
} else if (typeof forgeConfig !== 'object') {
Expand Down
7 changes: 7 additions & 0 deletions packages/api/core/test/fast/forge-config_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ describe('forge-config', () => {
expect(conf.defaultResolved).to.equal(true);
});

it('should resolve the ESM JS file exports of forge.config.js if config.forge does not exist ', async () => {
type DefaultResolvedConfig = ResolvedForgeConfig & { defaultResolved: boolean };
const conf = (await findConfig(path.resolve(__dirname, '../fixture/dummy_default_esm_conf'))) as DefaultResolvedConfig;
expect(conf.buildIdentifier).to.equal('esm');
expect(conf.defaultResolved).to.equal(true);
});

it(`should resolve the yml config from forge.config.yml if it's specified in config.forge`, async () => {
type DefaultResolvedConfig = ResolvedForgeConfig;
const conf = (await findConfig(path.resolve(__dirname, '../fixture/dummy_ts_conf'))) as DefaultResolvedConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line node/no-unsupported-features/es-syntax
export default {
buildIdentifier: 'esm',
defaultResolved: true,
};
17 changes: 17 additions & 0 deletions packages/api/core/test/fixture/dummy_default_esm_conf/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "",
"productName": "",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"type": "module",
"scripts": {
"start": "electron-forge start"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"electron-prebuilt": "9.9.9"
}
}

0 comments on commit 563fc17

Please sign in to comment.