Skip to content

Commit

Permalink
Add basic .env support. (#258)
Browse files Browse the repository at this point in the history
* Add .env support.

* Handle promise rejection.

* Use process.env.DOTENV

* Use dotenv

* Use dotenv module

* Move env loading to a util, copy to workers, add tests
  • Loading branch information
gongpeione authored and devongovett committed Jan 3, 2018
1 parent 989e9c2 commit 6c9a2f0
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/parcel/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 packages/core/parcel/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 packages/core/parcel/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 packages/core/parcel/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 packages/core/parcel/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 packages/core/parcel/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 packages/core/parcel/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

0 comments on commit 6c9a2f0

Please sign in to comment.