1.1.0 • Published 5 years ago

esm-config v1.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

ESM Config

Transform ES module configs to cjs using rollup.

Why?

Since ES6 import and export are standard, yet there is still no support only experimental support in NodeJS.

Most of the time that is not a problem, since projects have a build step transforming (webpack/rollup/babel) the syntax. So people expect import/export to just work.

This module allows you to use ES modules syntax to write your configuration. Build a project or framework that allows more advanced configuration than toml, yaml or json would. Like rollup.config.js, webpack.config.js, or postcss.config.js. (Only rollup allows import/export out of the box).

Usage

Within your project let esm-config import your configuration file.

const esmConfig = require("esm-config")

// /example/config.js may contain import/export syntax
const config = esmConfig("/example/config.js")

// you can provide a default configuration
// it is used, if the config file does not exist
// it is passed, if the config file exports a function
const defaultConfiguration = {
	name: "defaultName"
}
const config = esmConfig("/example/config.js", defaultConfiguration)

The configuration can then be written as either ESM or CJS module.

// example ESM config
export default {
	name: "hello world"
}

// example exporting a function
// is passed the default configuration
export default function(defaultConfiguration) {
	const { name } = defaultConfiguration
	return {
		name,
		otherConfig: false
	}
}

// example CJS config
// this would work in nodejs without esm-config
module.exports = {
	name: "hello world"
}

Background

This uses a modified version of rollups internal loadConfigFile function.

Rollup configs can be written using ES modules syntax, because rollup transforms them to CJS before using them.

License

MIT