esm-config
Transform es module configs to cjs using rollup.
Last updated 2 months ago by ocjojo .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install esm-config 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.1.0                                ...           latest (2 months ago)

2 Versions

  • 1.1.0                                ...           2 months ago
  • 1.0.0                                ...           3 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 19
Dependencies (1)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |