Skip to content

Commit

Permalink
Working plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Jun 20, 2018
0 parents commit ca63f4a
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { BABEL_ENV, NODE_ENV } = process.env
const modules = BABEL_ENV === 'cjs' || NODE_ENV === 'test' ? 'commonjs' : false
const loose = true

module.exports = {
presets: [
['@babel/env', {
loose,
modules,
}],
],
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
10 changes: 10 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"{src,__tests__}/**/*.js": [
"prettier --single-quote --no-semi --trailing-comma=all --write",
"git add"
],
"*.md": [
"prettier --single-quote --no-semi --trailing-comma=all --write",
"git add"
]
}
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# rollup-plugin-dotenv
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "rollup-plugin-dotenv",
"version": "0.0.0",
"description": "",
"main": "dist/rollup-plugin-dotenv.js",
"files": [
"dist"
],
"scripts": {
"precommit": "lint-staged",
"prebuild": "rimraf dist",
"build": "rollup -c",
"prepare": "npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Andarist/rollup-plugin-dotenv.git"
},
"keywords": [
"rollup",
"dotenv"
],
"author": "Mateusz Burzyński <[email protected]> (https://github.com/Andarist)",
"license": "MIT",
"bugs": {
"url": "https://github.com/Andarist/rollup-plugin-dotenv/issues"
},
"homepage": "https://github.com/Andarist/rollup-plugin-dotenv#readme",
"devDependencies": {
"@babel/core": "7.0.0-beta.51",
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.51",
"@babel/preset-env": "7.0.0-beta.51",
"builtin-modules": "^3.0.0",
"husky": "^0.14.3",
"lint-staged": "^7.0.0",
"prettier": "^1.13.5",
"rimraf": "^2.6.2",
"rollup": "^0.60.7",
"rollup-plugin-babel": "4.0.0-beta.5"
},
"dependencies": {
"dotenv": "^6.0.0",
"rollup-plugin-replace": "^2.0.0"
}
}
22 changes: 22 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import babel from 'rollup-plugin-babel'
import builtinModules from 'builtin-modules'
import pkg from './package.json'

const makeExternalPredicate = externalsArr => {
if (externalsArr.length === 0) {
return () => false
}
const externalPattern = new RegExp(`^(${ externalsArr.join('|') })($|/)`)
return id => externalPattern.test(id)
}

export default {
input: 'src/index.js',
output: { file: pkg.main, format: 'cjs' },
external: makeExternalPredicate([
...builtinModules,
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
]),
plugins: [babel()],
}
46 changes: 46 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from 'fs'
import path from 'path'
import dotenv from 'dotenv'
import replace from 'rollup-plugin-replace'
import { mapKeys, mapValues, pick, pipe, shallowMergeAll } from './utils'

const withDefaults = ({ cwd = '.', envKey = 'NODE_ENV' } = {}) => ({
cwd: path.resolve(process.cwd(), cwd),
envKey,
})

export default function dotenvPlugin(inputOptions) {
const { cwd, envKey } = withDefaults(inputOptions)

return {
...replace(
pipe(
priorities =>
[...priorities]
.reverse()
.map(dotenvFile => path.join(cwd, dotenvFile))
.filter(fs.existsSync)
.map(dotenvFile => fs.readFileSync(dotenvFile))
.map(dotenv.parse),
shallowMergeAll,
envVars =>
shallowMergeAll([
envVars,
pick(
Object.keys(envVars).filter(
key => process.env[key] !== undefined,
),
process.env,
),
]),
envVars => mapKeys(key => `process.env.${key}`, envVars),
envVars => mapValues(value => JSON.stringify(value), envVars),
)([
`.env.${process.env[envKey]}.local`,
`.env.${process.env[envKey]}`,
'.env',
]),
),
name: 'dotenv',
}
}
22 changes: 22 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const mapKeys = (mapper, obj) =>
Object.keys(obj).reduce((acc, key) => {
acc[mapper(key)] = obj[key]
return acc
}, {})

export const mapValues = (mapper, obj) =>
Object.keys(obj).reduce((acc, key) => {
acc[key] = mapper(obj[key])
return acc
}, {})

export const pick = (props, obj) =>
props.reduce((acc, prop) => {
acc[prop] = obj[prop]
return acc
}, {})

export const pipe = (...funcs) =>
funcs.reduceRight((piped, next) => (...args) => piped(next(...args)))

export const shallowMergeAll = objs => Object.assign(...objs)

0 comments on commit ca63f4a

Please sign in to comment.