ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.46k stars 977 forks source link

feat: Support capacitor.config.js #4602

Closed alexcroox closed 3 years ago

alexcroox commented 3 years ago

Feature Request

Description

Currently you can only have a dynamic config file if you use typescript. Which if you don't, means installing TS as a dependancy and an array of headaches that come with that over time.

Preferred Solution

Support capacitor.config.js. If you try this currently you get:

   export default config
        ^^^^^^

        SyntaxError: Unexpected token 'export'
jcesarmobile commented 3 years ago

capacitor.config.js is already supported is that supported javascript syntax?

alexcroox commented 3 years ago

Yikes, sorry for wasting your time, blindly copying from docs. For anyone else looking for an example for JS only (docs only have TS examples):

It's node's module.exports = { ...config... }

const fs = require('fs')
const ip = require('ip')
const argv = require('yargs').argv

// Safety checks for devs first getting setup
if (!fs.existsSync('capacitor-project.config.json')) {
  console.error('No capacitor-project.config.json found, please run native:switch-project first')
  process.exit(1)
}

if (!fs.existsSync('.env.native')) {
  console.error('No .env.native found, please run native:switch-project first')
  process.exit(1)
}

const capacitorProjectConfig = require('./capacitor-project.config.json')

const config = {
  appId: process.env.NATIVE_DEV ? `${capacitorProjectConfig.appId}.dev` : capacitorProjectConfig.appId,
  appName: capacitorProjectConfig.appName,
  webDir: 'dist',
  bundledWebRuntime: false,
  plugins: {
    SplashScreen: {
      launchAutoHide: false,
      launchShowDuration: 5,
      showSpinner: false,
      splashImmersive: true,
      backgroundColor: '#21303A',
      androidScaleType: 'CENTER_CROP'
    },
    PushNotifications: {
      presentationOptions: ['badge', 'sound', 'alert']
    },
    Keyboard: {
      resize: 'native'
    }
  }
}

if (!process.env.PACKAGE_NATIVE) {
  console.log('Configuring for HMR')

  config.server = {
    url: `http://${ip.address()}:3000`,
    cleartext: true
  }
}

if (argv.run) {
  console.log(config.server ? `Native HMR ready on ${config.server.url}` : 'Native HMR disabled')
}

module.exports = config
jcesarmobile commented 3 years ago

No problem, it was a possibility that we might have broken it with some changes we made for the ts, so I didn’t close until verifying that it was working. Will add an example on the docs

alexcroox commented 3 years ago

I’m glad you say that as I’m sure I tried it during the various alphas and it didn’t work at the point I tried. Thanks again for supporting it! ❤️

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.