Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: esm forge.config.js support #3358

Merged
merged 4 commits into from Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
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
@@ -0,0 +1 @@
export declare function dynamicImport(path: string): Promise<any>;
5 changes: 5 additions & 0 deletions packages/api/core/helper/dynamic-import.js
@@ -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
Expand Up @@ -93,6 +93,7 @@
},
"files": [
"dist",
"src"
"src",
"helper"
]
}
11 changes: 9 additions & 2 deletions packages/api/core/src/util/forge-config.ts
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 @@ -125,8 +127,13 @@ export default async (dir: string): Promise<ResolvedForgeConfig> => {
if (await forgeConfigIsValidFilePath(dir, forgeConfig)) {
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(path.resolve(dir, forgeConfig as string))) as MaybeESM<ForgeConfig | AsyncForgeConfigGenerator>;
mahnunchik marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
loaded = require(path.resolve(dir, forgeConfig as string)) as MaybeESM<ForgeConfig | AsyncForgeConfigGenerator>;
}
const maybeForgeConfig = 'default' in loaded ? loaded.default : loaded;
forgeConfig = typeof maybeForgeConfig === 'function' ? await maybeForgeConfig() : maybeForgeConfig;
} catch (err) {
Expand Down
7 changes: 7 additions & 0 deletions packages/api/core/test/fast/forge-config_spec.ts
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
@@ -0,0 +1,5 @@
// eslint-disable-next-line node/no-unsupported-features/es-syntax
export default {
buildIdentifier: 'esm',
defaultResolved: true,
};
@@ -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"
}
}