Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic .env support. #258

Merged
merged 7 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"commander": "^2.11.0",
"cross-spawn": "^5.1.0",
"cssnano": "^3.10.0",
"dotenv": "^4.0.0",
"get-port": "^3.2.0",
"glob": "^7.1.2",
"htmlnano": "^0.1.6",
Expand Down
3 changes: 3 additions & 0 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const PackagerRegistry = require('./packagers');
const localRequire = require('./utils/localRequire');
const config = require('./utils/config');
const emoji = require('./utils/emoji');
const loadEnv = require('./utils/env');

/**
* The Bundler is the main entry point. It resolves and loads assets,
Expand Down Expand Up @@ -176,8 +177,10 @@ class Bundler extends EventEmitter {
}

await this.loadPlugins();
await loadEnv(this.mainFile);

this.options.extensions = Object.assign({}, this.parser.extensions);
this.options.env = process.env;
this.farm = WorkerFarm.getShared(this.options);

if (this.options.watch) {
Expand Down
26 changes: 26 additions & 0 deletions src/utils/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const config = require('./config');
const dotenv = require('dotenv');

async function loadEnv(filepath) {
const NODE_ENV = process.env.NODE_ENV || 'development';
const dotenvFiles = [
`.env.${NODE_ENV}.local`,
`.env.${NODE_ENV}`,
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
NODE_ENV !== 'test' && '.env.local',
'.env'
].filter(Boolean);

await Promise.all(
dotenvFiles.map(async dotenvFile => {
const envPath = await config.resolve(filepath, [dotenvFile]);
if (envPath) {
dotenv.config({path: envPath});
}
})
);
}

module.exports = loadEnv;
1 change: 1 addition & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let parser;

exports.init = function(options, callback) {
parser = new Parser(options || {});
Object.assign(process.env, options.env || {});
callback();
};

Expand Down
2 changes: 2 additions & 0 deletions test/integration/env-file/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FOO=bar
BAR=test
1 change: 1 addition & 0 deletions test/integration/env-file/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = process.env.FOO + process.env.BAR;
7 changes: 7 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ describe('javascript', function() {
assert.equal(output(), 'test:test');
});

it('should insert environment variables from a file', async function() {
let b = await bundle(__dirname + '/integration/env-file/index.js');

let output = run(b);
assert.equal(output, 'bartest');
});

it('should support adding implicit dependencies', async function() {
let b = await bundle(__dirname + '/integration/json/index.js', {
delegate: {
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,10 @@ domutils@^1.5.1:
dom-serializer "0"
domelementtype "1"

dotenv@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-4.0.0.tgz#864ef1379aced55ce6f95debecdce179f7a0cd1d"

ecc-jsbn@~0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz#0fc73a9ed5f0d53c38193398523ef7e543777505"
Expand Down