From 08c356893b0a5e31e1a73670897cdffd5117eeed Mon Sep 17 00:00:00 2001 From: Jabrail <78273416+jabrailkhalil@users.noreply.github.com> Date: Wed, 9 Sep 2026 00:12:54 +0300 Subject: [PATCH] fix(cli): load default exports from JavaScript config --- cli/src/config.ts | 5 ++++- cli/test/config.spec.ts | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 cli/test/config.spec.ts diff --git a/cli/src/config.ts b/cli/src/config.ts index bb71177dc..35f7620d7 100644 --- a/cli/src/config.ts +++ b/cli/src/config.ts @@ -137,11 +137,14 @@ async function loadExtConfigJS( extConfigFilePath: string, ): Promise { try { + const extConfigObject = await require(extConfigFilePath); + const extConfig = extConfigObject.default ? await extConfigObject.default : extConfigObject; + return { extConfigType: 'js', extConfigName, extConfigFilePath: extConfigFilePath, - extConfig: await require(extConfigFilePath), + extConfig, }; } catch (e: any) { fatal(`Parsing ${c.strong(extConfigName)} failed.\n\n${e.stack ?? e}`); diff --git a/cli/test/config.spec.ts b/cli/test/config.spec.ts new file mode 100644 index 000000000..43461e803 --- /dev/null +++ b/cli/test/config.spec.ts @@ -0,0 +1,50 @@ +import { execFileSync } from 'child_process'; +import { mkdtemp, remove, writeFile, writeJSON } from 'fs-extra'; +import { tmpdir } from 'os'; +import { join, resolve } from 'path'; + +describe('JavaScript configuration', () => { + let appDir: string; + + beforeEach(async () => { + appDir = await mkdtemp(join(tmpdir(), 'capacitor-config-')); + }); + + afterEach(async () => { + await remove(appDir); + }); + + it.each([ + ['CommonJS', 'commonjs', 'module.exports ='], + ['ES module', 'module', 'export default'], + ])('loads a %s configuration', async (_name, type, exportStatement) => { + await writeJSON(join(appDir, 'package.json'), { name: 'config-test', type }); + const expected = { + appId: 'com.example.config', + appName: 'Config Test', + webDir: 'build/frontend', + server: { url: 'http://localhost:5173', cleartext: true }, + }; + await writeFile(join(appDir, 'capacitor.config.js'), `${exportStatement} ${JSON.stringify(expected)};`); + + // Use Node's module loader, as Jest transforms ES modules into CommonJS. + const configPath = resolve(__dirname, '../dist/config.js'); + const output = execFileSync( + process.execPath, + [ + '-e', + `require(${JSON.stringify(configPath)}).loadConfig().then(({ app }) => { + console.log(JSON.stringify({ appId: app.appId, appName: app.appName, webDir: app.webDir, extConfig: app.extConfig })); + }).catch(error => { console.error(error); process.exitCode = 1; });`, + ], + { cwd: appDir, encoding: 'utf8', timeout: 10000 }, + ); + + expect(JSON.parse(output)).toEqual({ + appId: expected.appId, + appName: expected.appName, + webDir: expected.webDir, + extConfig: expected, + }); + }); +});