Skip to content

Commit

Permalink
Fix: load .env value also if entry target is specified with source
Browse files Browse the repository at this point in the history
…value in package.json (#7537)

Co-authored-by: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
  • Loading branch information
Shinyaigeek and mischnic committed Apr 15, 2022
1 parent ddd640c commit 681f2b3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/core/core/src/resolveOptions.js
Expand Up @@ -14,7 +14,7 @@ import {hashString} from '@parcel/hash';
import {NodeFS} from '@parcel/fs';
import {LMDBCache, FSCache} from '@parcel/cache';
import {NodePackageManager} from '@parcel/package-manager';
import {getRootDir, relativePath, resolveConfig} from '@parcel/utils';
import {getRootDir, relativePath, resolveConfig, isGlob} from '@parcel/utils';
import loadDotEnv from './loadDotEnv';
import {toProjectPath} from './projectPath';
import {getResolveFrom} from './requests/ParcelConfigRequest';
Expand Down Expand Up @@ -50,7 +50,21 @@ export default async function resolveOptions(
entries = [path.resolve(inputCwd, initialOptions.entries)];
}

let entryRoot = getRootDir(entries);
let shouldMakeEntryReferFolder = false;
if (entries.length === 1 && !isGlob(entries[0])) {
let [entry] = entries;
try {
shouldMakeEntryReferFolder = (await inputFS.stat(entry)).isDirectory();
} catch {
// ignore failing stat call
}
}

// getRootDir treats the input as files, so getRootDir(["/home/user/myproject"]) returns "/home/user".
// Instead we need to make the the entry refer to some file inside the specified folders if entries refers to the directory.
let entryRoot = getRootDir(
shouldMakeEntryReferFolder ? [path.join(entries[0], 'index')] : entries,
);
let projectRootFile =
(await resolveConfig(
inputFS,
Expand Down
@@ -0,0 +1,2 @@
FOO=bar
BAR=test
@@ -0,0 +1 @@
module.exports = process.env.FOO + process.env.BAR;
@@ -0,0 +1,4 @@
{
"name": "env-file-with-package-source",
"source": "index.js"
}
Empty file.
9 changes: 9 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Expand Up @@ -3113,6 +3113,15 @@ describe('javascript', function () {
assert.equal(output, 'barbaz');
});

it('should insert environment variables from a file even if entry file is specified with source value in package.json', async function () {
let b = await bundle(
path.join(__dirname, '/integration/env-file-with-package-source'),
);

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

it('should error on process.env mutations', async function () {
let filePath = path.join(__dirname, '/integration/env-mutate/index.js');
await assert.rejects(bundle(filePath), {
Expand Down

0 comments on commit 681f2b3

Please sign in to comment.