Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.
This repository was archived by the owner on Nov 9, 2022. It is now read-only.

New Config Strategy Proposal: STRATEGY_MAKECONFIG_REFERENCE #145

@niieani

Description

@niieani

One of the significant limitations of beemo today is that you can't create drivers that make configs from non-serializable configurations, e.g. that include classes/instances or functions in the configs.

I propose a new strategy to generate configs, similar to STRATEGY_REFERENCE.

Instead of it simply re-exporting the provider config:

await fs.writeFile(configPath.path(), `module.exports = require('./${requirePath}');`);

The provider reference would be required to export a makeConfig function (instead of an actual config object), that takes in 2 arguments: driver options object and the optional local config override and returns the config. The resulting generated config file would look something like this:

import {makeConfig} from '@niieani/beemo-build-tools/configs/vite.ts';
import overrides from './.config/beemo/vite.ts';
export default makeConfig(
  {
    "some-custom-option": "123"
  },
  overrides
);

Since some configs don't understand TypeScript, it might be necessary for both the consumer overrides and the provider's makeConfig were written in pure JS, so the emit would be:

const {makeConfig} = require('@niieani/beemo-build-tools/configs/vite.js');
module.exports = makeConfig(
  {
    "some-custom-option": "123"
  },
  require('./.config/beemo/vite.js')
);

I've hacked this together today using the template strategy (nice flexibility!), but it would be nice to have something built-in as a shorthand for this:

export default function customTemplate(
  [providerConfig, consumerConfig]: ConfigObject[],
  options: ConfigTemplateOptions,
): ConfigTemplateResult {
  const {configModule} = options
  const providerConfigModule = `${configModule}${options.providerConfigPath
    .path()
    .split(configModule)
    .pop()}`

  const consumerPath =
    consumerConfig &&
    options.tool.project.root.relativeTo(options.consumerConfigPath).path()

  const {
    args,
    configStrategy,
    dependencies,
    env,
    expandGlobs,
    outputStrategy,
    template,
    // omit internals, keep only actual config:
    ...driverOptions
  } = (options.driver as ViteDriver).options

  return {
    config: [
      `import {makeConfig} from '${providerConfigModule}';`,
      consumerPath ? `import overrides from './${consumerPath}';` : undefined,
      `export default makeConfig(`,
      `  ${JSON.stringify(driverOptions, null, 2).split('\n').join('\n  ')},`,
      consumerPath ? '  overrides' : undefined,
      `);`,
    ]
      .filter(Boolean)
      .join('\n'),
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions