Git Product home page Git Product logo

unconfig's Introduction

unconfig

NPM version

A universal solution for loading configurations.

Why?

Configuration is hard, especially when you want to build an ecosystem of your tools.

You want your tools to be general and easy to use, you allow your config to be defined in a custom field of package.json.

You want your tools to be easy to integrate, you allow the configs to be defined in other tools' configurations like vite.config.js or webpack.config.js.

You want the configs to be agnostic and probably need to be load by IDE, you create new config files like .myconfigrc.

You want the configs to also be flexible and dynamic, you make your config files a JavaScript file, like my.config.js.

Then you want users to be able to use ESM and TypeScript, you also make your config accepting .ts or .mjs.

So users' project root end up with a lot of config files like .npmrc, rollup.config.js, .eslintrc, tsconfig.json, jest.config.js, postcss.config.js, nuxt.config.js, vite.config.cjs, windi.config.ts, etc. And each of them use different syntax, in JSON, in CJS, in ESM, in TypeScript, in INI, in TOML...

unconfig can't solve this fragmentation entirely, but it's trying to make loading them easier for tool authors.

Usage

npm i unconfig

For example, to load config for my.config:

import { loadConfig } from 'unconfig'

const { config, sources } = await loadConfig({
  sources: [
    // load from `my.config.xx`
    {
      files: 'my.config',
      // default extensions
      extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''],
    },
    // load `my` field in `package.json` if no above config files found
    {
      files: 'package.json',
      extensions: [],
      rewrite(config) {
        return config?.my
      },
    },
    // load inline config from `vite.config`
    {
      files: 'vite.config',
      async rewrite(config) {
        const resolved = await (typeof config === 'function' ? config() : config)
        return resolved?.my
      },
    },
    // ...
  ],
  // if false, the only the first matched will be loaded
  // if true, all matched will be loaded and deep merged
  merge: false,
})

unconfig supports loading ts, mjs, js, json by default.

Sponsors

License

MIT License © 2021 Anthony Fu

unconfig's People

Contributors

antfu avatar anubra266 avatar chrisbbreuer avatar ecstrema avatar laoer536 avatar lotwt avatar sajadhsm avatar sukkaw avatar userquin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

unconfig's Issues

Allow stubbing module to get config

For example:

import Icons from 'unplugin-icons/vite'

export default {
  plugins: [
    Icons({ /* options */ }),
  ]
}

Can be loaded as:

import Icons from '@replaced-module'

export default {
  plugins: [
    Icons({ /* options */ }),
  ]
}
// @replaced-module

export defualt (options) {
  emitOptions(options)
}

to retrieve the config without asking to export it from the plugin aurthor side.

"SyntaxError: Unexpected identifier" when loading vite.config with `@sveltejs/vite-plugin-svelte` or `vite-plugin-solid` plugins

Describe the bug

I encountered this error when using unocss vscode extention. I already opened issue in unocss repo unocss/unocss#2088.

When I try to load vite.config with @sveltejs/vite-plugin-svelte or vite-plugin-solid plugin i get this error.

/home/melkam/unconfig-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/vitefu/src/index.js:15
    const { createRequire } = (await Promise.resolve().then(() => require('module'))).default;
                                     ^^^^^^^

SyntaxError: Unexpected identifier
    at new Script (node:vm:100:7)
    at createScript (node:vm:259:10)
    at Object.runInThisContext (node:vm:307:10)
    at jiti (/home/melkam/unconfig-bug/node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:5:24838)
    at /home/melkam/unconfig-bug/node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/vite-plugin-solid/dist/esm/index.mjs:8:15
    at jiti (/home/melkam/unconfig-bug/node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:5:25030)
    at /home/melkam/unconfig-bug/vite.config.ts:2:47
    at jiti (/home/melkam/unconfig-bug/node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:5:25030)
    at loadConfigFile (file:///home/melkam/unconfig-bug/node_modules/.pnpm/[email protected]/node_modules/unconfig/dist/index.mjs:145:11)
    at async Object.load (file:///home/melkam/unconfig-bug/node_modules/.pnpm/[email protected]/node_modules/unconfig/dist/index.mjs:74:24)
    at async file:///home/melkam/unconfig-bug/test.ts:1:47

Reproduction

https://codesandbox.io/p/sandbox/unconfig-vite-solid-bug-2wrobj

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
    CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
    Memory: 6.14 GB / 7.74 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.13.0 - ~/.nvm/versions/node/v18.13.0/bin/node
    npm: 8.19.3 - ~/.nvm/versions/node/v18.13.0/bin/npm

Used Package Manager

pnpm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Allow glob pattern in sources

Suppose I want to find all config files below the current directory, I'd like to be able to do something like:

const { config, sources } = await unconfigLoad<MyConfig>({
    cwd: directory,
    sources: [
        {
            files: "**/my.config",
            extensions: ["js", "ts"]
        }
    ]
});

Does that introduce any weird complexity?

Great package btw. Super useful.

vitest is in dependencies

Discovered this as a nested dependency is failing windows CI of nuxt. Is it an intentional dependency for unconfig?

image

Please remove nuxt.config example

unconfig is not compatible nuxt.config loading. There are multiple sources and specific merging strategies provided by nuxt.loadConfig (nuxt 2) and @nuxt/kit (nuxt 2 and nuxt 3) that this package is not providing and misleading users.

Current loading strategy is: (which might change)

  • .env loaded by a specific integration with destr
  • nuxt.config.{ts,js} loaded by jiti with populated env (it might be also async function)
  • .nuxtrc loaded by rc9 and $XDG_HOME/.nuxtrc

Override configs after loading from file

Suppose I got some configurations from CLI, and I want to merge them with the configurations read from the file.

$ my-cli dev --force --output custom_path

Can I do this with unconfig?

Like:

import { loadConfig } from 'unconfig'

const { force, output } = '(cli-options)'
const { config, sources } = await loadConfig({
  sources: [
    // load from `my.config.xx`
    {
      files: 'my.config',
    },
    // ...
  ],
  // override configs after sources
  override: {
    mode: {
      force: force,
    },
    outDir: output,
  },
  merge: false,
})

When my config content is array, I get a {}

Describe the bug

It seems that the reason is that array is not supported when using defu for config merge
image

Reproduction

  1. build.config returns an array.
  2. In terminal, execute npm run dev

System Info

System:
    OS: macOS 13.3.1
    CPU: (8) x64 Apple M1 Pro
    Memory: 21.34 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.20.0 - ~/.nvm/versions/node/v16.20.0/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 8.19.4 - ~/.nvm/versions/node/v16.20.0/bin/npm
    pnpm: 8.15.4 - ~/.nvm/versions/node/v16.20.0/bin/pnpm
  Browsers:
    Chrome: 123.0.6312.87
    Safari: 16.4

Used Package Manager

pnpm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.