Transform ES module configs to cjs using rollup.
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 (webpack/rollup/babel) transforming 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).
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"
}
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.
MIT